diff options
| author | William Boman <william@redwill.se> | 2022-07-31 00:45:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-31 00:45:17 +0200 |
| commit | 116a825929fa3066f32afc77f7459630419bc347 (patch) | |
| tree | a695ae708b67b420d750f2dbc3bb76b0eb713735 /lua | |
| parent | feat(taplo): use prebuilt binary if available (#201) (diff) | |
| download | mason-116a825929fa3066f32afc77f7459630419bc347.tar mason-116a825929fa3066f32afc77f7459630419bc347.tar.gz mason-116a825929fa3066f32afc77f7459630419bc347.tar.bz2 mason-116a825929fa3066f32afc77f7459630419bc347.tar.lz mason-116a825929fa3066f32afc77f7459630419bc347.tar.xz mason-116a825929fa3066f32afc77f7459630419bc347.tar.zst mason-116a825929fa3066f32afc77f7459630419bc347.zip | |
fix(gem): write shim executables that enhance GEM_PATH appropriately (#202)
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/mason-core/installer/context.lua | 35 | ||||
| -rw-r--r-- | lua/mason-core/managers/gem/init.lua | 51 |
2 files changed, 52 insertions, 34 deletions
diff --git a/lua/mason-core/installer/context.lua b/lua/mason-core/installer/context.lua index a3577cfa..93d45a8a 100644 --- a/lua/mason-core/installer/context.lua +++ b/lua/mason-core/installer/context.lua @@ -226,29 +226,42 @@ end ---@param new_executable_rel_path string: Relative path to the executable file to create. ---@param command string: The shell command to run. +---@param env table<string, string>? ---@return string: The created executable filename. -function InstallContext:write_shell_exec_wrapper(new_executable_rel_path, command) +function InstallContext:write_shell_exec_wrapper(new_executable_rel_path, command, env) return platform.when { unix = function() local std = require "mason-core.managers.std" + local formatted_envs = _.map(function(pair) + local var, value = pair[1], pair[2] + return ("export %s=%q"):format(var, value) + end, _.to_pairs(env or {})) + self.fs:write_file( new_executable_rel_path, _.dedent(([[ #!/bin/bash + %s exec %s "$@" - ]]):format(command)) + ]]):format(_.join("\n", formatted_envs), command)) ) std.chmod("+x", { new_executable_rel_path }) return new_executable_rel_path end, win = function() local executable_file = ("%s.cmd"):format(new_executable_rel_path) + local formatted_envs = _.map(function(pair) + local var, value = pair[1], pair[2] + return ("SET %s=%s"):format(var, value) + end, _.to_pairs(env or {})) + self.fs:write_file( executable_file, _.dedent(([[ @ECHO off + %s %s %%* - ]]):format(command)) + ]]):format(_.join("\n", formatted_envs), command)) ) return executable_file end, @@ -259,20 +272,4 @@ function InstallContext:link_bin(executable, rel_path) self.receipt:with_link("bin", executable, rel_path) end ----@param patches string[] -function InstallContext:apply_patches(patches) - for _, patch in ipairs(patches) do - self.spawn.patch { - "-g", - "0", - "-f", - on_spawn = function(_, stdio) - local stdin = stdio[1] - stdin:write(patch) - stdin:close() - end, - } - end -end - return InstallContext diff --git a/lua/mason-core/managers/gem/init.lua b/lua/mason-core/managers/gem/init.lua index 8b64d68a..da9b800b 100644 --- a/lua/mason-core/managers/gem/init.lua +++ b/lua/mason-core/managers/gem/init.lua @@ -9,9 +9,38 @@ local platform = require "mason-core.platform" local M = {} +---@param install_dir string +local function env(install_dir) + return { + GEM_HOME = install_dir, + GEM_PATH = install_dir, + PATH = process.extend_path { path.concat { install_dir, "bin" } }, + } +end + local create_bin_path = _.compose(path.concat, function(executable) return _.append(executable, { "bin" }) -end, _.if_else(_.always(platform.is.win), _.format "%s.cmd", _.identity)) +end, _.if_else(_.always(platform.is.win), _.format "%s.bat", _.identity)) + +---@async +---@param executable string +local function link_executable(executable) + local ctx = installer.context() + local bin_path = create_bin_path(executable) + ctx:link_bin( + executable, + ctx:write_shell_exec_wrapper(executable, path.concat { ctx.package:get_install_path(), bin_path }, { + GEM_PATH = platform.when { + unix = function() + return ("%s:$GEM_PATH"):format(ctx.package:get_install_path()) + end, + win = function() + return ("%s;%%GEM_PATH%%"):format(ctx.package:get_install_path()) + end, + }, + }) + ) +end ---@param packages string[] local function with_receipt(packages) @@ -49,12 +78,13 @@ function M.install(packages) "--bindir=bin", "--no-document", pkgs, + env = { + GEM_HOME = ctx.cwd:get(), + }, } if packages.bin then - _.each(function(executable) - ctx:link_bin(executable, create_bin_path(executable)) - end, packages.bin) + _.each(link_executable, packages.bin) end return { @@ -109,7 +139,7 @@ function M.check_outdated_primary_package(receipt, install_dir) if receipt.primary_source.type ~= "gem" then return Result.failure "Receipt does not have a primary source of type gem" end - return spawn.gem({ "outdated", cwd = install_dir, env = M.env(install_dir) }):map_catching(function(result) + return spawn.gem({ "outdated", cwd = install_dir, env = env(install_dir) }):map_catching(function(result) ---@type string[] local lines = vim.split(result.stdout, "\n") local outdated_gems = vim.tbl_map(M.parse_outdated_gem, vim.tbl_filter(not_empty, lines)) @@ -138,7 +168,7 @@ function M.get_installed_primary_package_version(receipt, install_dir) .gem({ "list", cwd = install_dir, - env = M.env(install_dir), + env = env(install_dir), }) :map_catching(function(result) local gems = M.parse_gem_list_output(result.stdout) @@ -147,13 +177,4 @@ function M.get_installed_primary_package_version(receipt, install_dir) end) end ----@param install_dir string -function M.env(install_dir) - return { - GEM_HOME = install_dir, - GEM_PATH = install_dir, - PATH = process.extend_path { path.concat { install_dir, "bin" } }, - } -end - return M |
