Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(healthcheck): remove duplicate binary checks #19

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 50 additions & 66 deletions lua/pymple/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,32 @@ local warn = health.warn or health.report_warn
local error = health.error or health.report_error
local info = health.info or health.report_info
local utils = require("pymple.utils")
local log = require("pymple.log")

local M = {}
-- local is_win = vim.api.nvim_call_function("has", { "win32" }) == 1

---@class RequiredBinary
---@field name string
---@field url string
---@field optional boolean
---@field binaries string[]
---@field min_version string | nil
---@field max_version string | nil

---@type RequiredBinary[]
local required_binaries = {
{
functionality = "update_imports",
package = {
{
name = "gg",
url = "[alexpasmantier/grip-grab](https://github.com/alexpasmantier/grip-grab)",
optional = false,
binaries = { "gg" },
min_version = "0.2.22",
},
},
},
{
functionality = "resolve_imports",
package = {
{
name = "gg",
url = "[alexpasmantier/grip-grab](https://github.com/alexpasmantier/grip-grab)",
optional = false,
binaries = { "gg" },
min_version = "0.2.22",
},
},
name = "gg",
url = "[alexpasmantier/grip-grab](https://github.com/alexpasmantier/grip-grab)",
optional = false,
binaries = { "gg" },
min_version = "0.2.23",
},
{
functionality = "update_imports",
package = {
{
name = "sed",
url = "[https://www.gnu.org/software/sed](https://www.gnu.org/software/sed/manual/sed.html)",
optional = false,
binaries = { linux = "sed", darwin = "gsed" },
},
},
name = "sed",
url = "[https://www.gnu.org/software/sed](https://www.gnu.org/software/sed/manual/sed.html)",
optional = false,
binaries = { linux = "sed", darwin = "gsed" },
},
}

Expand Down Expand Up @@ -138,44 +124,42 @@ M.check = function()
start("Checking external dependencies")

for _, req_bin in pairs(required_binaries) do
for _, package in ipairs(req_bin.package) do
local installed, version = check_binary_installed(package)
if not installed then
local err_msg = ("%s: not found."):format(package.name)
if package.optional then
warn(
("%s %s"):format(
err_msg,
("Install %s for extended capabilities"):format(package.url)
)
local installed, version = check_binary_installed(req_bin)
if not installed then
local err_msg = ("%s: not found."):format(req_bin.name)
if req_bin.optional then
warn(
("%s %s"):format(
err_msg,
("Install %s for extended capabilities"):format(req_bin.url)
)
else
error(
("%s %s"):format(
err_msg,
("Functionality `%s` will not work without %s installed."):format(
req_bin.functionality,
package.url
)
)
)
end
)
else
version = version or "(unknown version)"
if
(package.min_version or package.max_version)
and version ~= "(unknown version)"
and not version_satisfies_constraint(version, package.min_version)
then
error(
("%s: installed version %s is too old."):format(
package.name,
version
error(
("%s %s"):format(
err_msg,
("pymple.nvim will not work without %s installed."):format(
req_bin.url
)
)
else
ok(("%s: found %s"):format(package.name, version))
end
)
end
else
version = version or "(unknown version)"
if
(req_bin.min_version or req_bin.max_version)
and version ~= "(unknown version)"
and not version_satisfies_constraint(
version,
req_bin.min_version,
req_bin.max_version
)
then
error(
("%s: installed version %s is too old."):format(req_bin.name, version)
)
else
ok(("%s: found %s"):format(req_bin.name, version))
end
end
end
Expand Down
62 changes: 30 additions & 32 deletions plugin/pymple.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
local utils = require("pymple.utils")
local health = require("pymple.health")

for _, binaries in ipairs(health.required_binaries) do
for _, p in ipairs(binaries.package) do
local installed, version = health.check_binary_installed(p)
for _, required_binary in ipairs(health.required_binaries) do
local installed, version = health.check_binary_installed(required_binary)

if not installed then
utils.print_err(
"Binary "
.. p.name
.. " is not installed. Please install it manually to use pymple.nvim "
.. "or run `:PympleBuild`. For more information, see "
.. p.url
)
end
if not installed then
utils.print_err(
"Binary "
.. required_binary.name
.. " is not installed. Please install it manually to use pymple.nvim "
.. "or run `:PympleBuild`. For more information, see "
.. required_binary.url
)
end

if
installed
and version
and version ~= "(unknown version)"
and (p.min_version or p.max_version)
and not health.version_satisfies_constraint(
if
installed
and version
and version ~= "(unknown version)"
and (required_binary.min_version or required_binary.max_version)
and not health.version_satisfies_constraint(
version,
required_binary.min_version,
required_binary.max_version
)
then
utils.print_err(
string.format(
"Binary %s (%s) is installed, but the version is either too old or too recent (min: %s, max: %s). Please update it to use pymple.nvim. For more information, see %s",
required_binary.name,
version,
p.min_version,
p.max_version
required_binary.min_version,
required_binary.max_version,
required_binary.url
)
then
utils.print_err(
string.format(
"Binary %s (%s) is installed, but the version is either too old or too recent (min: %s, max: %s). Please update it to use pymple.nvim. For more information, see %s",
p.name,
version,
p.min_version,
p.max_version,
p.url
)
)
end
)
end
end

Expand Down