aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/core/installer/context.lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2022-04-21 12:09:59 +0200
committerGitHub <noreply@github.com>2022-04-21 12:09:59 +0200
commitb68fcc6bb2c770495ff8e2508c06dfdd49abcc80 (patch)
treedf7c71efb59958deb21a18eeccf3e3c43c4cd704 /lua/nvim-lsp-installer/core/installer/context.lua
parentrun autogen_metadata.lua (diff)
downloadmason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.tar
mason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.tar.gz
mason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.tar.bz2
mason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.tar.lz
mason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.tar.xz
mason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.tar.zst
mason-b68fcc6bb2c770495ff8e2508c06dfdd49abcc80.zip
chore: refactor remaining installers to async impl (#616)
Diffstat (limited to 'lua/nvim-lsp-installer/core/installer/context.lua')
-rw-r--r--lua/nvim-lsp-installer/core/installer/context.lua42
1 files changed, 42 insertions, 0 deletions
diff --git a/lua/nvim-lsp-installer/core/installer/context.lua b/lua/nvim-lsp-installer/core/installer/context.lua
index f97f334f..366a3b77 100644
--- a/lua/nvim-lsp-installer/core/installer/context.lua
+++ b/lua/nvim-lsp-installer/core/installer/context.lua
@@ -63,6 +63,31 @@ function ContextualFs:dir_exists(rel_path)
return fs.dir_exists(path.concat { self.cwd:get(), rel_path })
end
+---@async
+---@param rel_path string @The relative path from the current working directory.
+function ContextualFs:rmrf(rel_path)
+ return fs.rmrf(path.concat { self.cwd:get(), rel_path })
+end
+
+---@async
+---@param rel_path string @The relative path from the current working directory.
+function ContextualFs:unlink(rel_path)
+ return fs.unlink(path.concat { self.cwd:get(), rel_path })
+end
+
+---@async
+---@param old_path string
+---@param new_path string
+function ContextualFs:rename(old_path, new_path)
+ return fs.rename(path.concat { self.cwd:get(), old_path }, path.concat { self.cwd:get(), new_path })
+end
+
+---@async
+---@param dirpath string
+function ContextualFs:mkdir(dirpath)
+ return fs.mkdir(path.concat { self.cwd:get(), dirpath })
+end
+
---@class CwdManager
---@field private boundary_path string @Defines the upper boundary for which paths are allowed as cwd.
---@field private cwd string
@@ -101,6 +126,7 @@ end
---@field public cwd CwdManager
---@field public destination_dir string
---@field public stdio_sink StdioSink
+---@field public boundary_path string
local InstallContext = {}
InstallContext.__index = InstallContext
@@ -112,6 +138,7 @@ function InstallContext.new(opts)
spawn = ContextualSpawn.new(cwd_manager, opts.stdio_sink),
fs = ContextualFs.new(cwd_manager),
receipt = receipt.InstallReceiptBuilder.new(),
+ boundary_path = opts.boundary_path,
destination_dir = opts.destination_dir,
requested_version = opts.requested_version,
stdio_sink = opts.stdio_sink,
@@ -154,4 +181,19 @@ function InstallContext:run_concurrently(suspend_fns)
end, suspend_fns))
end
+---@param rel_path string @The relative path from the current working directory to change cwd to. Will only restore to the initial cwd after execution of fn (if provided).
+---@param fn async fun() @(optional) The function to run in the context of the given path.
+function InstallContext:chdir(rel_path, fn)
+ local old_cwd = self.cwd:get()
+ self.cwd:set(path.concat { old_cwd, rel_path })
+ if fn then
+ local ok, result = pcall(fn)
+ self.cwd:set(old_cwd)
+ if not ok then
+ error(result, 0)
+ end
+ return result
+ end
+end
+
return InstallContext