aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/installer/managers/composer.lua
blob: a4a94270833b6c9efcc3cfcd01c37c5a3420d1ac (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
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
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 package string
---@param version string
---@nodiscard
function M.install(package, version)
    log.fmt_debug("composer: install %s %s", package, version)
    local ctx = installer.context()
    ctx.stdio_sink.stdout(("Installing composer package %s@%s…\n"):format(package, version))
    return Result.try(function(try)
        try(ctx.spawn.composer {
            "init",
            "--no-interaction",
            "--stability=stable",
        })
        try(ctx.spawn.composer {
            "require",
            ("%s:%s"):format(package, version),
        })
    end)
end

---@param executable string
function M.bin_path(executable)
    return Result.pcall(platform.when, {
        unix = function()
            return path.concat { "vendor", "bin", executable }
        end,
        win = function()
            return path.concat { "vendor", "bin", ("%s.bat"):format(executable) }
        end,
    })
end

return M