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: support extending the default luarocks config #452

Merged
merged 10 commits into from
Jul 5, 2024
4 changes: 2 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: nightly
version: stable
- name: Install rocks.nvim
run: |
mkdir rocks
Expand All @@ -54,7 +54,7 @@ jobs:
cat rocks/rocks.log
echo "vim.cmd.colorscheme('sweetie')" >> .github/resources/init-integration.lua
echo "vim.cmd.e('success')" >> .github/resources/init-integration.lua
nvim -u .github/resources/init-integration.lua -c +wq
nvim -u .github/resources/init-integration.lua -c 'set noswapfile' +wq
if [ ! -f success ]; then
echo "Integration test failed!"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
result
result*
https___*
.pre-commit-config.yaml
.direnv
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,11 @@ Examples:

### Updating rocks

Running the `:Rocks update` command will attempt to update every available rock
if it is not pinned.
- Running the `:Rocks update` command will update every available rock
that is not pinned.

- `:Rocks install {rock}` (without a version) will update `{rock}`
to the latest version.

### Syncing rocks

Expand Down
36 changes: 27 additions & 9 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ rocks.nvim commands *rocks-commands*
- pin={true|false}
Rocks that have been installed with 'pim=true'
will be ignored by ':Rocks update'.
Use 'Rocks! install ...' to skip prompts
prune {rock} Uninstall {rock} and its stale dependencies,
and remove it from rocks.toml.
sync Synchronize installed rocks with rocks.toml.
It may take more than one sync to prune all rocks that can be pruned.
update Search for updated rocks and install them.
Use 'Rocks! update` to skip prompts
with breaking changes.
edit Edit the rocks.toml file.
pin {rock} Pin {rock} to the installed version.
Pinned rocks are ignored by ':Rocks update'.
Expand All @@ -97,15 +100,30 @@ rocks.nvim configuration *rocks-config*
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 `{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. Defaults to `true`.
{reinstall_dev_rocks_on_update?} (boolean) Whether to reinstall 'dev' rocks on update (Default: `true`, as rocks.nvim cannot determine if 'dev' rocks are up to date).
{enable_luarocks_loader?} (boolean) Whether to use the luarocks loader to support multiple dependencies (Default: `true`).
{luarocks_config?} (string) Path to the luarocks config. If not set, rocks.nvim will create one in `rocks_path`. Warning: You should include the settings in the default luarocks-config.lua before overriding this.
{rocks_path?} (string)
Local path in your file system to install rocks
(Default: a `rocks` directory in `vim.fn.stdpath("data")`).
{config_path?} (string)
Rocks declaration file path (Default: `rocks.toml`) in `vim.fn.stdpath("config")`.
{luarocks_binary?} (string)
Luarocks binary path (Default: `{rocks_path}/bin/luarocks`).
{lazy?} (boolean)
Whether to query luarocks.org lazily (Default: `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'.
(Default: `true` for the best default experience).
{generate_help_pages?} (boolean)
Whether to re-generate plugins help pages after installation/upgrade. (Default: `true`).
{reinstall_dev_rocks_on_update?} (boolean)
Whether to reinstall 'dev' rocks on update
(Default: `true`, as rocks.nvim cannot determine if 'dev' rocks are up to date).
{enable_luarocks_loader?} (boolean)
Whether to use the luarocks loader to support multiple dependencies (Default: `true`).
{luarocks_config?} (table)
Extra luarocks config options.
rocks.nvim will create a default luarocks config in `rocks_path` and merge it with this table (if set).


