2024-06-18 20:54:54 +01:00
|
|
|
--# selene:allow(incorrect_standard_library_use)
|
2024-06-25 19:53:42 +01:00
|
|
|
local Community = require("lazy.community")
|
|
|
|
|
2024-06-24 17:02:54 +01:00
|
|
|
local Config = require("lazy.core.config")
|
2024-06-25 12:23:25 +01:00
|
|
|
local Health = require("lazy.health")
|
2024-06-24 17:02:54 +01:00
|
|
|
local Util = require("lazy.util")
|
2024-06-18 20:54:54 +01:00
|
|
|
|
2024-06-25 12:23:25 +01:00
|
|
|
---@class RockSpec
|
|
|
|
---@field rockspec_format string
|
|
|
|
---@field package string
|
|
|
|
---@field version string
|
|
|
|
---@field dependencies string[]
|
2024-06-26 07:50:06 +01:00
|
|
|
---@field build? {type?: string, modules?: any[]}
|
2024-06-25 16:41:14 +01:00
|
|
|
---@field source? {url?: string}
|
2024-06-25 12:23:25 +01:00
|
|
|
|
|
|
|
---@class RockManifest
|
2024-06-25 16:41:14 +01:00
|
|
|
---@field repository table<string, table<string,any>>
|
2024-06-25 12:23:25 +01:00
|
|
|
|
2024-06-18 20:54:54 +01:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.skip = { "lua" }
|
2024-06-24 20:57:34 +01:00
|
|
|
M.rewrites = {
|
|
|
|
["plenary.nvim"] = { "nvim-lua/plenary.nvim", lazy = true },
|
|
|
|
}
|
2024-06-25 19:53:42 +01:00
|
|
|
|
2024-06-25 12:23:25 +01:00
|
|
|
M.python = { "python3", "python" }
|
|
|
|
|
|
|
|
---@class HereRocks
|
|
|
|
M.hererocks = {}
|
|
|
|
|
|
|
|
---@param task LazyTask
|
|
|
|
function M.hererocks.build(task)
|
|
|
|
local root = Config.options.rocks.root .. "/hererocks"
|
|
|
|
|
|
|
|
---@param p string
|
|
|
|
local python = vim.tbl_filter(function(p)
|
|
|
|
return vim.fn.executable(p) == 1
|
|
|
|
end, M.python)[1]
|
|
|
|
|
|
|
|
task:spawn(python, {
|
|
|
|
args = {
|
|
|
|
"hererocks.py",
|
|
|
|
"--verbose",
|
|
|
|
"-l",
|
|
|
|
"5.1",
|
|
|
|
"-r",
|
|
|
|
"latest",
|
|
|
|
root,
|
|
|
|
},
|
|
|
|
cwd = task.plugin.dir,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param bin string
|
|
|
|
function M.hererocks.bin(bin)
|
|
|
|
local hererocks = Config.options.rocks.root .. "/hererocks/bin"
|
|
|
|
return Util.norm(hererocks .. "/" .. bin)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check if hererocks is building
|
|
|
|
---@return boolean?
|
|
|
|
function M.hererocks.building()
|
|
|
|
return vim.tbl_get(Config.plugins.hererocks or {}, "_", "build")
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? LazyHealth
|
|
|
|
function M.check(opts)
|
|
|
|
opts = vim.tbl_extend("force", {
|
|
|
|
error = Util.error,
|
|
|
|
warn = Util.warn,
|
|
|
|
ok = function() end,
|
|
|
|
}, opts or {})
|
|
|
|
|
|
|
|
local ok = false
|
2024-06-29 10:44:31 +01:00
|
|
|
if Config.hererocks() then
|
2024-06-25 12:23:25 +01:00
|
|
|
if M.hererocks.building() then
|
|
|
|
ok = true
|
|
|
|
else
|
|
|
|
ok = Health.have(M.python, opts)
|
|
|
|
ok = Health.have(M.hererocks.bin("luarocks")) and ok
|
2024-07-08 06:45:43 +01:00
|
|
|
Health.have(
|
2024-06-25 12:23:25 +01:00
|
|
|
M.hererocks.bin("lua"),
|
|
|
|
vim.tbl_extend("force", opts, {
|
|
|
|
version = "-v",
|
|
|
|
version_pattern = "5.1",
|
|
|
|
})
|
2024-07-08 06:45:43 +01:00
|
|
|
)
|
2024-06-25 12:23:25 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
ok = Health.have("luarocks", opts)
|
2024-07-08 06:45:43 +01:00
|
|
|
Health.have(
|
|
|
|
{ "lua5.1", "lua", "lua-5.1" },
|
|
|
|
vim.tbl_extend("force", opts, {
|
|
|
|
version = "-v",
|
|
|
|
version_pattern = "5.1",
|
|
|
|
})
|
|
|
|
)
|
2024-06-25 12:23:25 +01:00
|
|
|
end
|
|
|
|
return ok
|
|
|
|
end
|
|
|
|
|
2024-06-27 10:33:11 +01:00
|
|
|
---@async
|
2024-06-25 12:23:25 +01:00
|
|
|
---@param task LazyTask
|
|
|
|
function M.build(task)
|
2024-07-08 06:45:43 +01:00
|
|
|
M.check({
|
|
|
|
error = function(msg)
|
|
|
|
task:error(msg:gsub("[{}]", "`"))
|
|
|
|
end,
|
|
|
|
warn = function(msg)
|
|
|
|
task:warn(msg)
|
|
|
|
end,
|
|
|
|
ok = function(msg) end,
|
|
|
|
})
|
|
|
|
|
|
|
|
if task:has_warnings() then
|
2024-06-26 17:31:31 +01:00
|
|
|
task:log({
|
2024-06-25 12:23:25 +01:00
|
|
|
"",
|
|
|
|
"This plugin requires `luarocks`. Try one of the following:",
|
|
|
|
" - fix your `luarocks` installation",
|
2024-06-29 10:44:31 +01:00
|
|
|
Config.hererocks() and " - disable *hererocks* with `opts.rocks.hererocks = false`"
|
2024-06-25 12:23:25 +01:00
|
|
|
or " - enable `hererocks` with `opts.rocks.hererocks = true`",
|
|
|
|
" - disable `luarocks` support completely with `opts.rocks.enabled = false`",
|
|
|
|
})
|
2024-07-08 06:45:43 +01:00
|
|
|
task:warn("\nWill try building anyway, but will likely fail...")
|
|
|
|
|
|
|
|
task:warn("\n" .. string.rep("-", 80) .. "\n")
|
|
|
|
|
|
|
|
task:set_level(vim.log.levels.WARN)
|
2024-06-25 12:23:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if task.plugin.name == "hererocks" then
|
|
|
|
return M.hererocks.build(task)
|
|
|
|
end
|
|
|
|
|
|
|
|
local env = {}
|
|
|
|
local luarocks = "luarocks"
|
2024-06-29 10:44:31 +01:00
|
|
|
if Config.hererocks() then
|
2024-06-25 12:23:25 +01:00
|
|
|
-- hererocks is still building, so skip for now
|
|
|
|
-- a new build will happen in the next round
|
|
|
|
if M.hererocks.building() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local sep = Util.is_win and ";" or ":"
|
|
|
|
local hererocks = Config.options.rocks.root .. "/hererocks/bin"
|
|
|
|
if Util.is_win then
|
|
|
|
hererocks = hererocks:gsub("/", "\\")
|
|
|
|
end
|
|
|
|
local path = vim.split(vim.env.PATH, sep)
|
|
|
|
table.insert(path, 1, hererocks)
|
|
|
|
env = {
|
|
|
|
PATH = table.concat(path, sep),
|
|
|
|
}
|
|
|
|
if Util.is_win then
|
|
|
|
luarocks = luarocks .. ".bat"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-25 19:53:42 +01:00
|
|
|
local pkg = task.plugin._.pkg
|
|
|
|
assert(pkg, "missing rockspec pkg for " .. task.plugin.name .. "\nThis shouldn't happen, please report.")
|
|
|
|
|
|
|
|
local rockspec = M.rockspec(task.plugin.dir .. "/" .. pkg.file) or {}
|
|
|
|
assert(
|
|
|
|
rockspec.package,
|
|
|
|
"missing rockspec package name for " .. task.plugin.name .. "\nThis shouldn't happen, please report."
|
|
|
|
)
|
|
|
|
|
2024-06-25 12:23:25 +01:00
|
|
|
local root = Config.options.rocks.root .. "/" .. task.plugin.name
|
2024-06-27 10:33:11 +01:00
|
|
|
local ok = task:spawn(luarocks, {
|
2024-06-25 12:23:25 +01:00
|
|
|
args = {
|
|
|
|
"--tree",
|
|
|
|
root,
|
|
|
|
"--server",
|
|
|
|
Config.options.rocks.server,
|
|
|
|
"--lua-version",
|
|
|
|
"5.1",
|
2024-06-25 19:53:42 +01:00
|
|
|
"install", -- use install so that we can make use of pre-built rocks
|
2024-06-25 12:23:25 +01:00
|
|
|
"--force-fast",
|
2024-06-25 19:53:42 +01:00
|
|
|
"--deps-mode",
|
|
|
|
"one",
|
|
|
|
rockspec.package,
|
2024-06-25 12:23:25 +01:00
|
|
|
},
|
|
|
|
cwd = task.plugin.dir,
|
|
|
|
env = env,
|
|
|
|
})
|
2024-06-27 10:33:11 +01:00
|
|
|
|
|
|
|
if ok then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-07-08 06:45:43 +01:00
|
|
|
task:warn("Failed installing " .. rockspec.package .. " with `luarocks`.")
|
|
|
|
task:warn("\n" .. string.rep("-", 80) .. "\n")
|
|
|
|
task:warn("Trying to build from source.")
|
2024-06-27 10:33:11 +01:00
|
|
|
|
|
|
|
-- install failed, so try building from source
|
|
|
|
task:set_level() -- reset level
|
2024-07-08 06:45:43 +01:00
|
|
|
ok = task:spawn(luarocks, {
|
2024-06-27 10:33:11 +01:00
|
|
|
args = {
|
|
|
|
"--tree",
|
|
|
|
root,
|
|
|
|
"--dev",
|
|
|
|
"--lua-version",
|
|
|
|
"5.1",
|
|
|
|
"make",
|
|
|
|
"--force-fast",
|
|
|
|
"--deps-mode",
|
|
|
|
"one",
|
|
|
|
},
|
|
|
|
cwd = task.plugin.dir,
|
|
|
|
env = env,
|
|
|
|
})
|
2024-07-08 06:45:43 +01:00
|
|
|
if not ok then
|
|
|
|
require("lazy.manage.task.fs").clean.run(task, { rocks_only = true })
|
|
|
|
end
|
2024-06-25 12:23:25 +01:00
|
|
|
end
|
|
|
|
|
2024-06-26 10:11:00 +01:00
|
|
|
---@param rockspec RockSpec
|
|
|
|
function M.is_simple_build(rockspec)
|
|
|
|
local type = vim.tbl_get(rockspec, "build", "type")
|
|
|
|
return type == nil or type == "none" or (type == "builtin" and not rockspec.build.modules)
|
|
|
|
end
|
|
|
|
|
2024-06-25 12:23:25 +01:00
|
|
|
---@param file string
|
|
|
|
---@return table?
|
|
|
|
function M.parse(file)
|
|
|
|
local ret = {}
|
2024-11-04 10:46:48 +00:00
|
|
|
local ok = pcall(function()
|
|
|
|
loadfile(file, nil, ret)()
|
2024-06-25 12:23:25 +01:00
|
|
|
end) and ret or nil
|
2024-11-04 10:46:48 +00:00
|
|
|
return ok and ret or nil
|
2024-06-25 12:23:25 +01:00
|
|
|
end
|
2024-06-24 20:57:34 +01:00
|
|
|
|
2024-06-24 17:02:54 +01:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.deps(plugin)
|
|
|
|
local root = Config.options.rocks.root .. "/" .. plugin.name
|
2024-06-25 12:23:25 +01:00
|
|
|
---@type RockManifest?
|
|
|
|
local manifest = M.parse(root .. "/lib/luarocks/rocks-5.1/manifest")
|
|
|
|
return manifest and vim.tbl_keys(manifest.repository or {})
|
2024-06-24 17:02:54 +01:00
|
|
|
end
|
|
|
|
|
2024-06-25 12:23:25 +01:00
|
|
|
---@param file string
|
|
|
|
---@return RockSpec?
|
|
|
|
function M.rockspec(file)
|
|
|
|
return M.parse(file)
|
|
|
|
end
|
2024-06-18 20:54:54 +01:00
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2024-06-25 19:53:42 +01:00
|
|
|
function M.find_rockspec(plugin)
|
2024-06-18 20:54:54 +01:00
|
|
|
local rockspec_file ---@type string?
|
|
|
|
Util.ls(plugin.dir, function(path, name, t)
|
2024-06-25 19:53:42 +01:00
|
|
|
if t == "file" then
|
|
|
|
for _, suffix in ipairs({ "scm", "git", "dev" }) do
|
|
|
|
suffix = suffix .. "-1.rockspec"
|
|
|
|
if name:sub(-#suffix) == suffix then
|
|
|
|
rockspec_file = path
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2024-06-18 20:54:54 +01:00
|
|
|
end
|
|
|
|
end)
|
2024-06-25 19:53:42 +01:00
|
|
|
return rockspec_file
|
|
|
|
end
|
2024-06-18 20:54:54 +01:00
|
|
|
|
2024-06-25 19:53:42 +01:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@return LazyPkgSpec?
|
|
|
|
function M.get(plugin)
|
|
|
|
if Community.get_spec(plugin.name) then
|
|
|
|
return {
|
|
|
|
file = "community",
|
|
|
|
source = "lazy",
|
|
|
|
spec = Community.get_spec(plugin.name),
|
|
|
|
}
|
2024-06-18 20:54:54 +01:00
|
|
|
end
|
|
|
|
|
2024-06-25 19:53:42 +01:00
|
|
|
local rockspec_file = M.find_rockspec(plugin)
|
|
|
|
local rockspec = rockspec_file and M.rockspec(rockspec_file)
|
2024-06-24 16:07:25 +01:00
|
|
|
if not rockspec then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local has_lua = not not vim.uv.fs_stat(plugin.dir .. "/lua")
|
|
|
|
|
2024-06-24 20:57:34 +01:00
|
|
|
---@type LazyPluginSpec
|
2024-06-25 19:53:42 +01:00
|
|
|
local specs = {}
|
2024-06-24 20:57:34 +01:00
|
|
|
|
2024-06-23 16:49:19 +01:00
|
|
|
---@param dep string
|
|
|
|
local rocks = vim.tbl_filter(function(dep)
|
|
|
|
local name = dep:gsub("%s.*", "")
|
2024-06-25 19:53:42 +01:00
|
|
|
local url = Community.get_url(name)
|
|
|
|
local spec = Community.get_spec(name)
|
|
|
|
|
|
|
|
if spec then
|
|
|
|
-- community spec
|
|
|
|
table.insert(specs, spec)
|
|
|
|
return false
|
|
|
|
elseif url then
|
|
|
|
-- Neovim plugin rock
|
2024-06-26 07:49:42 +01:00
|
|
|
table.insert(specs, { url })
|
2024-06-24 20:57:34 +01:00
|
|
|
return false
|
|
|
|
end
|
2024-06-23 16:49:19 +01:00
|
|
|
return not vim.tbl_contains(M.skip, name)
|
2024-06-24 16:07:25 +01:00
|
|
|
end, rockspec.dependencies or {})
|
2024-06-23 16:49:19 +01:00
|
|
|
|
2024-06-25 19:53:42 +01:00
|
|
|
local use =
|
|
|
|
-- package without a /lua directory
|
|
|
|
not has_lua
|
|
|
|
-- has dependencies that are not skipped,
|
|
|
|
-- not in community specs,
|
|
|
|
-- and don't have a rockspec mapping
|
2024-06-24 16:07:25 +01:00
|
|
|
or #rocks > 0
|
2024-06-25 19:53:42 +01:00
|
|
|
-- has a complex build process
|
2024-06-26 10:11:00 +01:00
|
|
|
or not M.is_simple_build(rockspec)
|
2024-06-24 16:07:25 +01:00
|
|
|
|
|
|
|
if not use then
|
2024-06-25 19:53:42 +01:00
|
|
|
-- community specs only
|
|
|
|
return #specs > 0
|
|
|
|
and {
|
|
|
|
file = vim.fn.fnamemodify(rockspec_file, ":t"),
|
|
|
|
spec = {
|
|
|
|
plugin.name,
|
|
|
|
specs = specs,
|
|
|
|
build = false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
or nil
|
2024-06-24 16:07:25 +01:00
|
|
|
end
|
2024-06-24 13:14:41 +01:00
|
|
|
|
|
|
|
local lazy = nil
|
2024-06-24 16:07:25 +01:00
|
|
|
if not has_lua then
|
2024-06-24 13:14:41 +01:00
|
|
|
lazy = false
|
|
|
|
end
|
|
|
|
|
2024-06-24 16:07:25 +01:00
|
|
|
return {
|
|
|
|
file = vim.fn.fnamemodify(rockspec_file, ":t"),
|
|
|
|
spec = {
|
|
|
|
plugin.name,
|
|
|
|
build = "rockspec",
|
|
|
|
lazy = lazy,
|
|
|
|
},
|
2024-06-24 20:57:34 +01:00
|
|
|
}
|
2024-06-18 20:54:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|