From 5a311d04f6b6fe30c2ed5bdaa5783b2b2cc7f67d Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 23 Jun 2024 22:00:48 +0200 Subject: [PATCH] fix(pkg): versioning and reload specs when pkg-cache is dirty --- lua/lazy/core/plugin.lua | 7 +++ lua/lazy/pkg/init.lua | 92 +++++++++++++++++++------------------- lua/lazy/pkg/lazy.lua | 3 +- lua/lazy/pkg/rockspec.lua | 2 +- lua/lazy/view/commands.lua | 3 +- 5 files changed, 59 insertions(+), 48 deletions(-) diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index 713e54c..878ed3f 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -1,5 +1,6 @@ local Config = require("lazy.core.config") local Meta = require("lazy.core.meta") +local Pkg = require("lazy.pkg") local Util = require("lazy.core.util") ---@class LazyCorePlugin @@ -332,6 +333,12 @@ function M.load() Util.track("state") M.update_state() Util.track() + + if Config.options.pkg.enabled and Pkg.dirty then + Pkg.update() + return M.load() + end + M.loading = false vim.api.nvim_exec_autocmds("User", { pattern = "LazyPlugins", modeline = false }) end diff --git a/lua/lazy/pkg/init.lua b/lua/lazy/pkg/init.lua index bee9b42..781f66a 100644 --- a/lua/lazy/pkg/init.lua +++ b/lua/lazy/pkg/init.lua @@ -2,6 +2,8 @@ local Config = require("lazy.core.config") local Util = require("lazy.util") local M = {} +M.VERSION = 7 +M.dirty = false ---@alias LazyPkgSpec LazySpec | fun():LazySpec @@ -10,10 +12,13 @@ local M = {} ---@field name string ---@field file string ---@field spec? LazySpec ----@field chunk? string|fun():LazySpec + +---@class LazyPkgInput: LazyPkg +---@field spec? LazySpec|fun():LazySpec +---@field code? string ---@class LazyPkgSource ----@field get fun(plugin:LazyPlugin):LazyPkg +---@field get fun(plugin:LazyPlugin):LazyPkgInput? ---@type table? M.cache = nil @@ -25,76 +30,78 @@ function M.update() sources[#sources + 1] = require("lazy.pkg." .. s) end - ---@type table - local ret = {} + M.cache = {} for _, plugin in pairs(Config.plugins) do - for _, source in ipairs(sources) do - local spec = source.get(plugin) - if spec then - spec.name = plugin.name - if type(spec.chunk) == "function" then - spec.chunk = string.dump(spec.chunk) + if plugin._.installed then + for _, source in ipairs(sources) do + local spec = source.get(plugin) + if spec then + spec.name = plugin.name + if type(spec.code) == "string" then + spec.spec = { _raw = spec.code } + spec.code = nil + end + M.cache[plugin.dir] = spec + break end - ret[plugin.dir] = spec - break end end end - local code = "return " .. Util.dump(ret) + local code = "return " .. Util.dump({ version = M.VERSION, specs = M.cache }) Util.write_file(Config.options.pkg.cache, code) + M.dirty = false M.cache = nil end local function _load() Util.track("pkg") - M.cache = {} + M.cache = nil 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 - M.cache = chunk() + local ret = chunk() + if ret and ret.version == M.VERSION then + M.cache = ret.specs + end end, "Error loading pkg:") end + if rawget(M, "cache") then + M.dirty = false + else + M.cache = {} + M.dirty = true + end Util.track() end ----@param plugin LazyPlugin +---@param dir string ---@return LazyPkg? -function M.get(plugin) - if not M.cache then - _load() - end - - local ret = M.cache[plugin.dir] +function M.get(dir) + local ret = M.cache[dir] if not ret then return end - if ret.chunk and not ret.spec then - if type(ret.chunk) == "string" then - ret.chunk = load(ret.chunk, "@" .. plugin.dir) - end - ret.spec = ret.chunk() - ret.chunk = nil + if type(ret.spec) == "function" then + ret.spec = ret.spec() end return ret end function M.spec() - if not M.cache then - _load() - end ---@type table local ret = {} for dir in pairs(M.cache) do - local pkg = M.get({ dir = dir }) + 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 @@ -102,16 +109,11 @@ function M.spec() return ret end ----@param plugin LazyPlugin ----@return LazySpec? -function M.get_spec(plugin) - local pkg = M.get(plugin) - local spec = pkg and pkg.spec - if not spec then - return - end - spec = type(spec) == "table" and vim.deepcopy(spec) or spec - return { plugin.name, specs = spec } -end - -return M +return setmetatable(M, { + __index = function(_, key) + if key == "cache" then + _load() + return M.cache + end + end, +}) diff --git a/lua/lazy/pkg/lazy.lua b/lua/lazy/pkg/lazy.lua index 07b0491..9ca0637 100644 --- a/lua/lazy/pkg/lazy.lua +++ b/lua/lazy/pkg/lazy.lua @@ -16,11 +16,12 @@ function M.get(plugin) end, "`" .. M.lazy_file .. "` for **" .. plugin.name .. "** has errors:") if not chunk then Util.error("Invalid `" .. M.lazy_file .. "` for **" .. plugin.name .. "**") + return end return { source = "lazy", file = M.lazy_file, - chunk = chunk, + code = "function()\n" .. Util.read_file(file) .. "\nend", } end end diff --git a/lua/lazy/pkg/rockspec.lua b/lua/lazy/pkg/rockspec.lua index 5732fbf..c0e5ec3 100644 --- a/lua/lazy/pkg/rockspec.lua +++ b/lua/lazy/pkg/rockspec.lua @@ -46,7 +46,7 @@ function M.get(plugin) return #rocks > 0 and { source = "rockspec", - file = rockspec_file, + file = vim.fn.fnamemodify(rockspec_file, ":t"), spec = { plugin.name, rocks = rocks, diff --git a/lua/lazy/view/commands.lua b/lua/lazy/view/commands.lua index 6b828a5..9791924 100644 --- a/lua/lazy/view/commands.lua +++ b/lua/lazy/view/commands.lua @@ -34,6 +34,7 @@ M.commands = { health = function() vim.cmd.checkhealth("lazy") end, + ---@param opts ManagerOpts pkg = function(opts) local Pkg = require("lazy.pkg") Pkg.update() @@ -44,7 +45,7 @@ M.commands = { }, }) for _, plugin in pairs(opts and opts.plugins or {}) do - local spec = Pkg.get(plugin) + local spec = Pkg.get(plugin.dir) Util.info(vim.inspect(spec), { lang = "lua", title = plugin.name }) end end,