Skip to content

Commit

Permalink
feat: added support for plugin packages by lazy, rockspec and packspec
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 24, 2024
1 parent f1ba2e3 commit 3be55a4
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 171 deletions.
20 changes: 13 additions & 7 deletions lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ M.defaults = {
-- increase downloads a lot.
filter = true,
},
pkg = {
enabled = true,
cache = vim.fn.stdpath("state") .. "/lazy/pkg-cache.lua",
versions = true, -- Honor versions in pkg sources
sources = {
"lazy",
"rockspec",
"packspec",
},
},
rocks = {
root = vim.fn.stdpath("data") .. "/lazy-rocks",
server = "https://nvim-neorocks.github.io/rocks-binaries/",
},
dev = {
---@type string | fun(plugin: LazyPlugin): string directory where you store your local plugin projects
Expand Down Expand Up @@ -182,11 +193,6 @@ M.defaults = {
-- Track each new require in the Lazy profiling tab
require = false,
},
packspec = {
enabled = true,
versions = true, -- Honor dependency versions in packspecs
path = vim.fn.stdpath("state") .. "/lazy/packspec.lua",
},
debug = false,
}

Expand Down Expand Up @@ -306,9 +312,9 @@ function M.setup(opts)

-- useful for plugin developers when making changes to a packspec file
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = "package.lua",
pattern = "lazy.lua",
callback = function()
require("lazy.view.commands").cmd("packspec")
require("lazy.view.commands").cmd("pkg")
end,
})
end,
Expand Down
125 changes: 0 additions & 125 deletions lua/lazy/core/packspec.lua

This file was deleted.

16 changes: 8 additions & 8 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Config = require("lazy.core.config")
local Packspec = require("lazy.core.packspec")
local Pkg = require("lazy.pkg")
local Util = require("lazy.core.util")

---@class LazyCorePlugin
Expand All @@ -16,7 +16,7 @@ M.loading = false
---@field notifs {msg:string, level:number, file?:string}[]
---@field importing? string
---@field optional? boolean
---@field packspecs table<string, boolean>
---@field pkgs table<string, boolean>
---@field names table<string,string>
local Spec = {}
M.Spec = Spec
Expand All @@ -35,7 +35,7 @@ function Spec.new(spec, opts)
self.dirty = {}
self.notifs = {}
self.ignore_installed = {}
self.packspecs = {}
self.pkgs = {}
self.optional = opts and opts.optional
self.names = {}
if spec then
Expand Down Expand Up @@ -173,11 +173,11 @@ function Spec:add(plugin, results)
end

-- import the plugin's spec
if Config.options.packspec.enabled and plugin.dir and not self.packspecs[plugin.dir] then
self.packspecs[plugin.dir] = true
local packspec = Packspec.get(plugin)
if packspec then
self:normalize(packspec, nil, true)
if Config.options.pkg.enabled and plugin.dir and not self.pkgs[plugin.dir] then
self.pkgs[plugin.dir] = true
local pkg = Pkg.get_spec(plugin)
if pkg then
self:normalize(pkg, nil)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/manage/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function M.install(opts)
}, opts):wait(function()
require("lazy.manage.lock").update()
require("lazy.help").update()
require("lazy.core.packspec").update()
require("lazy.pkg").update()
end)
end

Expand All @@ -119,7 +119,7 @@ function M.update(opts)
}, opts):wait(function()
require("lazy.manage.lock").update()
require("lazy.help").update()
require("lazy.core.packspec").update()
require("lazy.pkg").update()
end)
end
--
Expand Down
39 changes: 20 additions & 19 deletions lua/lazy/manage/rocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@ M.rocks = {}
---@param ... string
---@return string[]
function M.args(...)
local ret = { "--tree", Config.rocks.tree, "--lua-version", "5.1" }
local ret = {
"--tree",
Config.rocks.tree,
"--server",
Config.options.rocks.server,
"--dev",
"--lua-version",
"5.1",
}
vim.list_extend(ret, { ... })
return ret
end

function M.parse(rockspec_file)
local rockspec = {}
local ret, err = loadfile(rockspec_file, "t", rockspec)
if not ret then
error(err)
end
ret()
return rockspec
end

-- dd(M.parse("/home/folke/.local/share/nvim/lazy/neorg/neorg-scm-1.rockspec"))

---@param plugin LazyPlugin
function M.get_rockspec(plugin)
assert(plugin.rocks and #plugin.rocks > 0, plugin.name .. " has no rocks")
local rockspec_file = Config.rocks.specs .. "/lazy-" .. plugin.name .. "-0.0-0.rockspec"
local rocks = vim.tbl_map(function(rock)
return rock.name
end, plugin._.rocks)
assert(rocks and #rocks > 0, plugin.name .. " has no rocks")
local rockspec_file = Config.rocks.specs .. "/lazy-" .. plugin.name .. "-scm-1.rockspec"
require("lazy.util").write_file(
rockspec_file,
([[
rockspec_format = "3.0"
package = "lazy-%s"
version = "0.0-0"
version = "scm-1"
source = { url = "%s" }
dependencies = %s
build = { type = "builtin" }
Expand Down Expand Up @@ -78,9 +77,11 @@ function M.update_state()
spec = spec,
installed = installed[name] ~= nil,
}
plugin._.rocks_installed = plugin._.rocks_installed and rock.installed
table.insert(plugin._.rocks, rock)
table.insert(rocks, rock)
if rock.name ~= "lua" then
plugin._.rocks_installed = plugin._.rocks_installed and rock.installed
table.insert(plugin._.rocks, rock)
table.insert(rocks, rock)
end
end
end
end
Expand Down
92 changes: 92 additions & 0 deletions lua/lazy/pkg/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local Config = require("lazy.core.config")
local Util = require("lazy.util")

local M = {}

---@alias LazyPkgSpec LazySpec | fun():LazySpec

---@class LazyPkg
---@field source string
---@field file string
---@field spec? LazySpec
---@field chunk? string|fun():LazySpec

---@class LazyPkgSource
---@field get fun(plugin:LazyPlugin):LazyPkg

---@type table<string, LazyPkg>?
M.cache = nil

function M.update()
---@type LazyPkgSource[]
local sources = {}
for _, s in ipairs(Config.options.pkg.sources) do
sources[#sources + 1] = require("lazy.pkg." .. s)
end

---@type table<string, LazyPkg>
local ret = {}
for _, plugin in pairs(Config.plugins) do
for _, source in ipairs(sources) do
local spec = source.get(plugin)
if spec then
if type(spec.chunk) == "function" then
spec.chunk = string.dump(spec.chunk)
end
ret[plugin.dir] = spec
break
end
end
end
local code = "return " .. Util.dump(ret)
Util.write_file(Config.options.pkg.cache, code)
M.cache = nil
end

local function _load()
Util.track("pkg")
M.cache = {}
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()
end, "Error loading pkg:")
end
Util.track()
end

---@param plugin LazyPlugin
---@return LazyPkg?
function M.get(plugin)
if not M.cache then
_load()
end

local ret = M.cache[plugin.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
end

return ret
end

---@param plugin LazyPlugin
---@return LazySpec?
function M.get_spec(plugin)
local pkg = M.get(plugin)
local spec = pkg and pkg.spec
return spec and type(spec) == "table" and vim.deepcopy(spec) or spec
end

return M
Loading

0 comments on commit 3be55a4

Please sign in to comment.