2024-06-18 20:54:54 +01:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Util = require("lazy.util")
|
|
|
|
|
|
|
|
local M = {}
|
2024-06-24 13:14:41 +01:00
|
|
|
M.VERSION = 8
|
2024-06-23 21:00:48 +01:00
|
|
|
M.dirty = false
|
2024-06-18 20:54:54 +01:00
|
|
|
|
|
|
|
---@class LazyPkg
|
2024-06-23 16:49:19 +01:00
|
|
|
---@field name string
|
2024-06-24 13:14:41 +01:00
|
|
|
---@field dir string
|
|
|
|
---@field source "lazy" | "packspec" | "rockspec"
|
2024-06-18 20:54:54 +01:00
|
|
|
---@field file string
|
2024-06-24 13:14:41 +01:00
|
|
|
---@field spec LazyPluginSpec
|
2024-06-23 21:00:48 +01:00
|
|
|
|
2024-06-24 13:14:41 +01:00
|
|
|
---@class LazyPkgSpec
|
|
|
|
---@field file string
|
|
|
|
---@field spec? LazySpec
|
2024-06-23 21:00:48 +01:00
|
|
|
---@field code? string
|
2024-06-18 20:54:54 +01:00
|
|
|
|
|
|
|
---@class LazyPkgSource
|
2024-06-24 13:14:41 +01:00
|
|
|
---@field name string
|
|
|
|
---@field get fun(plugin:LazyPlugin):LazyPkgSpec?
|
|
|
|
|
|
|
|
---@class LazyPkgCache
|
|
|
|
---@field pkgs LazyPkg[]
|
|
|
|
---@field version number
|
2024-06-18 20:54:54 +01:00
|
|
|
|
|
|
|
---@type table<string, LazyPkg>?
|
|
|
|
M.cache = nil
|
|
|
|
|
|
|
|
function M.update()
|
|
|
|
---@type LazyPkgSource[]
|
|
|
|
local sources = {}
|
|
|
|
for _, s in ipairs(Config.options.pkg.sources) do
|
2024-06-24 13:14:41 +01:00
|
|
|
sources[#sources + 1] = {
|
|
|
|
name = s,
|
|
|
|
get = require("lazy.pkg." .. s).get,
|
|
|
|
}
|
2024-06-18 20:54:54 +01:00
|
|
|
end
|
|
|
|
|
2024-06-24 13:14:41 +01:00
|
|
|
---@type LazyPkgCache
|
|
|
|
local ret = {
|
|
|
|
version = M.VERSION,
|
|
|
|
pkgs = {},
|
|
|
|
}
|
2024-06-18 20:54:54 +01:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2024-06-23 21:00:48 +01:00
|
|
|
if plugin._.installed then
|
|
|
|
for _, source in ipairs(sources) do
|
|
|
|
local spec = source.get(plugin)
|
|
|
|
if spec then
|
2024-06-24 13:14:41 +01:00
|
|
|
---@type LazyPkg
|
|
|
|
local pkg = {
|
|
|
|
name = plugin.name,
|
|
|
|
dir = plugin.dir,
|
|
|
|
source = source.name,
|
|
|
|
file = spec.file,
|
|
|
|
spec = spec.spec or {},
|
|
|
|
}
|
2024-06-23 21:00:48 +01:00
|
|
|
if type(spec.code) == "string" then
|
2024-06-24 13:14:41 +01:00
|
|
|
pkg.spec = { _raw = spec.code }
|
2024-06-23 21:00:48 +01:00
|
|
|
end
|
2024-06-24 13:14:41 +01:00
|
|
|
table.insert(ret.pkgs, pkg)
|
2024-06-23 21:00:48 +01:00
|
|
|
break
|
2024-06-18 20:54:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-24 13:14:41 +01:00
|
|
|
local code = "return " .. Util.dump(ret)
|
2024-06-23 21:04:32 +01:00
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(Config.options.pkg.cache, ":h"), "p")
|
2024-06-18 20:54:54 +01:00
|
|
|
Util.write_file(Config.options.pkg.cache, code)
|
2024-06-23 21:00:48 +01:00
|
|
|
M.dirty = false
|
2024-06-18 20:54:54 +01:00
|
|
|
M.cache = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _load()
|
|
|
|
Util.track("pkg")
|
2024-06-23 21:00:48 +01:00
|
|
|
M.cache = nil
|
2024-06-18 20:54:54 +01:00
|
|
|
if vim.uv.fs_stat(Config.options.pkg.cache) then
|
|
|
|
Util.try(function()
|
|
|
|
local chunk, err = loadfile(Config.options.pkg.cache)
|
|
|
|
if not chunk then
|
|
|
|
error(err)
|
|
|
|
end
|
2024-06-24 13:14:41 +01:00
|
|
|
---@type LazyPkgCache?
|
2024-06-23 21:00:48 +01:00
|
|
|
local ret = chunk()
|
|
|
|
if ret and ret.version == M.VERSION then
|
2024-06-24 13:14:41 +01:00
|
|
|
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
|
2024-06-23 21:00:48 +01:00
|
|
|
end
|
2024-06-18 20:54:54 +01:00
|
|
|
end, "Error loading pkg:")
|
|
|
|
end
|
2024-06-23 21:00:48 +01:00
|
|
|
if rawget(M, "cache") then
|
|
|
|
M.dirty = false
|
|
|
|
else
|
|
|
|
M.cache = {}
|
|
|
|
M.dirty = true
|
|
|
|
end
|
2024-06-18 20:54:54 +01:00
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
|
2024-06-23 21:00:48 +01:00
|
|
|
---@param dir string
|
2024-06-18 20:54:54 +01:00
|
|
|
---@return LazyPkg?
|
2024-06-24 13:14:41 +01:00
|
|
|
---@overload fun():table<string, LazyPkg>
|
2024-06-23 21:00:48 +01:00
|
|
|
function M.get(dir)
|
2024-06-24 13:14:41 +01:00
|
|
|
if dir then
|
|
|
|
return M.cache[dir]
|
2024-06-18 20:54:54 +01:00
|
|
|
end
|
2024-06-24 13:14:41 +01:00
|
|
|
return M.cache
|
2024-06-23 16:49:19 +01:00
|
|
|
end
|
|
|
|
|
2024-06-23 21:00:48 +01:00
|
|
|
return setmetatable(M, {
|
|
|
|
__index = function(_, key)
|
|
|
|
if key == "cache" then
|
|
|
|
_load()
|
|
|
|
return M.cache
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|