From 116a825929fa3066f32afc77f7459630419bc347 Mon Sep 17 00:00:00 2001 From: William Boman Date: Sun, 31 Jul 2022 00:45:17 +0200 Subject: fix(gem): write shim executables that enhance GEM_PATH appropriately (#202) --- lua/mason-core/installer/context.lua | 35 +++++++++++-------------- lua/mason-core/managers/gem/init.lua | 51 +++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 34 deletions(-) (limited to 'lua') 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? ---@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, +