Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: manage luarocks installation as rockspec dependency #340

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/luarocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
version: ${{ env.LUAROCKS_VERSION }}
test_interpreters: ""
dependencies: |
luarocks >= 3.11.1, < 4.0.0
toml-edit >= 0.3.6
fidget.nvim >= 1.1.0
fzy
Expand Down
7 changes: 5 additions & 2 deletions bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ math.randomseed(os.time())

local config_data = vim.g.rocks_nvim or {}
local install_path = config_data.rocks_path or vim.fs.joinpath(vim.fn.stdpath("data") --[[@as string]], "rocks")
local luarocks_binary = config_data.luarocks_binary or vim.fs.joinpath(install_path, "bin", "luarocks")
local temp_luarocks_path =
---@diagnostic disable-next-line: param-type-mismatch
vim.fs.joinpath(vim.fn.stdpath("run"), ("luarocks-%X"):format(math.random(256 ^ 7)))
local luarocks_binary = vim.fs.joinpath(temp_luarocks_path, "bin", "luarocks")

---@param dep string
---@return boolean is_missing
Expand Down Expand Up @@ -104,7 +107,7 @@ local function set_up_luarocks(path)
return true
end

assert(set_up_luarocks(install_path), "failed to install luarocks! Please try again :)")
assert(set_up_luarocks(temp_luarocks_path), "failed to install luarocks! Please try again :)")

vim.notify("Installing rocks.nvim...")

Expand Down
2 changes: 1 addition & 1 deletion doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RocksOpts *RocksOpts*
Fields: ~
{rocks_path?} (string) Local path in your filesystem to install rocks. Defaults to a `rocks` directory in `vim.fn.stdpath("data")`.
{config_path?} (string) Rocks declaration file path. Defaults to `rocks.toml` in `vim.fn.stdpath("config")`.
{luarocks_binary?} (string) Luarocks binary path. Defaults to `luarocks`.
{luarocks_binary?} (string) Luarocks binary path. Defaults to `{rocks_path}/bin/luarocks`.
{lazy?} (boolean) Whether to query luarocks.org lazily. Defaults to `false`. Setting this to `true` may improve startup time, but features like auto-completion will lag initially.
{dynamic_rtp?} (boolean) Whether to automatically add freshly installed plugins to the 'runtimepath'. Defaults to `true` for the best default experience.
{generate_help_pages?} (boolean) Whether to re-generate plugins help pages after installation/upgrade.
Expand Down
44 changes: 22 additions & 22 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions installer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,18 @@ local function install()
if line == "< OK >" then
local install_path = input_fields.install_path.content
local setup_luarocks = input_fields.setup_luarocks.content == "true"
local temp_luarocks_path =
---@diagnostic disable-next-line: param-type-mismatch
vim.fs.joinpath(vim.fn.stdpath("run"), ("luarocks-%X"):format(math.random(256 ^ 7)))

local luarocks_binary = "luarocks"

