Skip to content

Commit 3be55a4

Browse files
committed
feat: added support for plugin packages by lazy, rockspec and packspec
1 parent f1ba2e3 commit 3be55a4

11 files changed

+281
-171
lines changed

lua/lazy/core/config.lua

+13-7
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ M.defaults = {
3131
-- increase downloads a lot.
3232
filter = true,
3333
},
34+
pkg = {
35+
enabled = true,
36+
cache = vim.fn.stdpath("state") .. "/lazy/pkg-cache.lua",
37+
versions = true, -- Honor versions in pkg sources
38+
sources = {
39+
"lazy",
40+
"rockspec",
41+
"packspec",
42+
},
43+
},
3444
rocks = {
3545
root = vim.fn.stdpath("data") .. "/lazy-rocks",
46+
server = "https://nvim-neorocks.github.io/rocks-binaries/",
3647
},
3748
dev = {
3849
---@type string | fun(plugin: LazyPlugin): string directory where you store your local plugin projects
@@ -182,11 +193,6 @@ M.defaults = {
182193
-- Track each new require in the Lazy profiling tab
183194
require = false,
184195
},
185-
packspec = {
186-
enabled = true,
187-
versions = true, -- Honor dependency versions in packspecs
188-
path = vim.fn.stdpath("state") .. "/lazy/packspec.lua",
189-
},
190196
debug = false,
191197
}
192198

@@ -306,9 +312,9 @@ function M.setup(opts)
306312

307313
-- useful for plugin developers when making changes to a packspec file
308314
vim.api.nvim_create_autocmd("BufWritePost", {
309-
pattern = "package.lua",
315+
pattern = "lazy.lua",
310316
callback = function()
311-
require("lazy.view.commands").cmd("packspec")
317+
require("lazy.view.commands").cmd("pkg")
312318
end,
313319
})
314320
end,

lua/lazy/core/packspec.lua

-125
This file was deleted.

lua/lazy/core/plugin.lua

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local Config = require("lazy.core.config")
2-
local Packspec = require("lazy.core.packspec")
2+
local Pkg = require("lazy.pkg")
33
local Util = require("lazy.core.util")
44

55
---@class LazyCorePlugin
@@ -16,7 +16,7 @@ M.loading = false
1616
---@field notifs {msg:string, level:number, file?:string}[]
1717
---@field importing? string
1818
---@field optional? boolean
19-
---@field packspecs table<string, boolean>
19+
---@field pkgs table<string, boolean>
2020
---@field names table<string,string>
2121
local Spec = {}
2222
M.Spec = Spec
@@ -35,7 +35,7 @@ function Spec.new(spec, opts)
3535
self.dirty = {}
3636
self.notifs = {}
3737
self.ignore_installed = {}
38-
self.packspecs = {}
38+
self.pkgs = {}
3939
self.optional = opts and opts.optional
4040
self.names = {}
4141
if spec then
@@ -173,11 +173,11 @@ function Spec:add(plugin, results)
173173
end
174174

175175
-- import the plugin's spec
176-
if Config.options.packspec.enabled and plugin.dir and not self.packspecs[plugin.dir] then
177-
self.packspecs[plugin.dir] = true
178-
local packspec = Packspec.get(plugin)
179-
if packspec then
180-
self:normalize(packspec, nil, true)
176+
if Config.options.pkg.enabled and plugin.dir and not self.pkgs[plugin.dir] then
177+
self.pkgs[plugin.dir] = true
178+
local pkg = Pkg.get_spec(plugin)
179+
if pkg then
180+
self:normalize(pkg, nil)
181181
end
182182
end
183183

lua/lazy/manage/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function M.install(opts)
9393
}, opts):wait(function()
9494
require("lazy.manage.lock").update()
9595
require("lazy.help").update()
96-
require("lazy.core.packspec").update()
96+
require("lazy.pkg").update()
9797
end)
9898
end
9999

@@ -119,7 +119,7 @@ function M.update(opts)
119119
}, opts):wait(function()
120120
require("lazy.manage.lock").update()
121121
require("lazy.help").update()
122-
require("lazy.core.packspec").update()
122+
require("lazy.pkg").update()
123123
end)
124124
end
125125
--

lua/lazy/manage/rocks.lua

+20-19
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,32 @@ M.rocks = {}
1616
---@param ... string
1717
---@return string[]
1818
function M.args(...)
19-
local ret = { "--tree", Config.rocks.tree, "--lua-version", "5.1" }
19+
local ret = {
20+
"--tree",
21+
Config.rocks.tree,
22+
"--server",
23+
Config.options.rocks.server,
24+
"--dev",
25+
"--lua-version",
26+
"5.1",
27+
}
2028
vim.list_extend(ret, { ... })
2129
return ret
2230
end
2331

