From 4c922b81b677048e36f2837cd30edceaf5b7cb49 Mon Sep 17 00:00:00 2001 From: Ben Lubas <56943754+benlubas@users.noreply.github.com> Date: Sat, 27 Jul 2024 21:39:15 -0400 Subject: [PATCH] feat: support dev.path & dev = true (#28) --- README.md | 15 +++++++++++- lua/rocks-dev/init.lua | 12 ++++++++-- lua/rocks-dev/local-rock-handler.lua | 3 ++- spec/load_spec.lua | 34 +++++++++++++++++++++------- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d95d7d2..3db3170 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,26 @@ and you are good to go! ## :books: Usage With this extension, you can add a `dir` field table to plugins in your `rocks.toml`, -for example +for example: ```toml [plugins."sweetie.nvim"] dir = "~/git/nvim/sweetie.nvim" ``` +This extension also supports `dev.path`, which allows you to provide the path to where local plugins +are stored. You can tell rocks-dev to load a plugin from that path with `dev = true`, for example: + +```toml +[dev] +path = "~/Projects" + +[plugins] +"sweetie.nvim" = { dev = true } +``` + +When both `dir` and `dev = true` are present, `dir` gets priority. + ## :book: License `rocks-dev.nvim` is licensed under [GPLv3](./LICENSE). diff --git a/lua/rocks-dev/init.lua b/lua/rocks-dev/init.lua index 4231a11..2ec4294 100644 --- a/lua/rocks-dev/init.lua +++ b/lua/rocks-dev/init.lua @@ -7,13 +7,21 @@ function rocks_dev.setup(user_configuration) return end + local dev_path = user_configuration.dev and user_configuration.dev.path + for _, data in pairs(user_configuration.plugins or {}) do + local path if data.dir then - vim.opt.runtimepath:append(vim.fn.expand(data.dir)) + path = vim.fn.expand(data.dir) + elseif dev_path and data.dev then + path = vim.fn.expand(vim.fs.joinpath(dev_path, data.name)) + end + if path then + vim.opt.runtimepath:append(path) -- NOTE: We can't support `opt` for dev plugins, -- as it doesn't integrate with `:Rocks packadd` - require("rtp_nvim").source_rtp_dir(vim.fn.expand(data.dir)) + require("rtp_nvim").source_rtp_dir(path) end end end diff --git a/lua/rocks-dev/local-rock-handler.lua b/lua/rocks-dev/local-rock-handler.lua index 55bdd07..fd4f6de 100644 --- a/lua/rocks-dev/local-rock-handler.lua +++ b/lua/rocks-dev/local-rock-handler.lua @@ -14,7 +14,8 @@ local rock_handler = {} ---@param rock RockSpec ---@return async fun(report_progress: fun(message: string), report_error: fun(message: string)) | nil function rock_handler.get_sync_callback(rock) - if rock.dir then + local user_configuration = api.get_rocks_toml() + if rock.dir or (rock.dev and user_configuration.dev.path) then ---@cast rock DevRockSpec ---@param report_progress fun(message: string) ---@param report_error fun(message: string) diff --git a/spec/load_spec.lua b/spec/load_spec.lua index 8c167a3..a312a24 100644 --- a/spec/load_spec.lua +++ b/spec/load_spec.lua @@ -8,17 +8,12 @@ vim.g.rocks_nvim = { } describe("rocks-dev", function() - it("can load local plugins with path alias", function() - local config_content = [[ -[plugins] -"foo.nvim" = { dir = "~/Projects/foo.nvim" } -]] + ---Assert that a plugin "foo.nvim" is loaded by the given configuration + local function assert_loaded(config_content) local fh = assert(io.open(vim.g.rocks_nvim.config_path, "w"), "Could not open rocks.toml for writing") fh:write(config_content) fh:close() - local plugin_content = [[ -vim.g.foo_nvim_loaded = true -]] + local plugin_content = "vim.g.foo_nvim_loaded = true" fh = assert( io.open(vim.fs.joinpath(tempdir, "Projects", "foo.nvim", "plugin", "foo.lua"), "w"), "Could not open config file for writing" @@ -29,5 +24,28 @@ vim.g.foo_nvim_loaded = true local user_configuration = require("rocks.api").get_rocks_toml() rocks_dev.setup(user_configuration) assert.True(vim.g.foo_nvim_loaded) + + -- reset the variable so that subsequent tests are not affected + vim.g.foo_nvim_loaded = nil + end + + it("can load local plugins with path alias", function() + local config_content = [[ +[plugins] +"foo.nvim" = { dir = "~/Projects/foo.nvim" } +]] + assert_loaded(config_content) + end) + + it("can load local plugins when `dev.path` is set", function() + local config_content = [[ +[dev] +path = "~/Projects" + +[plugins] +"foo.nvim" = { dev = true } +]] + + assert_loaded(config_content) end) end)