Skip to content

Commit

Permalink
feat(rocks): simple rockspecs are now fully resolved by lazy without …
Browse files Browse the repository at this point in the history
…luarocks. See #1548
  • Loading branch information
folke committed Jun 25, 2024
1 parent be74a8a commit 6b8bf58
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 32 deletions.
4 changes: 4 additions & 0 deletions lua/lazy/community/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ function M.get_url(rock)
return load()[rock]
end

function M.get_spec(name)
return require("lazy.community.specs")[name]
end

return M
7 changes: 7 additions & 0 deletions lua/lazy/community/specs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---@type table<string, LazySpec>
return {
["plenary.nvim"] = {
"nvim-lua/plenary.nvim",
lazy = true,
},
}
2 changes: 1 addition & 1 deletion lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function M.find_local_spec()
return
end
local path = vim.uv.cwd()
while path ~= "" do
while path and path ~= "" do
local file = path .. "/" .. M.LOCAL_SPEC
if vim.fn.filereadable(file) == 1 then
return {
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/pkg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local Config = require("lazy.core.config")
local Util = require("lazy.core.util")

local M = {}
M.VERSION = 10
M.VERSION = 12
M.dirty = false

---@class LazyPkg
Expand Down
97 changes: 67 additions & 30 deletions lua/lazy/pkg/rockspec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
--# selene:allow(incorrect_standard_library_use)
local Community = require("lazy.community")

local Config = require("lazy.core.config")
local Health = require("lazy.health")
local Util = require("lazy.util")
Expand All @@ -16,11 +18,11 @@ local Util = require("lazy.util")

local M = {}

M.dev_suffix = "-1.rockspec"
M.skip = { "lua" }
M.rewrites = {
["plenary.nvim"] = { "nvim-lua/plenary.nvim", lazy = true },
}

M.python = { "python3", "python" }

---@class HereRocks
Expand Down Expand Up @@ -151,6 +153,15 @@ function M.build(task)
end
end

local pkg = task.plugin._.pkg
assert(pkg, "missing rockspec pkg for " .. task.plugin.name .. "\nThis shouldn't happen, please report.")

local rockspec = M.rockspec(task.plugin.dir .. "/" .. pkg.file) or {}
assert(
rockspec.package,
"missing rockspec package name for " .. task.plugin.name .. "\nThis shouldn't happen, please report."
)

local root = Config.options.rocks.root .. "/" .. task.plugin.name
task:spawn(luarocks, {
args = {
Expand All @@ -161,8 +172,11 @@ function M.build(task)
"--dev",
"--lua-version",
"5.1",
"make",
"install", -- use install so that we can make use of pre-built rocks
"--force-fast",
"--deps-mode",
"one",
rockspec.package,
},
cwd = task.plugin.dir,
env = env,
Expand Down Expand Up @@ -193,51 +207,70 @@ function M.rockspec(file)
end

---@param plugin LazyPlugin
---@return LazyPkgSpec?
function M.get(plugin)
if M.rewrites[plugin.name] then
return {
file = "rewrite",
source = "lazy",
spec = M.rewrites[plugin.name],
}
end

function M.find_rockspec(plugin)
local rockspec_file ---@type string?
Util.ls(plugin.dir, function(path, name, t)
if t == "file" and name:sub(-#M.dev_suffix) == M.dev_suffix then
rockspec_file = path
return false
if t == "file" then
for _, suffix in ipairs({ "scm", "git", "dev" }) do
suffix = suffix .. "-1.rockspec"
if name:sub(-#suffix) == suffix then
rockspec_file = path
return false
end
end
end
end)
return rockspec_file
end

if not rockspec_file then
return
---@param plugin LazyPlugin
---@return LazyPkgSpec?
function M.get(plugin)
if Community.get_spec(plugin.name) then
return {
file = "community",
source = "lazy",
spec = Community.get_spec(plugin.name),
}
end

local rockspec = M.rockspec(rockspec_file)

local rockspec_file = M.find_rockspec(plugin)
local rockspec = rockspec_file and M.rockspec(rockspec_file)
if not rockspec then
return
end

local has_lua = not not vim.uv.fs_stat(plugin.dir .. "/lua")

---@type LazyPluginSpec
local rewrites = {}
local specs = {}

---@param dep string
local rocks = vim.tbl_filter(function(dep)
local name = dep:gsub("%s.*", "")
if M.rewrites[name] then
table.insert(rewrites, M.rewrites[name])
local url = Community.get_url(name)
local spec = Community.get_spec(name)

if spec then
-- community spec
table.insert(specs, spec)
return false
elseif url then
-- Neovim plugin rock
table.insert(specs, { url, lazy = true })
return false
end
return not vim.tbl_contains(M.skip, name)
end, rockspec.dependencies or {})

local use = not has_lua
local use =
-- package without a /lua directory
not has_lua
-- has dependencies that are not skipped,
-- not in community specs,
-- and don't have a rockspec mapping
or #rocks > 0
-- has a complex build process
or (
rockspec.build
and rockspec.build.build_type
Expand All @@ -246,13 +279,17 @@ function M.get(plugin)
)

if not use then
if #rewrites > 0 then
return {
file = vim.fn.fnamemodify(rockspec_file, ":t"),
spec = rewrites,
}
end
return
-- community specs only
return #specs > 0
and {
file = vim.fn.fnamemodify(rockspec_file, ":t"),
spec = {
plugin.name,
specs = specs,
build = false,
},
}
or nil
end

local lazy = nil
Expand Down

0 comments on commit 6b8bf58

Please sign in to comment.