24-
function M.parse(rockspec_file)
25-
local rockspec = {}
26-
local ret, err = loadfile(rockspec_file, "t", rockspec)
27-
if not ret then
28-
error(err)
29-
end
30-
ret()
31-
return rockspec
32-
end
33-
34-
-- dd(M.parse("/home/folke/.local/share/nvim/lazy/neorg/neorg-scm-1.rockspec"))
35-
3632
---@param plugin LazyPlugin
3733
function M.get_rockspec(plugin)
38-
assert(plugin.rocks and #plugin.rocks > 0, plugin.name .. " has no rocks")
39-
local rockspec_file = Config.rocks.specs .. "/lazy-" .. plugin.name .. "-0.0-0.rockspec"
34+
local rocks = vim.tbl_map(function(rock)
35+
return rock.name
36+
end, plugin._.rocks)
37+
assert(rocks and #rocks > 0, plugin.name .. " has no rocks")
38+
local rockspec_file = Config.rocks.specs .. "/lazy-" .. plugin.name .. "-scm-1.rockspec"
4039
require("lazy.util").write_file(
4140
rockspec_file,
4241
([[
4342
rockspec_format = "3.0"
4443
package = "lazy-%s"
45-
version = "0.0-0"
44+
version = "scm-1"
4645
source = { url = "%s" }
4746
dependencies = %s
4847
build = { type = "builtin" }
@@ -78,9 +77,11 @@ function M.update_state()
7877
spec = spec,
7978
installed = installed[name] ~= nil,
8079
}
81-
plugin._.rocks_installed = plugin._.rocks_installed and rock.installed
82-
table.insert(plugin._.rocks, rock)
83-
table.insert(rocks, rock)
80+
if rock.name ~= "lua" then
81+
plugin._.rocks_installed = plugin._.rocks_installed and rock.installed
82+
table.insert(plugin._.rocks, rock)
83+
table.insert(rocks, rock)
84+
end
8485
end
8586
end
8687
end

lua/lazy/pkg/init.lua

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
local Config = require("lazy.core.config")
2+
local Util = require("lazy.util")
3+
4+
local M = {}
5+
6+
---@alias LazyPkgSpec LazySpec | fun():LazySpec
7+
8+
---@class LazyPkg
9+
---@field source string
10+
---@field file string
11+
---@field spec? LazySpec
12+
---@field chunk? string|fun():LazySpec
13+
14+
---@class LazyPkgSource
15+
---@field get fun(plugin:LazyPlugin):LazyPkg
16+
17+
---@type table<string, LazyPkg>?
18+
M.cache = nil
19+
20+
function M.update()
21+
---@type LazyPkgSource[]
22+
local sources = {}
23+
for _, s in ipairs(Config.options.pkg.sources) do
24+
sources[#sources + 1] = require("lazy.pkg." .. s)
25+
end
26+
27+
---@type table<string, LazyPkg>
28+
local ret = {}
29+
for _, plugin in pairs(Config.plugins) do
30+
for _, source in ipairs(sources) do
31+
local spec = source.get(plugin)
32+
if spec then
33+
if type(spec.chunk) == "function" then
34+
spec.chunk = string.dump(spec.chunk)
35+
end
36+
ret[plugin.dir] = spec
37+
break
38+
end
39+
end
40+
end
41+
local code = "return " .. Util.dump(ret)
42+
Util.write_file(Config.options.pkg.cache, code)
43+
M.cache = nil
44+
end
45+
46+
local function _load()
47+
Util.track("pkg")
48+
M.cache = {}
49+
if vim.uv.fs_stat(Config.options.pkg.cache) then
50+
Util.try(function()
51+
local chunk, err = loadfile(Config.options.pkg.cache)
52+
if not chunk then
53+
error(err)
54+
end
55+
M.cache = chunk()
56+
end, "Error loading pkg:")
57+
end
58+
Util.track()
59+
end
60+
61+
---@param plugin LazyPlugin
62+
---@return LazyPkg?
63+
function M.get(plugin)
64+
if not M.cache then
65+
_load()
66+
end
67+
68+
local ret = M.cache[plugin.dir]
69+
if not ret then
70+
return
71+
end
72+
73+
if ret.chunk and not ret.spec then
74+
if type(ret.chunk) == "string" then
75+
ret.chunk = load(ret.chunk, "@" .. plugin.dir)
76+
end
77+
ret.spec = ret.chunk()
78+
ret.chunk = nil
79+
end
80+
81+
return ret
82+
end
83+
84+
---@param plugin LazyPlugin
85+
---@return LazySpec?
86+
function M.get_spec(plugin)
87+
local pkg = M.get(plugin)
88+
local spec = pkg and pkg.spec
89+
return spec and type(spec) == "table" and vim.deepcopy(spec) or spec
90+
end
91+
92+
return M

0 commit comments

Comments
 (0)