From b6e97ba20e01f8bf592500eaee5b285891635fdb Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Tue, 26 Mar 2024 23:28:40 +0100 Subject: [PATCH] feat(loader): support multiple versions of the same dependency --- .editorconfig | 4 ++++ README.md | 1 + lua/rocks/config/internal.lua | 20 ++++++++++++++++++++ lua/rocks/luarocks.lua | 2 +- plugin/rocks.lua | 17 +++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index b328442c..ed0098e4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/README.md b/README.md index 69a55cfa..557f0fb3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lua/rocks/config/internal.lua b/lua/rocks/config/internal.lua index 1d14f861..0f2826fb 100644 --- a/lua/rocks/config/internal.lua +++ b/lua/rocks/config/internal.lua @@ -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 @@ -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 diff --git a/lua/rocks/luarocks.lua b/lua/rocks/luarocks.lua index 615b31fd..b2dfb992 100644 --- a/lua/rocks/luarocks.lua +++ b/lua/rocks/luarocks.lua @@ -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 = { diff --git a/plugin/rocks.lua b/plugin/rocks.lua index 2a906dba..dc9d6a1b 100644 --- a/plugin/rocks.lua +++ b/plugin/rocks.lua @@ -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()