==============================================================================
Expand Down
1 change: 0 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
inherit nvim;
plugins = with pkgs.lua51Packages; [
toml-edit
toml
fidget-nvim
fzy
nvim-nio
Expand Down
15 changes: 11 additions & 4 deletions lua/rocks/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
--- - pin={true|false}
--- Rocks that have been installed with 'pim=true'
--- will be ignored by ':Rocks update'.
--- Use 'Rocks! install ...' to skip prompts
--- prune {rock} Uninstall {rock} and its stale dependencies,
--- and remove it from rocks.toml.
--- sync Synchronize installed rocks with rocks.toml.
--- It may take more than one sync to prune all rocks that can be pruned.
--- update Search for updated rocks and install them.
--- Use 'Rocks! update` to skip prompts
--- with breaking changes.
--- edit Edit the rocks.toml file.
--- pin {rock} Pin {rock} to the installed version.
--- Pinned rocks are ignored by ':Rocks update'.
Expand Down Expand Up @@ -119,8 +122,10 @@ end
---@type { [string]: RocksCmd }
local rocks_command_tbl = {
update = {
impl = function(_)
require("rocks.operations").update()
impl = function(_, opts)
require("rocks.operations").update(nil, {
skip_prompts = opts.bang,
})
end,
},
sync = {
Expand All @@ -129,12 +134,14 @@ local rocks_command_tbl = {
end,
},
install = {
impl = function(args)
impl = function(args, opts)
if #args == 0 then
vim.notify("Rocks install: Called without required package argument.", vim.log.levels.ERROR)
return
end
require("rocks.operations").add(args)
require("rocks.operations").add(args, nil, {
skip_prompts = opts.bang,
})
end,
complete = function(query)
local name, version_query = query:match("([^%s]+)%s(.+)$")
Expand Down
42 changes: 33 additions & 9 deletions lua/rocks/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,39 @@ local config = {}
---@tag vim.g.rocks_nvim
---@tag g:rocks_nvim
---@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 `{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. Defaults to `true`.
---@field reinstall_dev_rocks_on_update? boolean Whether to reinstall 'dev' rocks on update (Default: `true`, as rocks.nvim cannot determine if 'dev' rocks are up to date).
---@field enable_luarocks_loader? boolean Whether to use the luarocks loader to support multiple dependencies (Default: `true`).
---@field luarocks_config? string Path to the luarocks config. If not set, rocks.nvim will create one in `rocks_path`. Warning: You should include the settings in the default luarocks-config.lua before overriding this.
---
--- Local path in your file system to install rocks
--- (Default: a `rocks` directory in `vim.fn.stdpath("data")`).
---@field rocks_path? string
---
--- Rocks declaration file path (Default: `rocks.toml`) in `vim.fn.stdpath("config")`.
---@field config_path? string
---
--- Luarocks binary path (Default: `{rocks_path}/bin/luarocks`).
---@field luarocks_binary? string
---
--- Whether to query luarocks.org lazily (Default: `false`).
--- Setting this to `true` may improve startup time,
--- but features like auto-completion will lag initially.
---@field lazy? boolean
---
--- Whether to automatically add freshly installed plugins to the 'runtimepath'.
--- (Default: `true` for the best default experience).
---@field dynamic_rtp? boolean
---
--- Whether to re-generate plugins help pages after installation/upgrade. (Default: `true`).
---@field generate_help_pages? boolean
---
--- Whether to reinstall 'dev' rocks on update
--- (Default: `true`, as rocks.nvim cannot determine if 'dev' rocks are up to date).
---@field reinstall_dev_rocks_on_update? boolean
---
--- Whether to use the luarocks loader to support multiple dependencies (Default: `true`).
---@field enable_luarocks_loader? boolean
---
--- Extra luarocks config options.
--- rocks.nvim will create a default luarocks config in `rocks_path` and merge it with this table (if set).
---@field luarocks_config? table

---@type RocksOpts | fun():RocksOpts
vim.g.rocks_nvim = vim.g.rocks_nvim
Expand Down
79 changes: 58 additions & 21 deletions lua/rocks/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ local default_config = {
vim.tbl_deep_extend("force", vim.empty_dict(), rocks_toml.rocks or {}, rocks_toml.plugins or {})
return config.apply_rock_spec_modifiers(user_rocks)
end,
---@type string
luarocks_config = nil,
---@type fun():string
luarocks_config_path = nil,
}

---@type RocksOpts
Expand Down Expand Up @@ -153,34 +153,71 @@ if #config.debug_info.unrecognized_configs > 0 then
)
end

if opts.luarocks_config then
---@return string
local function mk_luarocks_config()
local default_luarocks_config = {
lua_version = "5.1",
rocks_trees = {
{
name = "rocks.nvim",
root = config.rocks_path,
},
},
}
local luarocks_config = vim.tbl_deep_extend("force", default_luarocks_config, opts.luarocks_config or {})

local config_str = vim.iter(luarocks_config):fold("", function(acc, k, v)
return ([[
%s
%s = %s
]]):format(acc, k, vim.inspect(v))
end)
require("rocks.log").debug("luarocks config:\n" .. config_str)
return config_str
end

if type(opts.luarocks_config) == "string" then
vim.deprecate(
"g:rocks_nvim.luarocks_config (string)",
"g:rocks_nvim.luarocks_config (table)",
"3.0.0",
"rocks.nvim"
)
-- luarocks_config override
if vim.uv.fs_stat(opts.luarocks_config) then
local luarocks_config_path = ("%s"):format(opts.luarocks_config)
---@diagnostic disable-next-line: inject-field
config.luarocks_config = ("%s"):format(opts.luarocks_config)
config.luarocks_config_path = function()
return luarocks_config_path
end
else
vim.notify("rocks.nvim: luarocks_config does not exist!", vim.log.levels.ERROR)
opts.luarocks_config = nil
end
end
if not opts.luarocks_config then
local luarocks_config_path = vim.fs.joinpath(config.rocks_path, "luarocks-config.lua")
fs.write_file(
luarocks_config_path,
"w+",
([==[
lua_version = "5.1"
rocks_trees = {
{
name = "rocks.nvim",
root = "%s",
},
}
]==]):format(config.rocks_path)
)

if not opts.luarocks_config or type(opts.luarocks_config) == "table" then
local luarocks_config_path
---@diagnostic disable-next-line: inject-field
config.luarocks_config = ("%s"):format(luarocks_config_path)
config.luarocks_config_path = function()
if luarocks_config_path then
return ("%s"):format(luarocks_config_path)
end
luarocks_config_path = vim.fs.joinpath(config.rocks_path, "luarocks-config.lua")
require("rocks.log").debug("luarocks config path: " .. luarocks_config_path)
-- NOTE: We don't use fs/libuv here, because we need the file to be written
-- before it is used
local fh = io.open(luarocks_config_path, "w+")
if fh then
local config_str = mk_luarocks_config()
fh:write(config_str)
fh:close()
else
require("rocks.log").error(("Could not open %s for writing."):format(luarocks_config_path))
luarocks_config_path = ""
end
---@diagnostic disable-next-line: inject-field
return ("%s"):format(luarocks_config_path)
end
end

return config
Expand Down
9 changes: 6 additions & 3 deletions lua/rocks/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ local config = require("rocks.config.internal")

---@return string | nil
local function get_luarocks_lua_dir_from_luarocks()
local ok, so = pcall(vim.system, { config.luarocks_binary, "--lua-version=5.1", "which", "luarocks.loader" })
---@type boolean, vim.SystemObj
local ok, so = pcall(vim.system, { config.luarocks_binary, "--lua-version=5.1", "which", "luarocks.loader" }, nil)
if not ok then
log.error(("Could not invoke luarocks at %s"):format(config.luarocks_binary))
return
end
---@type vim.SystemCompleted
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
---@return boolean success
function loader.enable()
local luarocks_config_path = config.luarocks_config_path()
log.trace("Enabling luarocks loader")
local luarocks_lua_dir = config.luarocks_binary == config.default_luarocks_binary
and vim.fs.joinpath(config.rocks_path, "share", "lua")
Expand All @@ -28,7 +31,7 @@ function loader.enable()
vim.fs.joinpath(luarocks_lua_dir, "5.1", "?.lua"),
vim.fs.joinpath(luarocks_lua_dir, "5.1", "init.lua"),
}, ";")
vim.env.LUAROCKS_CONFIG = config.luarocks_config
vim.env.LUAROCKS_CONFIG = luarocks_config_path
local ok, err = pcall(require, "luarocks.loader")
if ok then
return true
Expand Down
6 changes: 3 additions & 3 deletions lua/rocks/luarocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end
--- asynchronously. Receives SystemCompleted object, see return of SystemObj:wait().
---@param opts? LuarocksCliOpts
---@see vim.system
luarocks.cli = function(args, on_exit, opts)
luarocks.cli = nio.create(function(args, on_exit, opts)
opts = opts or {}
---@cast opts LuarocksCliOpts
opts.synchronized = opts.synchronized ~= nil and opts.synchronized or false
Expand All @@ -72,7 +72,7 @@ luarocks.cli = function(args, on_exit, opts)
semaphore.acquire()
end
opts.env = vim.tbl_deep_extend("force", opts.env or {}, {
LUAROCKS_CONFIG = config.luarocks_config,
LUAROCKS_CONFIG = config.luarocks_config_path(),
TREE_SITTER_LANGUAGE_VERSION = tostring(vim.treesitter.language_version),
LUA_PATH = ('"%s"'):format(package.path),
LUA_CPATH = ('"%s"'):format(package.cpath),
Expand All @@ -97,7 +97,7 @@ luarocks.cli = function(args, on_exit, opts)
}
on_exit_wrapped(sc)
end
end
end, 3)

---@class LuarocksSearchOpts
---@field dev? boolean Include dev manifest? Default: false
Expand Down
Loading
Loading