Skip to content

Commit

Permalink
feat: support extending the default luarocks config with a table
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 3, 2024
1 parent 543b7f1 commit fba7170
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 34 deletions.
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
35 changes: 26 additions & 9 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,32 @@ 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?} (string|table)
Path to the luarocks config file or table of extra luarocks config options.
If a table or not set, rocks.nvim will create a default luarocks config in `rocks_path`
and merge it with this table.
Warning: this is a file path, You should include the settings in the default luarocks-config.lua before overriding this.


==============================================================================
Expand Down
44 changes: 35 additions & 9 deletions lua/rocks/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,41 @@ 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
---
--- Path to the luarocks config file or table of extra luarocks config options.
--- If a table or not set, rocks.nvim will create a default luarocks config in `rocks_path`
--- and merge it with this table.
--- Warning: this is a file path, You should include the settings in the default luarocks-config.lua before overriding this.
---@field luarocks_config? string | table

---@type RocksOpts | fun():RocksOpts
vim.g.rocks_nvim = vim.g.rocks_nvim
Expand Down
37 changes: 22 additions & 15 deletions lua/rocks/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ if #config.debug_info.unrecognized_configs > 0 then
)
end

if opts.luarocks_config then
if type(opts.luarocks_config) == "string" then
-- luarocks_config override
if vim.uv.fs_stat(opts.luarocks_config) then
---@diagnostic disable-next-line: inject-field
Expand All @@ -163,21 +163,28 @@ if opts.luarocks_config then
opts.luarocks_config = nil
end
end
if not opts.luarocks_config then
if not opts.luarocks_config or type(opts.luarocks_config) == "table" 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)
)
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 {})

---@type string
local config_str = vim.iter(luarocks_config):fold("", function(acc, k, v)
return ([[
%s
%s = %s
]]):format(acc, k, vim.inspect(v))
end)

fs.write_file(luarocks_config_path, "w+", config_str)

---@diagnostic disable-next-line: inject-field
config.luarocks_config = ("%s"):format(luarocks_config_path)
Expand Down
34 changes: 34 additions & 0 deletions spec/luarocks_config_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local nio = require("nio")

vim.env.PLENARY_TEST_TIMEOUT = 60000

describe("luarocks config", function()
nio.tests.it("extra luarocks_config", function()
local tempdir = vim.fn.tempname()

local external_deps_dirs = {
"/some/path",
}

vim.g.rocks_nvim = {
luarocks_binary = "luarocks",
rocks_path = tempdir,
luarocks_config = {
external_deps_dirs = external_deps_dirs,
},
}

local config = require("rocks.config.internal")
nio.sleep(2000)
assert.is_not_nil(vim.uv.fs_stat(config.luarocks_config))
local luarocks_config = {}
loadfile(config.luarocks_config, "t", luarocks_config)()
assert.same({
lua_version = "5.1",
external_deps_dirs = external_deps_dirs,
rocks_trees = {
{ name = "rocks.nvim", root = tempdir },
},
}, luarocks_config)
end)
end)

0 comments on commit fba7170

Please sign in to comment.