aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/health/init.lua
blob: 909c91cb529af6d01c402ed938bb75da19545b5c (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
local health = vim.health or require "health"
local a = require "nvim-lsp-installer.core.async"
local platform = require "nvim-lsp-installer.core.platform"
local github_client = require "nvim-lsp-installer.core.managers.github.client"
local _ = require "nvim-lsp-installer.core.functional"
local spawn = require "nvim-lsp-installer.core.spawn"

local when = _.when

local M = {}

---@alias HealthCheckResult
---| '"success"'
---| '"version-mismatch"'
---| '"parse-error"'
---| '"not-available"'

---@class HealthCheck
---@field public result HealthCheckResult
---@field public version string|nil
---@field public relaxed boolean|nil
---@field public reason string|nil
---@field public name string
local HealthCheck = {}
HealthCheck.__index = HealthCheck

function HealthCheck.new(props)
    local self = setmetatable(props, HealthCheck)
    return self
end

function HealthCheck:get_version()
    if self.result == "success" and not self.version or self.version == "" then
        -- Some checks (bourne shell for instance) don't produce any output, so we default to just "Ok"
        return "Ok"
    end
    return self.version
end

function HealthCheck:get_health_report_level()
    return ({
        ["success"] = "report_ok",
        ["parse-error"] = "report_warn",
        ["version-mismatch"] = "report_error",
        ["not-available"] = self.relaxed and "report_warn" or "report_error",
    })[self.result]
end

function HealthCheck:__tostring()
    if self.result == "success" then
        return ("**%s**: `%s`"):format(self.name, self:get_version())
    elseif self.result == "version-mismatch" then
        return ("**%s**: unsupported version `%s`. %s"):format(self.name, self:get_version(), self.reason)
    elseif self.result == "parse-error" then
        return ("**%s**: failed to parse version"):format(self.name)
    elseif self.result == "not-available" then
        return ("**%s**: not available"):format(self.name)
    end
end

---@param callback fun(result: HealthCheck)
local function mk_healthcheck(callback)
    ---@param opts {cmd:string, args:string[], name: string, use_stderr:boolean}
    return function(opts)
        local parse_version = _.compose(
            _.head,
            _.split "\n",
            _.if_else(_.always(opts.use_stderr), _.prop "stderr", _.prop "stdout")
        )

        ---@async
        return function()
            local healthcheck_result = spawn[opts.cmd]({
                opts.args,
                on_spawn = function(_, stdio)
                    local stdin = stdio[1]
                    stdin:close() -- some processes (`sh` for example) will endlessly read from stdin, so we close it immediately
                end,
            })
                :map(parse_version)
                :map(function(version)
                    if opts.version_check then
                        local ok, version_check = pcall(opts.version_check, version)
                        if ok and version_check then
                            return HealthCheck.new {
                                result = "version-mismatch",
                                reason = version_check,
                                version = version,
                                name = opts.name,
                                relaxed = opts.relaxed,
                            }
                        elseif not ok then
                            return HealthCheck.new {
                                result = "parse-error",
                                version = "N/A",
                                name = opts.name,
                                relaxed = opts.relaxed,
                            }
                        end
                    end

                    return HealthCheck.new {
                        result = "success",
                        version = version,
                        name = opts.name,
                        relaxed = opts.relaxed,
                    }
                end)
                :get_or_else(HealthCheck.new {
                    result = "not-available",
                    version = nil,
                    name = opts.name,
                    relaxed = opts.relaxed,
                })

            callback(healthcheck_result)
        end
    end
end

function M.check()
    health.report_start "nvim-lsp-installer report"
    if vim.fn.has "nvim-0.7.0" == 1 then
        health.report_ok "neovim version >= 0.7.0"
    else
        health.report_error "neovim version < 0.7.0"
    end

    local completed = 0

    local check = mk_healthcheck(vim.schedule_wrap(
        ---@param healthcheck HealthCheck
        function(healthcheck)
            completed = completed + 1
            health[healthcheck:get_health_report_level()](tostring(healthcheck))
        end
    ))

    local checks = _.list_not_nil(
        check {
            cmd = "go",
            args = { "version" },
            name = "Go",
            relaxed = true,
            version_check = function(version)
                -- Parses output such as "go version go1.17.3 darwin/arm64" into major, minor, patch components
                local _, _, major, minor = version:find "go(%d+)%.(%d+)"
                -- Due to https://go.dev/doc/go-get-install-deprecation
                if not (tonumber(major) >= 1 and tonumber(minor) >= 17) then
                    return "Go version must be >= 1.17."
                end
            end,
        },
        check { cmd = "cargo", args = { "--version" }, name = "cargo", relaxed = true },
        check {
            cmd = "luarocks",
            args = { "--version" },
            name = "luarocks",
            relaxed = true,
            version_check = function(version)
                local _, _, major = version:find "(%d+)%.(%d)%.(%d)"
                if not (tonumber(major) >= 3) then
                    -- Because of usage of "--dev" flag
                    return "Luarocks version must be >= 3.0.0."
                end
            end,
        },
        check { cmd = "ruby", args = { "--version" }, name = "Ruby", relaxed = true },
        check { cmd = "gem", args = { "--version" }, name = "RubyGem", relaxed = true },
        check { cmd = "composer", args = { "--version" }, name = "Composer", relaxed = true },
        check { cmd = "php", args = { "--version" }, name = "PHP", relaxed = true },
        check {
            cmd = "npm",
            args = { "--version" },
            name = "npm",
            version_check = function(version)
                -- Parses output such as "8.1.2" into major, minor, patch components
                local _, _, major = version:find "(%d+)%.(%d+)%.(%d+)"
                -- Based off of general observations of feature parity
                if tonumber(major) < 6 then
                    return "npm version must be >= 6"
                end
            end,
        },
        check {
            cmd = "node",
            args = { "--version" },
            name = "node",
            version_check = function(version)
                -- Parses output such as "v16.3.1" into major, minor, patch
                local _, _, major = version:find "v(%d+)%.(%d+)%.(%d+)"
                if tonumber(major) < 14 then
                    return "Node version must be >= 14"
                end
            end,
        },
        when(
            platform.is_win,
            check { cmd = "python", use_stderr = true, args = { "--version" }, name = "python", relaxed = true }
        ),
        when(
            platform.is_win,
            check { cmd = "python", args = { "-m", "pip", "--version" }, name = "pip", relaxed = true }
        ),
        check { cmd = "python3", args = { "--version" }, name = "python3", relaxed = true },
        check { cmd = "python3", args = { "-m", "pip", "--version" }, name = "pip3", relaxed = true },
        check { cmd = "javac", args = { "-version" }, name = "javac", relaxed = true },
        check { cmd = "java", args = { "-version" }, name = "java", use_stderr = true, relaxed = true },
        when(
            vim.env.JAVA_HOME,
            check {
                cmd = ("%s/bin/java"):format(vim.env.JAVA_HOME),
                args = { "-version" },
                name = "JAVA_HOME",
                use_stderr = true,
                relaxed = true,
            }
        ),
        check { cmd = "julia", args = { "--version" }, name = "julia", relaxed = true },
        check { cmd = "wget", args = { "--version" }, name = "wget" },
        -- wget is used interchangeably with curl, but with higher priority, so we mark curl as relaxed
        check { cmd = "curl", args = { "--version" }, name = "curl", relaxed = true },
        check {
            cmd = "gzip",
            args = { "--version" },
            name = "gzip",
            use_stderr = platform.is_mac, -- Apple gzip prints version string to stderr
        },
        check { cmd = "tar", args = { "--version" }, name = "tar" },
        when(
            vim.g.python3_host_prog,
            check { cmd = vim.g.python3_host_prog, args = { "--version" }, name = "python3_host_prog", relaxed = true }
        ),
        when(platform.is_unix, check { cmd = "bash", args = { "--version" }, name = "bash" }),
        when(platform.is_unix, check { cmd = "sh", name = "sh" })
        -- when(platform.is_win, check { cmd = "powershell.exe", args = { "-Version" }, name = "PowerShell" }), -- TODO fix me
        -- when(platform.is_win, check { cmd = "cmd.exe", args = { "-Version" }, name = "cmd" }) -- TODO fix me
    )

    a.run_blocking(function()
        for _, c in ipairs(checks) do
            c()
        end

        github_client.fetch_rate_limit()
            :map(
                ---@param rate_limit GitHubRateLimitResponse
                function(rate_limit)
                    if vim.in_fast_event() then
                        a.scheduler()
                    end
                    local remaining = rate_limit.resources.core.remaining
                    local used = rate_limit.resources.core.used
                    local limit = rate_limit.resources.core.limit
                    local reset = rate_limit.resources.core.reset
                    local diagnostics = ("Used: %d. Remaining: %d. Limit: %d. Reset: %s."):format(
                        used,
                        remaining,
                        limit,
                        vim.fn.strftime("%c", reset)
                    )
                    if remaining <= 0 then
                        health.report_error(("GitHub API rate limit exceeded. %s"):format(diagnostics))
                    else
                        health.report_ok(("GitHub API rate limit. %s"):format(diagnostics))
                    end
                end
            )
            :on_failure(function()
                if vim.in_fast_event() then
                    a.scheduler()
                end
                health.report_warn "Failed to check GitHub API rate limit status."
            end)
    end)
end

return M