aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/composer.lua
blob: 26e4e7bea195532505ac515284cfaa3e41956d09 (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
72
73
74
75
76
77
78
79
80
81
82
83
local installers = require "nvim-lsp-installer.installers"
local std = require "nvim-lsp-installer.installers.std"
local platform = require "nvim-lsp-installer.platform"
local process = require "nvim-lsp-installer.process"
local fs = require "nvim-lsp-installer.fs"
local path = require "nvim-lsp-installer.path"
local Data = require "nvim-lsp-installer.data"

local list_copy = Data.list_copy

local M = {}

M.composer_cmd = platform.is_win and "composer.bat" or "composer"

---@param installer ServerInstallerFunction
local function ensure_composer(installer)
    return installers.pipe {
        std.ensure_executables {
            { "php", "php was not found in path. Refer to https://www.php.net/." },
            { M.composer_cmd, "composer was not found in path. Refer to https://getcomposer.org/download/." },
        },
        installer,
    }
end

---@param packages string[] The composer packages to install. The first item in this list will be the recipient of the server version, should the user request a specific one.
function M.require(packages)
    return ensure_composer(
        ---@type ServerInstallerFunction
        function(_, callback, ctx)
            local pkgs = list_copy(packages)
            local c = process.chain {
                cwd = ctx.install_dir,
                stdio_sink = ctx.stdio_sink,
            }

            ctx.receipt:with_primary_source(ctx.receipt.composer(pkgs[1]))
            for i = 2, #pkgs do
                ctx.receipt:with_secondary_source(ctx.receipt.composer(pkgs[i]))
            end

            if not (fs.file_exists(path.concat { ctx.install_dir, "composer.json" })) then
                c.run(M.composer_cmd, { "init", "--no-interaction", "--stability=stable" })
            end

            if ctx.requested_server_version then
                -- The "head" package is the recipient for the requested version. It's.. by design... don't ask.
                pkgs[1] = ("%s:%s"):format(pkgs[1], ctx.requested_server_version)
            end

            c.run(M.composer_cmd, vim.list_extend({ "require" }, pkgs))
            c.spawn(callback)
        end
    )
end

function M.install()
    return ensure_composer(
        ---@type ServerInstallerFunction
        function(_, callback, ctx)
            process.spawn(M.composer_cmd, {
                args = {
                    "install",
                    "--no-interaction",
                    "--no-dev",
                    "--optimize-autoloader",
                    "--classmap-authoritative",
                },
                cwd = ctx.install_dir,
                stdio_sink = ctx.stdio_sink,
            }, callback)
        end
    )
end

---@param root_dir string The directory to resolve the executable from.
function M.env(root_dir)
    return {
        PATH = process.extend_path { path.concat { root_dir, "vendor", "bin" } },
    }
end

return M