aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/shell.lua
blob: 34cfc524f9539ea355801343da1975809c743333 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
local installers = require "nvim-lsp-installer.installers"
local process = require "nvim-lsp-installer.process"

local M = {}

---@param opts {shell: string, cmd: string[], env: table|nil}
local function shell(opts)
    ---@type ServerInstallerFunction
    return function(_, callback, context)
        local _, stdio = process.spawn(opts.shell, {
            args = opts.args,
            cwd = context.install_dir,
            stdio_sink = context.stdio_sink,
            env = process.graft_env(opts.env or {}, {
                "PSMODULEPATH", -- https://github.com/williamboman/nvim-lsp-installer/issues/271
            }),
        }, callback)

        if stdio then
            local stdin = stdio[1]

            stdin:write(opts.cmd)
            stdin:write "\n"
            stdin:close()
        end
    end
end

---@param raw_script string @The bash script to execute as-is.
---@param opts {prefix: string, env: table}
function M.bash(raw_script, opts)
    local default_opts = {
        prefix = "set -euo pipefail;",
        env = {},
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})

    return shell {
        shell = "bash",
        cmd = (opts.prefix or "") .. raw_script,
        env = opts.env,
    }
end

---@param raw_script string @The sh script to execute as-is.
---@param opts {prefix: string, env: table}
function M.sh(raw_script, opts)
    local default_opts = {
        prefix = "set -eu;",
        env = {},
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})

    return shell {
        shell = "sh",
        cmd = (opts.prefix or "") .. raw_script,
        env = opts.env,
    }
end

---@param raw_script string @The cmd.exe script to execute as-is.
---@param opts {env: table}
function M.cmd(raw_script, opts)
    local default_opts = {
        env = {},
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})

    return shell {
        shell = "cmd.exe",
        cmd = raw_script,
        env = opts.env,
    }
end

---@param raw_script string @The powershell script to execute as-is.
---@param opts {prefix: string, env: table}
function M.powershell(raw_script, opts)
    local default_opts = {
        env = {},
        -- YIKES https://stackoverflow.com/a/63301751
        prefix = "$ProgressPreference = 'SilentlyContinue';",
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})

    return shell {
        shell = "powershell.exe",
        args = { "-NoProfile" },
        cmd = (opts.prefix or "") .. raw_script,
        env = opts.env,
    }
end

---@param raw_script string @A script that is compatible with bash and cmd.exe.
---@param opts {env: table}
function M.polyshell(raw_script, opts)
    local default_opts = {
        env = {},
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})

    return installers.when {
        unix = M.bash(raw_script, { env = opts.env }),
        win = M.cmd(raw_script, { env = opts.env }),
    }
end

return M