Skip to content

Commit

Permalink
feat(loader): support multiple versions of the same dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Mar 26, 2024
1 parent 0189ebe commit b6e97ba
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ insert_final_newline = unset
trim_trailing_whitespace = unset
indent_style = unset
indent_size = unset

[*/**/config/internal.lua]
indent_size = unset
indent_style = unset
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Name-based installation
(` "nvim-neorg/neorg" ` becomes `:Rocks install neorg` instead).
- Automatic dependency and build script management.
- Supports [multiple versions of the same dependency](https://github.com/luarocks/luarocks/wiki/Using-LuaRocks#multiple-versions-using-the-luarocks-package-loader).
- True semver versioning!
- Minimal, non-intrusive UI.
- Async execution.
Expand Down
20 changes: 20 additions & 0 deletions lua/rocks/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ local default_config = {
local rocks_toml = config.get_rocks_toml()
return vim.tbl_deep_extend("force", rocks_toml.rocks or {}, rocks_toml.plugins or {})
end,
---@type string
luarocks_config = nil,
}

---@type RocksOpts
Expand All @@ -99,6 +101,24 @@ if #config.debug_info.unrecognized_configs > 0 then
)
end

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)
)

---@diagnostic disable-next-line: inject-field
config.luarocks_config = ('"%s"'):format(luarocks_config_path)

return config

--- config.lua ends here
2 changes: 1 addition & 1 deletion lua/rocks/luarocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ luarocks.cli = function(args, on_exit, opts)
lock = nio.control.future()
end
opts.env = vim.tbl_deep_extend("force", opts.env or {}, {
LUAROCKS_CONFIG = "",
LUAROCKS_CONFIG = config.luarocks_config,
TREE_SITTER_LANGUAGE_VERSION = tostring(vim.treesitter.language_version),
})
local luarocks_cmd = {
Expand Down
17 changes: 17 additions & 0 deletions plugin/rocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ local nio = require("nio")
local adapter = require("rocks.adapter")
local config = require("rocks.config.internal")

-- Initialize the luarocks loader
local sc = vim.system({ config.luarocks_binary, "which", "luarocks.loader" }):wait()
local luarocks_path = sc.stdout and sc.stdout:match("(%S+)loader.lua")
if luarocks_path then
package.path = package.path .. ";" .. luarocks_path .. "?.lua"
vim.uv.os_setenv("LUAROCKS_CONFIG", config.luarocks_config)
local ok, err = pcall(function()
require("luarocks.loader")
end)
-- TODO: log errors
if not ok then
vim.notify("Failed to initialize luarocks loader: " .. err, vim.log.levels.DEBUG, {
title = "rocks.nvim",
})
end
end

-- Set up the Rocks user command
require("rocks.commands").create_commands()

Expand Down

0 comments on commit b6e97ba

Please sign in to comment.