aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/shell.lua
blob: 98f12103bb50de06ce22802c8544ab1dad634e2e (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
local installers = require "nvim-lsp-installer.installers"

local M = {}

local function termopen(opts)
    return function(server, callback)
        local jobstart_opts = {
            cwd = server._root_dir,
            on_exit = function(_, exit_code)
                if exit_code ~= 0 then
                    callback(false, ("Exit code %d"):format(exit_code))
                else
                    callback(true, nil)
                end
            end,
        }

        if type(opts.env) == "table" and vim.tbl_count(opts.env) > 0 then
            -- passing an empty Lua table causes E475, for whatever reason
            jobstart_opts.env = opts.env
        end

        local orig_shell = vim.o.shell
        vim.o.shell = opts.shell
        vim.cmd [[new]]
        vim.fn.termopen(opts.cmd, jobstart_opts)
        vim.o.shell = orig_shell
        vim.cmd [[startinsert]] -- so that we tail the term log nicely ¯\_(ツ)_/¯
    end
end

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 termopen {
        shell = "/bin/bash",
        cmd = (opts.prefix or "") .. raw_script,
        env = opts.env,
    }
end

function M.remote_bash(url, opts)
    return M.bash(("wget -nv -O - %q | bash"):format(url), opts)
end

function M.cmd(raw_script, opts)
    local default_opts = {
        env = {},
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})

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

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