2024-06-19 03:54:54 +08:00
|
|
|
--# selene:allow(incorrect_standard_library_use)
|
2024-06-25 00:02:54 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2024-06-25 19:23:25 +08:00
|
|
|
local Health = require("lazy.health")
|
2024-06-25 00:02:54 +08:00
|
|
|
local Util = require("lazy.util")
|
2024-06-19 03:54:54 +08:00
|
|
|
|
2024-06-25 19:23:25 +08:00
|
|
|
---@class RockSpec
|
|
|
|
---@field rockspec_format string
|
|
|
|
---@field package string
|
|
|
|
---@field version string
|
|
|
|
---@field dependencies string[]
|
|
|
|
---@field build? {build_type?: string, modules?: any[]}
|
|
|
|
|
|
|
|
---@class RockManifest
|
|
|
|
---@field repository table<string, any>
|
|
|
|
|
2024-06-19 03:54:54 +08:00
|
|
|
local M = {}
|
|
|
|
|
2024-06-24 20:14:41 +08:00
|
|
|
M.dev_suffix = "-1.rockspec"
|
2024-06-19 03:54:54 +08:00
|
|
|
M.skip = { "lua" }
|
2024-06-25 03:57:34 +08:00
|
|
|
M.rewrites = {
|
|
|
|
["plenary.nvim"] = { "nvim-lua/plenary.nvim", lazy = true },
|
|
|
|
}
|
2024-06-25 19:23:25 +08: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
|
|
|
|
if Config.options.rocks.hererocks then
|
|
|
|
if M.hererocks.building() then
|
|
|
|
ok = true
|
|
|
|
else
|
|
|
|
ok = Health.have(M.python, opts)
|
|
|
|
ok = Health.have(M.hererocks.bin("luarocks")) and ok
|
|
|
|
ok = Health.have(
|
|
|
|
M.hererocks.bin("lua"),
|
|
|
|
vim.tbl_extend("force", opts, {
|
|
|
|
version = "-v",
|
|
|
|
version_pattern = "5.1",
|
|
|
|
})
|
|
|
|
) and ok
|
|
|
|
end
|
|
|
|
else
|
|
|
|
ok = Health.have("luarocks", opts)
|
|
|
|
ok = (
|
|
|
|
Health.have(
|
|
|
|
{ "lua5.1", "lua" },
|
|
|
|
vim.tbl_extend("force", opts, {
|
|
|
|
version = "-v",
|
|
|
|
version_pattern = "5.1",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
) and ok
|
|
|
|
end
|
|
|
|
return ok
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param task LazyTask
|
|
|
|
function M.build(task)
|
|
|
|
if
|
|
|
|
not M.check({
|
|
|
|
error = function(msg)
|
|
|
|
task:notify_error(msg:gsub("[{}]", "`"))
|
|
|
|
end,
|
|
|
|
warn = function(msg)
|
|
|
|
task:notify_warn(msg)
|
|
|
|
end,
|
|
|
|
ok = function(msg) end,
|
|
|
|
})
|
|
|
|
then
|
|
|
|
task:notify_warn({
|
|
|
|
"",
|
|
|
|
"This plugin requires `luarocks`. Try one of the following:",
|
|
|
|
" - fix your `luarocks` installation",
|
|
|
|
Config.options.rocks.hererocks and " - disable *hererocks* with `opts.rocks.hererocks = false`"
|
|
|
|
or " - enable `hererocks` with `opts.rocks.hererocks = true`",
|
|
|
|
" - disable `luarocks` support completely with `opts.rocks.enabled = false`",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if task.plugin.name == "hererocks" then
|
|
|
|
return M.hererocks.build(task)
|
|
|
|
end
|
|
|
|
|
|
|
|
local env = {}
|
|
|
|
local luarocks = "luarocks"
|
|
|
|
if Config.options.rocks.hererocks then
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
local root = Config.options.rocks.root .. "/" .. task.plugin.name
|
|
|
|
task:spawn(luarocks, {
|
|
|
|
args = {
|
|
|
|
"--tree",
|
|
|
|
root,
|
|
|
|
"--server",
|
|
|
|
Config.options.rocks.server,
|
|
|
|
"--dev",
|
|
|
|
"--lua-version",
|
|
|
|
"5.1",
|
|
|
|
"make",
|
|
|
|
"--force-fast",
|
|
|
|
},
|
|
|
|
cwd = task.plugin.dir,
|
|
|
|
env = env,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param file string
|
|
|
|
---@return table?
|
|
|
|
function M.parse(file)
|
|
|
|
local ret = {}
|
|
|
|
return pcall(function()
|
|
|
|
loadfile(file, "t", ret)()
|
|
|
|
end) and ret or nil
|
|
|
|
end
|
2024-06-25 03:57:34 +08:00
|
|
|
|
2024-06-25 00:02:54 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.deps(plugin)
|
|
|
|
local root = Config.options.rocks.root .. "/" .. plugin.name
|
2024-06-25 19:23:25 +08: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-25 00:02:54 +08:00
|
|
|
end
|
|
|
|
|
2024-06-25 19:23:25 +08:00
|
|
|
---@param file string
|
|
|
|
---@return RockSpec?
|
|
|
|
function M.rockspec(file)
|
|
|
|
return M.parse(file)
|
|
|
|
end
|
2024-06-19 03:54:54 +08:00
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2024-06-24 20:14:41 +08:00
|
|
|
---@return LazyPkgSpec?
|
2024-06-19 03:54:54 +08:00
|
|
|
function M.get(plugin)
|
2024-06-25 03:57:34 +08:00
|
|
|
if M.rewrites[plugin.name] then
|
|
|
|
return {
|
|
|
|
file = "rewrite",
|
|
|
|
source = "lazy",
|
|
|
|
spec = M.rewrites[plugin.name],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-06-19 03:54:54 +08:00
|
|
|
local rockspec_file ---@type string?
|
|
|
|
Util.ls(plugin.dir, function(path, name, t)
|
|
|
|
if t == "file" and name:sub(-#M.dev_suffix) == M.dev_suffix then
|
|
|
|
rockspec_file = path
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
if not rockspec_file then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-06-25 19:23:25 +08:00
|
|
|
local rockspec = M.rockspec(rockspec_file)
|
2024-06-23 23:49:19 +08:00
|
|
|
|
2024-06-24 23:07:25 +08:00
|
|
|
if not rockspec then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local has_lua = not not vim.uv.fs_stat(plugin.dir .. "/lua")
|
|
|
|
|
2024-06-25 03:57:34 +08:00
|
|
|
---@type LazyPluginSpec
|
|
|
|
local rewrites = {}
|
|
|
|
|
2024-06-23 23:49:19 +08:00
|
|
|
---@param dep string
|
|
|
|
local rocks = vim.tbl_filter(function(dep)
|
|
|
|
local name = dep:gsub("%s.*", "")
|
2024-06-25 03:57:34 +08:00
|
|
|
if M.rewrites[name] then
|
|
|
|
table.insert(rewrites, M.rewrites[name])
|
|
|
|
return false
|
|
|
|
end
|
2024-06-23 23:49:19 +08:00
|
|
|
return not vim.tbl_contains(M.skip, name)
|
2024-06-24 23:07:25 +08:00
|
|
|
end, rockspec.dependencies or {})
|
2024-06-23 23:49:19 +08:00
|
|
|
|
2024-06-24 23:07:25 +08:00
|
|
|
local use = not has_lua
|
|
|
|
or #rocks > 0
|
|
|
|
or (
|
|
|
|
rockspec.build
|
|
|
|
and rockspec.build.build_type
|
|
|
|
and rockspec.build.build_type ~= "none"
|
|
|
|
and not (rockspec.build.build_type == "builtin" and not rockspec.build.modules)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not use then
|
2024-06-25 03:57:34 +08:00
|
|
|
if #rewrites > 0 then
|
|
|
|
return {
|
|
|
|
file = vim.fn.fnamemodify(rockspec_file, ":t"),
|
|
|
|
spec = rewrites,
|
|
|
|
}
|
|
|
|
end
|
2024-06-24 23:07:25 +08:00
|
|
|
return
|
|
|
|
end
|
2024-06-24 20:14:41 +08:00
|
|
|
|
|
|
|
local lazy = nil
|
2024-06-24 23:07:25 +08:00
|
|
|
if not has_lua then
|
2024-06-24 20:14:41 +08:00
|
|
|
lazy = false
|
|
|
|
end
|
|
|
|
|
2024-06-24 23:07:25 +08:00
|
|
|
return {
|
|
|
|
file = vim.fn.fnamemodify(rockspec_file, ":t"),
|
|
|
|
spec = {
|
|
|
|
plugin.name,
|
|
|
|
build = "rockspec",
|
|
|
|
lazy = lazy,
|
|
|
|
},
|
2024-06-25 03:57:34 +08:00
|
|
|
}
|
2024-06-19 03:54:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|