feat: spec.rocks is no longer needed & added support for installing any luarock

This commit is contained in:
Folke Lemaitre 2024-06-24 14:14:41 +02:00
parent 66249054c4
commit 73ea05feda
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
10 changed files with 120 additions and 235 deletions

View file

@ -2,23 +2,28 @@ local Config = require("lazy.core.config")
local Util = require("lazy.util")
local M = {}
M.VERSION = 7
M.VERSION = 8
M.dirty = false
---@alias LazyPkgSpec LazySpec | fun():LazySpec
---@class LazyPkg
---@field source string
---@field name string
---@field dir string
---@field source "lazy" | "packspec" | "rockspec"
---@field file string
---@field spec LazyPluginSpec
---@class LazyPkgSpec
---@field file string
---@field spec? LazySpec
---@class LazyPkgInput: LazyPkg
---@field spec? LazySpec|fun():LazySpec
---@field code? string
---@class LazyPkgSource
---@field get fun(plugin:LazyPlugin):LazyPkgInput?
---@field name string
---@field get fun(plugin:LazyPlugin):LazyPkgSpec?
---@class LazyPkgCache
---@field pkgs LazyPkg[]
---@field version number
---@type table<string, LazyPkg>?
M.cache = nil
@ -27,27 +32,40 @@ function M.update()
---@type LazyPkgSource[]
local sources = {}
for _, s in ipairs(Config.options.pkg.sources) do
sources[#sources + 1] = require("lazy.pkg." .. s)
sources[#sources + 1] = {
name = s,
get = require("lazy.pkg." .. s).get,
}
end
M.cache = {}
---@type LazyPkgCache
local ret = {
version = M.VERSION,
pkgs = {},
}
for _, plugin in pairs(Config.plugins) do
if plugin._.installed then
for _, source in ipairs(sources) do
local spec = source.get(plugin)
if spec then
spec.name = plugin.name
---@type LazyPkg
local pkg = {
name = plugin.name,
dir = plugin.dir,
source = source.name,
file = spec.file,
spec = spec.spec or {},
}
if type(spec.code) == "string" then
spec.spec = { _raw = spec.code }
spec.code = nil
pkg.spec = { _raw = spec.code }
end
M.cache[plugin.dir] = spec
table.insert(ret.pkgs, pkg)
break
end
end
end
end
local code = "return " .. Util.dump({ version = M.VERSION, specs = M.cache })
local code = "return " .. Util.dump(ret)
vim.fn.mkdir(vim.fn.fnamemodify(Config.options.pkg.cache, ":h"), "p")
Util.write_file(Config.options.pkg.cache, code)
M.dirty = false
@ -63,9 +81,18 @@ local function _load()
if not chunk then
error(err)
end
---@type LazyPkgCache?
local ret = chunk()
if ret and ret.version == M.VERSION then
M.cache = ret.specs
M.cache = {}
for _, pkg in ipairs(ret.pkgs) do
if type(pkg.spec) == "function" then
pkg.spec = pkg.spec()
end
-- wrap in the scope of the plugin
pkg.spec = { pkg.name, specs = pkg.spec }
M.cache[pkg.dir] = pkg
end
end
end, "Error loading pkg:")
end
@ -80,34 +107,12 @@ end
---@param dir string
---@return LazyPkg?
---@overload fun():table<string, LazyPkg>
function M.get(dir)
local ret = M.cache[dir]
if not ret then
return
if dir then
return M.cache[dir]
end
if type(ret.spec) == "function" then
ret.spec = ret.spec()
end
return ret
end
function M.spec()
---@type table<string,LazyPluginSpec>
local ret = {}
for dir in pairs(M.cache) do
local pkg = M.get(dir)
local spec = pkg and pkg.spec
if pkg and spec then
spec = type(spec) == "table" and vim.deepcopy(spec) or spec
---@cast spec LazySpec
ret[dir] = { pkg.name, specs = spec }
end
end
return ret
return M.cache
end
return setmetatable(M, {

View file

@ -4,7 +4,7 @@ local Util = require("lazy.core.util")
local M = {}
M.dev_suffix = "-scm-1.rockspec"
M.dev_suffix = "-1.rockspec"
M.skip = { "lua" }
---@class RockSpec
@ -14,7 +14,7 @@ M.skip = { "lua" }
---@field dependencies string[]
---@param plugin LazyPlugin
---@return LazyPkg?
---@return LazyPkgSpec?
function M.get(plugin)
local rockspec_file ---@type string?
Util.ls(plugin.dir, function(path, name, t)
@ -43,13 +43,21 @@ function M.get(plugin)
return not vim.tbl_contains(M.skip, name)
end, rockspec and rockspec.dependencies or {})
return #rocks > 0
local use = #rocks > 0
use = true
local lazy = nil
if not vim.uv.fs_stat(plugin.dir .. "/lua") then
lazy = false
end
return use
and {
source = "rockspec",
file = vim.fn.fnamemodify(rockspec_file, ":t"),
spec = {
plugin.name,
rocks = rocks,
build = "rockspec",
lazy = lazy,
},
}
or nil