aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/installers/std.lua
blob: 07e28d16df4b3f1927ef0430b3942d58fe639464 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
local path = require "nvim-lsp-installer.path"
local process = require "nvim-lsp-installer.process"
local platform = require "nvim-lsp-installer.platform"
local installers = require "nvim-lsp-installer.installers"
local shell = require "nvim-lsp-installer.installers.shell"

local M = {}

function M.download_file(url, out_file)
    return installers.when {
        unix = function(server, callback, context)
            process.attempt {
                jobs = {
                    process.lazy_spawn("wget", {
                        args = { "-nv", "-O", out_file, url },
                        cwd = server.root_dir,
                        stdio_sink = context.stdio_sink,
                    }),
                    process.lazy_spawn("curl", {
                        args = { "-fsSL", "-o", out_file, url },
                        cwd = server.root_dir,
                        stdio_sink = context.stdio_sink,
                    }),
                },
                on_finish = callback,
            }
        end,
        win = shell.powershell(("iwr -Uri %q -OutFile %q"):format(url, out_file)),
    }
end

function M.unzip(file, dest)
    return installers.pipe {
        installers.when {
            unix = function(server, callback, context)
                process.spawn("unzip", {
                    args = { "-d", dest, file },
                    cwd = server.root_dir,
                    stdio_sink = context.stdio_sink,
                }, callback)
            end,
            win = shell.powershell(("Expand-Archive -Path %q -DestinationPath %q"):format(file, dest)),
        },
        installers.always_succeed(M.delete_file(file)),
    }
end

function M.unzip_remote(url, dest)
    return installers.pipe {
        M.download_file(url, "archive.zip"),
        M.unzip("archive.zip", dest or "."),
    }
end

function M.untar(file, opts)
    local default_opts = {
        strip_components = 0,
    }
    opts = vim.tbl_deep_extend("force", default_opts, opts or {})
    return installers.pipe {
        function(server, callback, context)
            process.spawn("tar", {
                args = { "-xvf", file, "--strip-components", opts.strip_components },
                cwd = server.root_dir,
                stdio_sink = context.stdio_sink,
            }, callback)
        end,
        installers.always_succeed(M.delete_file(file)),
    }
end

function M.untarxz_remote(url, tar_opts)
    return installers.pipe {
        M.download_file(url, "archive.tar.xz"),
        M.untar("archive.tar.xz", tar_opts),
    }
end

function M.untargz_remote(url, tar_opts)
    return installers.pipe {
        M.download_file(url, "archive.tar.gz"),
        M.untar("archive.tar.gz", tar_opts),
    }
end

function M.gunzip(file)
    return function(server, callback, context)
        process.spawn("gzip", {
            args = { "-d", file },
            cwd = server.root_dir,
            stdio_sink = context.stdio_sink,
        }, callback)
    end
end

function M.gunzip_remote(url, out_file)
    local archive = ("%s.gz"):format(out_file or "archive")
    return installers.pipe {
        M.download_file(url, archive),
        M.gunzip(archive),
        installers.always_succeed(M.delete_file(archive)),
    }
end

function M.delete_file(file)
    return installers.when {
        unix = function(server, callback, context)
            process.spawn("rm", {
                args = { "-f", file },
                cwd = server.root_dir,
                stdio_sink = context.stdio_sink,
            }, callback)
        end,
        win = shell.powershell(("rm %q"):format(file)),
    }
end

function M.git_clone(repo_url)
    return function(server, callback, context)
        local c = process.chain {
            cwd = server.root_dir,
            stdio_sink = context.stdio_sink,
        }

        c.run("git", { "clone", "--depth", "1", repo_url, "." })

        if context.requested_server_version then
            c.run("git", { "fetch", "--depth", "1", "origin", context.requested_server_version })
            c.run("git", { "checkout", "FETCH_HEAD" })
        end

        c.spawn(callback)
    end
end

function M.gradlew(opts)
    return function(server, callback, context)
        process.spawn(path.concat { server.root_dir, platform.is_win and "gradlew.bat" or "gradlew" }, {
            args = opts.args,
            cwd = server.root_dir,
            stdio_sink = context.stdio_sink,
        }, callback)
    end
end

function M.ensure_executables(executables)
    return vim.schedule_wrap(function(_, callback, context)
        for i = 1, #executables do
            local entry = executables[i]
            local executable = entry[1]
            local error_msg = entry[2]
            if vim.fn.executable(executable) ~= 1 then
                context.stdio_sink.stderr(error_msg .. "\n")
                callback(false)
                return
            end
        end
        callback(true)
    end)
end

function M.chmod(flags, files)
    return installers.on {
        unix = function(server, callback, context)
            process.spawn("chmod", {
                args = vim.list_extend({ flags }, files),
                cwd = server.root_dir,
                stdio_sink = context.stdio_sink,
            }, callback)
        end,
    }
end

return M