aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2021-12-19 07:47:41 +0100
committerGitHub <noreply@github.com>2021-12-19 07:47:41 +0100
commit70a64ed1774fe3db10aa747f08657b341227a4e5 (patch)
tree681255ac3b1bfdfd65aff942437582876a5e37d3 /lua
parentfix(health): ignore the patch number in capturing go version (#343) (diff)
downloadmason-70a64ed1774fe3db10aa747f08657b341227a4e5.tar
mason-70a64ed1774fe3db10aa747f08657b341227a4e5.tar.gz
mason-70a64ed1774fe3db10aa747f08657b341227a4e5.tar.bz2
mason-70a64ed1774fe3db10aa747f08657b341227a4e5.tar.lz
mason-70a64ed1774fe3db10aa747f08657b341227a4e5.tar.xz
mason-70a64ed1774fe3db10aa747f08657b341227a4e5.tar.zst
mason-70a64ed1774fe3db10aa747f08657b341227a4e5.zip
feat(health): relax some checks, also improve bourne shell version check (#344)
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-lsp-installer/health/init.lua95
1 files changed, 63 insertions, 32 deletions
diff --git a/lua/nvim-lsp-installer/health/init.lua b/lua/nvim-lsp-installer/health/init.lua
index 6af53e77..6a09da15 100644
--- a/lua/nvim-lsp-installer/health/init.lua
+++ b/lua/nvim-lsp-installer/health/init.lua
@@ -18,6 +18,7 @@ local M = {}
---@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 = {}
@@ -28,13 +29,39 @@ function HealthCheck.new(props)
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",
+ ["version-mismatch"] = "report_warn",
+ ["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 == "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)
return function()
local stdio = process.in_memory_sink()
- process.spawn(opts.cmd, {
+ local _, stdio = process.spawn(opts.cmd, {
args = opts.args,
stdio_sink = stdio.sink,
}, function(success)
@@ -44,14 +71,15 @@ local function mk_healthcheck(callback)
"\n"
)[1]
- if opts.check then
- local version_check = opts.check(version)
+ if opts.version_check then
+ local version_check = opts.version_check(version)
if version_check then
callback(HealthCheck.new {
result = "version-mismatch",
reason = version_check,
version = version,
name = opts.name,
+ relaxed = opts.relaxed,
})
return
end
@@ -61,15 +89,23 @@ local function mk_healthcheck(callback)
result = "success",
version = version,
name = opts.name,
+ relaxed = opts.relaxed,
})
else
callback(HealthCheck.new {
result = "not-available",
version = nil,
name = opts.name,
+ relaxed = opts.relaxed,
})
end
end)
+
+ if stdio then
+ -- Immediately close stdin to avoid leaving the process waiting for input.
+ local stdin = stdio[1]
+ stdin:close()
+ end
end
end
end
@@ -82,20 +118,7 @@ function M.check()
---@param healthcheck HealthCheck
function(healthcheck)
completed = completed + 1
- if healthcheck.result == "success" then
- -- We report on info level because we don't verify version compatibility yet
- health.report_info(("**%s**: `%s`"):format(healthcheck.name, healthcheck.version))
- elseif healthcheck.result == "version-mismatch" then
- health.report_warn(
- ("**%s**: unsupported version `%s`. %s"):format(
- healthcheck.name,
- healthcheck.version,
- healthcheck.reason
- )
- )
- elseif healthcheck.result == "not-available" then
- health.report_error(("**%s**: not available"):format(healthcheck.name))
- end
+ health[healthcheck:get_health_report_level()](tostring(healthcheck))
end
))
@@ -104,7 +127,8 @@ function M.check()
cmd = "go",
args = { "version" },
name = "Go",
- check = function(version)
+ 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
@@ -113,15 +137,15 @@ function M.check()
end
end,
},
- check { cmd = "ruby", args = { "--version" }, name = "Ruby" },
- check { cmd = gem.gem_cmd, args = { "--version" }, name = "RubyGem" },
- check { cmd = composer.composer_cmd, args = { "--version" }, name = "Composer" },
- check { cmd = "php", args = { "--version" }, name = "PHP" },
+ check { cmd = "ruby", args = { "--version" }, name = "Ruby", relaxed = true },
+ check { cmd = gem.gem_cmd, args = { "--version" }, name = "RubyGem", relaxed = true },
+ check { cmd = composer.composer_cmd, args = { "--version" }, name = "Composer", relaxed = true },
+ check { cmd = "php", args = { "--version" }, name = "PHP", relaxed = true },
check {
cmd = npm.npm_command,
args = { "--version" },
name = "npm",
- check = function(version)
+ 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
@@ -134,7 +158,7 @@ function M.check()
cmd = "node",
args = { "--version" },
name = "node",
- check = function(version)
+ 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
@@ -142,13 +166,20 @@ function M.check()
end
end,
},
- when(platform.is_win, check { cmd = "python", use_stderr = true, args = { "--version" }, name = "python" }),
- when(platform.is_win, check { cmd = "python", args = { "-m", "pip", "--version" }, name = "pip" }),
- check { cmd = "python3", args = { "--version" }, name = "python3" },
- check { cmd = "python3", args = { "-m", "pip", "--version" }, name = "pip3" },
- check { cmd = "javac", args = { "-version" }, name = "java" },
+ 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 = "java", relaxed = true },
check { cmd = "wget", args = { "--version" }, name = "wget" },
- check { cmd = "curl", args = { "--version" }, name = "curl" },
+ -- 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" },
@@ -158,10 +189,10 @@ function M.check()
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" }
+ 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", args = { "--version" }, name = "sh" })
+ 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
)