aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/installer/managers/gem.lua
blob: e8723d7e52d0fbd914196e863e419cfa21ddb6ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
local Result = require "mason-core.result"
local installer = require "mason-core.installer"
local log = require "mason-core.log"
local path = require "mason-core.path"
local platform = require "mason-core.platform"

local M = {}

---@async
---@param pkg string
---@param version string
---@param opts? { extra_packages?: string[] }
---@nodiscard
function M.install(pkg, version, opts)
    opts = opts or {}
    log.fmt_debug("gem: install %s %s %s", pkg, version, opts)
    local ctx = installer.context()
    ctx.stdio_sink.stdout(("Installing gem %s@%s…\n"):format(pkg, version))
    return ctx.spawn.gem {
        "install",
        "--no-user-install",
        "--no-format-executable",
        "--install-dir=.",
        "--bindir=bin",
        "--no-document",
        ("%s:%s"):format(pkg, version),
        opts.extra_packages or vim.NIL,
        env = {
            GEM_HOME = ctx.cwd:get(),
        },
    }
end

---@async
---@param bin string
---@nodiscard
function M.create_bin_wrapper(bin)
    local ctx = installer.context()

    local bin_path = platform.when {
        unix = function()
            return path.concat { "bin", bin }
        end,
        win = function()
            return path.concat { "bin", ("%s.bat"):format(bin) }
        end,
    }

    if not ctx.fs:file_exists(bin_path) then
        return Result.failure(("Cannot link Gem executable %q because it doesn't exist."):format(bin))
    end

    return Result.pcall(
        ctx.write_shell_exec_wrapper,
        ctx,
        bin,
        path.concat { ctx:get_install_path(), bin_path },
        {
            GEM_PATH = platform.when {
                unix = function()
                    return ("%s:$GEM_PATH"):format(ctx:get_install_path())
                end,
                win = function()
                    return ("%s;%%GEM_PATH%%"):format(ctx:get_install_path())
                end,
            },
        }
    )
end

return M