aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/installer/init.lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-02-17 20:08:45 +0100
committerGitHub <noreply@github.com>2023-02-17 20:08:45 +0100
commitf3d90e3b7580a2d1b47a1f9b905808e22a7fac87 (patch)
treed8d1822c88f8f8da9a11cdb45cb3207530510662 /lua/mason-core/installer/init.lua
parentchore(workflows): change autogenerate commit message (#1004) (diff)
downloadmason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.tar
mason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.tar.gz
mason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.tar.bz2
mason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.tar.lz
mason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.tar.xz
mason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.tar.zst
mason-f3d90e3b7580a2d1b47a1f9b905808e22a7fac87.zip
feat(installer): add share links (#965)
* feat(installer): add share links Adds the ability to symlink share files to ~/.local/share/nvim/mason/share (default location). This is currently not used by any packages but will be soon (e.g. linking .jar files to a canonical location on the fs). This also includes the following changes: - fix(windows): correctly unlink executables Prior to this change, executables on Windows would not be removed when uninstalling a package. - refactor(installer): use Result interfaces The motivation behind this is to move away from exceptions and pcalls to leverage the Result interface. This allows for better error messaging during installation, as well as improved composability of actions that may or may not fail. - refactor(bin): use absolute paths in exec wrapper scripts While relative paths are preferred and will end up returning in the future, they i) cannot be guaranteed for all packages, and ii) is somewhat complicated to produce due to lack of std APIs. Moving the entire Mason installation directory was never officially supported anyway. - feat(installer): add "force" flag When this flag is true, any existing executables or share files will be overridden if they exist (i.e. mangle another package installation). * refactor(result): always return Result objects in Result.try The rationale here used to be that exceptions in Result.try() blocks were treated truly as exceptions that should interrupt code execution per Lua's traditional error handling semantics. However, Lua code is somewhat prone to raise exceptions when you don't expect it to (especially when interacting with loosely documented external APIs). Combine this with the fact that code that invokes Result.try() blocks generally doesn't `pcall` and only relies on the Result API to handle errors, you end up with code that only gracefully handles one class of errors (the well-known ones). * test(terminator): sleep in tests to avoid race condition I've no idea why this doesn't pass in CI, works just fine locally.
Diffstat (limited to 'lua/mason-core/installer/init.lua')
-rw-r--r--lua/mason-core/installer/init.lua123
1 files changed, 78 insertions, 45 deletions
diff --git a/lua/mason-core/installer/init.lua b/lua/mason-core/installer/init.lua
index a11de45d..8e05cb20 100644
--- a/lua/mason-core/installer/init.lua
+++ b/lua/mason-core/installer/init.lua
@@ -17,24 +17,34 @@ local M = {}
---@async
local function create_prefix_dirs()
- for _, p in ipairs { path.install_prefix(), path.bin_prefix(), path.package_prefix(), path.package_build_prefix() } do
- if not fs.async.dir_exists(p) then
- fs.async.mkdirp(p)
+ return Result.try(function(try)
+ for _, p in ipairs {
+ path.install_prefix(),
+ path.bin_prefix(),
+ path.share_prefix(),
+ path.package_prefix(),
+ path.package_build_prefix(),
+ } do
+ if not fs.async.dir_exists(p) then
+ try(Result.pcall(fs.async.mkdirp, p))
+ end
end
- end
+ end)
end
---@async
---@param context InstallContext
local function write_receipt(context)
- log.fmt_debug("Writing receipt for %s", context.package)
- context.receipt
- :with_name(context.package.name)
- :with_schema_version("1.0")
- :with_completion_time(vim.loop.gettimeofday())
- local receipt_path = path.concat { context.cwd:get(), "mason-receipt.json" }
- local install_receipt = context.receipt:build()
- fs.async.write_file(receipt_path, vim.json.encode(install_receipt))
+ return Result.pcall(function()
+ log.fmt_debug("Writing receipt for %s", context.package)
+ context.receipt
+ :with_name(context.package.name)
+ :with_schema_version("1.1")
+ :with_completion_time(vim.loop.gettimeofday())
+ local receipt_path = path.concat { context.cwd:get(), "mason-receipt.json" }
+ local install_receipt = context.receipt:build()
+ fs.async.write_file(receipt_path, vim.json.encode(install_receipt))
+ end)
end
local CONTEXT_REQUEST = {}
@@ -47,13 +57,17 @@ end
---@async
---@param context InstallContext
function M.prepare_installer(context)
- create_prefix_dirs()
- local package_build_prefix = path.package_build_prefix(context.package.name)
- if fs.async.dir_exists(package_build_prefix) then
- fs.async.rmrf(package_build_prefix)
- end
- fs.async.mkdirp(package_build_prefix)
- context.cwd:set(package_build_prefix)
+ return Result.try(function(try)
+ try(create_prefix_dirs())
+ local package_build_prefix = path.package_build_prefix(context.package.name)
+ if fs.async.dir_exists(package_build_prefix) then
+ try(Result.pcall(fs.async.rmrf, package_build_prefix))
+ end
+ try(Result.pcall(fs.async.mkdirp, package_build_prefix))
+ context.cwd:set(package_build_prefix)
+
+ return context.package.spec.install
+ end)
end
---@async
@@ -81,12 +95,40 @@ function M.exec_in_context(context, fn)
end
end
context.receipt:with_start_time(vim.loop.gettimeofday())
- M.prepare_installer(context)
step(context)
return ret_val
end
---@async
+---@param context InstallContext
+---@param installer async fun(ctx: InstallContext)
+local function run_installer(context, installer)
+ local handle = context.handle
+ return Result.pcall(function()
+ return a.wait(function(resolve, reject)
+ local cancel_thread = a.run(M.exec_in_context, function(success, result)
+ if success then
+ resolve(result)
+ else
+ reject(result)
+ end
+ end, context, installer)
+
+ handle:once("terminate", function()
+ cancel_thread()
+ if handle:is_closed() then
+ reject "Installation was aborted."
+ else
+ handle:once("closed", function()
+ reject "Installation was aborted."
+ end)
+ end
+ end)
+ end)
+ end)
+end
+
+---@async
---@param handle InstallHandle
---@param opts PackageInstallOpts
function M.execute(handle, opts)
@@ -117,34 +159,25 @@ function M.execute(handle, opts)
handle:on("stderr", append_log)
end
- log.fmt_info("Executing installer for %s", pkg)
- return Result.run_catching(function()
- -- 1. run installer
- a.wait(function(resolve, reject)
- local cancel_thread = a.run(M.exec_in_context, function(success, result)
- if success then
- resolve(result)
- else
- reject(result)
- end
- end, context, pkg.spec.install)
+ log.fmt_info("Executing installer for %s version=%s", pkg, opts.version or "latest")
- handle:once("terminate", function()
- handle:once("closed", function()
- reject "Installation was aborted."
- end)
- cancel_thread()
- end)
- end)
+ return Result.try(function(try)
+ -- 1. prepare directories and initialize cwd
+ local installer = try(M.prepare_installer(context))
+
+ -- 2. execute installer
+ try(run_installer(context, installer))
- -- 2. promote temporary installation dir
- context:promote_cwd()
+ -- 3. promote temporary installation dir
+ try(Result.pcall(function()
+ context:promote_cwd()
+ end))
- -- 3. link package
- linker.link(context)
+ -- 4. link package
+ try(linker.link(context))
- -- 4. write receipt
- write_receipt(context)
+ -- 5. write receipt
+ try(write_receipt(context))
end)
:on_success(function()
permit:forget()
@@ -173,7 +206,7 @@ function M.execute(handle, opts)
end
-- unlink linked executables (in the rare occasion an error occurs after linking)
- linker.unlink(context.package, context.receipt.links)
+ linker.unlink(context.package, context.receipt)
if not handle:is_closed() and not handle.is_terminated then
handle:close()