if setup_luarocks then
local success = set_up_luarocks(install_path)
local success = set_up_luarocks(temp_luarocks_path)
if not success then
return
end
luarocks_binary = vim.fs.joinpath(install_path, "bin", "luarocks")
luarocks_binary = vim.fs.joinpath(temp_luarocks_path, "bin", "luarocks")
elseif vim.fn.executable(luarocks_binary) ~= 1 then
vim.notify(
luarocks_binary
Expand Down Expand Up @@ -389,7 +392,6 @@ local function install()

acquire_buffer_lock(buffer, function()
local install_path_rel = install_path:gsub(vim.env.HOME, "")
local luarocks_binary_rel = luarocks_binary:gsub(vim.env.HOME, "")

vim.api.nvim_buf_set_lines(buffer, 0, -1, true, {
"INSTALLATION COMPLETE",
Expand All @@ -399,7 +401,6 @@ local function install()
">lua",
" local rocks_config = {",
' rocks_path = vim.env.HOME .. "' .. install_path_rel .. '",',
mrcjkb marked this conversation as resolved.
Show resolved Hide resolved
' luarocks_binary = vim.env.HOME .. "' .. luarocks_binary_rel .. '",',
" }",
" ",
" vim.g.rocks_nvim = rocks_config",
Expand Down Expand Up @@ -437,7 +438,6 @@ local function install()
vim.fn.setreg('"', {
"local rocks_config = {",
' rocks_path = vim.env.HOME .. "' .. install_path_rel .. '",',
' luarocks_binary = vim.env.HOME .. "' .. luarocks_binary_rel .. '",',
"}",
"",
"vim.g.rocks_nvim = rocks_config",
Expand Down
2 changes: 1 addition & 1 deletion lua/rocks/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local config = {}
---@class RocksOpts
---@field rocks_path? string Local path in your filesystem to install rocks. Defaults to a `rocks` directory in `vim.fn.stdpath("data")`.
---@field config_path? string Rocks declaration file path. Defaults to `rocks.toml` in `vim.fn.stdpath("config")`.
---@field luarocks_binary? string Luarocks binary path. Defaults to `luarocks`.
---@field luarocks_binary? string Luarocks binary path. Defaults to `{rocks_path}/bin/luarocks`.
---@field lazy? boolean Whether to query luarocks.org lazily. Defaults to `false`. Setting this to `true` may improve startup time, but features like auto-completion will lag initially.
---@field dynamic_rtp? boolean Whether to automatically add freshly installed plugins to the 'runtimepath'. Defaults to `true` for the best default experience.
---@field generate_help_pages? boolean Whether to re-generate plugins help pages after installation/upgrade.
Expand Down
31 changes: 28 additions & 3 deletions lua/rocks/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,30 @@ local config = {}
local constants = require("rocks.constants")
local fs = require("rocks.fs")

---@diagnostic disable-next-line: param-type-mismatch
local default_rocks_path = vim.fs.joinpath(vim.fn.stdpath("data"), "rocks")

---@param rocks_path string
---@return string
local function get_default_luarocks_binary(rocks_path)
-- NOTE: On Windows, the binary installed with the luarocks rock is luarocks.bat,
-- but that doesn't seem to work with vim.system.
local default_luarocks_path = vim.fs.joinpath(rocks_path, "bin", "luarocks")
return vim.uv.fs_stat(default_luarocks_path) and default_luarocks_path or "luarocks"
end

local default_luarocks_binary = get_default_luarocks_binary(default_rocks_path)

--- rocks.nvim default configuration
---@class RocksConfig
local default_config = {
---@type string Local path in your filesystem to install rocks
---@diagnostic disable-next-line: param-type-mismatch
rocks_path = vim.fs.joinpath(vim.fn.stdpath("data"), "rocks"),
rocks_path = default_rocks_path,
---@type string Rocks declaration file path
---@diagnostic disable-next-line: param-type-mismatch
config_path = vim.fs.joinpath(vim.fn.stdpath("config"), "rocks.toml"),
---@type string Luarocks binary path
luarocks_binary = "luarocks",
luarocks_binary = get_default_luarocks_binary(default_rocks_path),
---@type boolean Whether to query luarocks.org lazily
lazy = false,
---@type boolean Whether to automatically add freshly installed plugins to the 'runtimepath'
Expand All @@ -41,6 +54,10 @@ local default_config = {
reinstall_dev_rocks_on_update = true,
---@type boolean Whether to use the luarocks loader to support multiple dependencies
enable_luarocks_loader = true,

-- Internal configs
---@type string
default_luarocks_binary = default_luarocks_binary,
---@class RocksConfigDebugInfo
debug_info = {
---@type boolean
Expand Down Expand Up @@ -90,6 +107,14 @@ config = vim.tbl_deep_extend("force", {
}, default_config, opts)
---@cast config RocksConfig

if not opts.luarocks_binary and opts.rocks_path and opts.rocks_path ~= default_rocks_path then
-- luarocks_binary has not been overridden, but rocks_path has
---@diagnostic disable-next-line: inject-field
config.default_luarocks_binary = get_default_luarocks_binary(opts.rocks_path)
---@diagnostic disable-next-line: inject-field
config.luarocks_binary = config.default_luarocks_binary
end

local ok, err = check.validate(config)
if not ok then
vim.notify("Rocks: " .. err, vim.log.levels.ERROR)
Expand Down
13 changes: 9 additions & 4 deletions lua/rocks/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ local loader = {}
local log = require("rocks.log")
local config = require("rocks.config.internal")

---@return string | nil
local function get_luarocks_lua_dir_from_luarocks()
local sc = vim.system({ config.luarocks_binary, "--lua-version=5.1", "which", "luarocks.loader" }):wait()
local ok, so = pcall(vim.system, { config.luarocks_binary, "--lua-version=5.1", "which", "luarocks.loader" })
if not ok then
log.error(("Could not invoke luarocks at %s"):format(config.luarocks_binary))
return
end
local sc = so:wait()
local result = sc.stdout and sc.stdout:match(vim.fs.joinpath("(%S+)", "5.1", "luarocks", "loader.lua"))
return result
end

---@return boolean
function loader.enable()
log.trace("Enabling luarocks loader")
local default_luarocks_binary = vim.fs.joinpath(config.rocks_path, "bin", "luarocks")
local luarocks_lua_dir = config.luarocks_binary == default_luarocks_binary
and vim.fs.joinpath(default_luarocks_binary, "share", "lua")
local luarocks_lua_dir = config.luarocks_binary == config.default_luarocks_binary
and vim.fs.joinpath(config.rocks_path, "share", "lua")
or get_luarocks_lua_dir_from_luarocks()
if luarocks_lua_dir then
package.path = package.path
Expand Down
14 changes: 11 additions & 3 deletions lua/rocks/luarocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ local function mk_server_args(servers)
end

---@param args string[] luarocks CLI arguments
---@param on_exit (function|nil) Called asynchronously when the luarocks command exits.
---@param on_exit fun(sc: vim.SystemCompleted)|nil Called asynchronously when the luarocks command exits.
--- asynchronously. Receives SystemCompleted object, see return of SystemObj:wait().
---@param opts? LuarocksCliOpts
---@return vim.SystemObj
---@see vim.system
luarocks.cli = function(args, on_exit, opts)
opts = opts or {}
Expand Down Expand Up @@ -86,7 +85,16 @@ luarocks.cli = function(args, on_exit, opts)
luarocks_cmd = vim.list_extend(luarocks_cmd, args)
log.info(luarocks_cmd)
opts.detach = true -- Prevent luarocks from exiting uncleanly
return vim.system(luarocks_cmd, opts, on_exit_wrapped)
local ok, err = pcall(vim.system, luarocks_cmd, opts, on_exit_wrapped)
if not ok then
---@type vim.SystemCompleted
local sc = {
code = 1,
signal = 0,
stderr = ("Failed to invoke luarocks at %s: %s"):format(config.luarocks_binary, err),
}
on_exit_wrapped(sc)
end
end

---@class LuarocksSearchOpts
Expand Down
29 changes: 26 additions & 3 deletions nix/plugin-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
}: final: prev: let
lib = final.lib;
rocks-nvim-luaPackage-override = luaself: luaprev: {
# Workaround for https://github.com/NixOS/nixpkgs/issues/316009
luarocks-rock = luaself.callPackage ({
buildLuarocksPackage,
fetchFromGitHub,
fetchurl,
}:
buildLuarocksPackage {
pname = "luarocks";
version = "3.11.1-1";
knownRockspec =
(fetchurl {
url = "mirror://luarocks/luarocks-3.11.1-1.rockspec";
sha256 = "0xg0siza8nlnnkaarmw73q12qx3frlfbysd5ipmxxi1d7yc38bbn";
})
.outPath;
src = fetchFromGitHub {
owner = "luarocks";
repo = "luarocks";
rev = "v3.11.1";
hash = "sha256-GglygI8HP+aDFEuucOkjQ2Pgfv4+jW+og+2vL3KoZCQ=";
};
}) {};

toml-edit =
(luaself.callPackage ({
buildLuarocksPackage,
Expand Down Expand Up @@ -133,7 +156,7 @@
luaOlder,
buildLuarocksPackage,
lua,
luarocks,
luarocks-rock,
toml-edit,
fidget-nvim,
nvim-nio,
Expand All @@ -147,7 +170,7 @@
src = self;
disabled = luaOlder "5.1";
propagatedBuildInputs = [
luarocks
luarocks-rock
toml-edit
fidget-nvim
nvim-nio
Expand Down Expand Up @@ -200,7 +223,7 @@ in {
-- Copied from installer.lua
local rocks_config = {
rocks_path = vim.fn.stdpath("data") .. "/rocks",
luarocks_binary = "${final.lua51Packages.luarocks}/bin/luarocks",
luarocks_binary = "${final.lua51Packages.luarocks-rock}/bin/luarocks",
}

vim.g.rocks_nvim = rocks_config
Expand Down
1 change: 1 addition & 0 deletions nix/test-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
neovim = nvim;
luaPackages = ps:
with ps; [
luarocks-rock
toml-edit
toml
fidget-nvim
Expand Down
1 change: 1 addition & 0 deletions plugin/rocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if vim.fn.has("nvim-" .. min_version) ~= 1 then
end

local log = require("rocks.log")
log.info(vim.uv.os_uname())
log.trace("loading nio")
local nio = require("nio")
log.trace("loading rocks.adapter")
Expand Down
Loading
Loading