From de1c86c93fb671c695f13cbba0b4ee35677e81e7 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Wed, 20 Mar 2024 00:31:31 +0100 Subject: [PATCH] feat(update): reinstall `dev` rocks by default (#210) * feat(update): reinstall `dev` rocks by default This also fixes some type annotations * docs(generated): update doc/rocks.txt skip-checks: true --------- Co-authored-by: Github Actions --- doc/rocks.txt | 17 +++++++++-------- lua/rocks/api/init.lua | 6 +++--- lua/rocks/config/init.lua | 1 + lua/rocks/config/internal.lua | 4 +++- lua/rocks/operations/init.lua | 25 ++++++++++++++++++++++++- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/doc/rocks.txt b/doc/rocks.txt index d389422b..6f2d6ee3 100644 --- a/doc/rocks.txt +++ b/doc/rocks.txt @@ -58,12 +58,13 @@ 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 `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. + {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 `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. + {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). ============================================================================== @@ -152,7 +153,7 @@ Specification for a rock in rocks.toml. May be extended by external modules. RocksToml *RocksToml* - { rocks?: RockSpec[], plugins?: RockSpec[], [string]: V } + { rocks?: table, plugins?: table, [string]: V } Content of rocks.toml @@ -169,7 +170,7 @@ api.get_user_rocks() *api.get_user_rocks* If the file doesn't exist a file with the default configuration will be created. Returns: ~ - (RockSpec[]) + (table) RocksCmd *RocksCmd* diff --git a/lua/rocks/api/init.lua b/lua/rocks/api/init.lua index 8214c81f..84e7bdb7 100644 --- a/lua/rocks/api/init.lua +++ b/lua/rocks/api/init.lua @@ -105,9 +105,9 @@ end ---Specification for a rock in rocks.toml. May be extended by external modules. ---@brief ]] ----@class RocksToml: { rocks?: RockSpec[], plugins?: RockSpec[], [string]: unknown } +---@class RocksToml: { rocks?: table, plugins?: table, [string]: unknown } ---@brief [[ ---- { rocks?: RockSpec[], plugins?: RockSpec[], [string]: V } +--- { rocks?: table, plugins?: table, [string]: V } --- ---Content of rocks.toml ---@brief ]] @@ -121,7 +121,7 @@ end ---Returns a table with the rock specifications parsed from the rocks.toml file. ---If the file doesn't exist a file with the default configuration will be created. ----@return RockSpec[] +---@return table function api.get_user_rocks() return config.get_user_rocks() end diff --git a/lua/rocks/config/init.lua b/lua/rocks/config/init.lua index 90401a10..b85bfb5f 100644 --- a/lua/rocks/config/init.lua +++ b/lua/rocks/config/init.lua @@ -20,6 +20,7 @@ local config = {} ---@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. +---@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). ---@type RocksOpts | fun():RocksOpts vim.g.rocks_nvim = vim.g.rocks_nvim diff --git a/lua/rocks/config/internal.lua b/lua/rocks/config/internal.lua index b6af7067..1d14f861 100644 --- a/lua/rocks/config/internal.lua +++ b/lua/rocks/config/internal.lua @@ -38,6 +38,8 @@ local default_config = { dynamic_rtp = true, ---@type boolean Whether to re-generate plugins help pages after installation/upgrade generate_help_pages = true, + ---@type boolean Whether to reinstall 'dev' rocks on update + reinstall_dev_rocks_on_update = true, ---@class RocksConfigDebugInfo debug_info = { ---@type boolean @@ -66,7 +68,7 @@ local default_config = { end return rocks_toml end, - ---@type fun():RockSpec[] + ---@type fun():table get_user_rocks = function() local rocks_toml = config.get_rocks_toml() return vim.tbl_deep_extend("force", rocks_toml.rocks or {}, rocks_toml.plugins or {}) diff --git a/lua/rocks/operations/init.lua b/lua/rocks/operations/init.lua index 83749547..48f6c898 100644 --- a/lua/rocks/operations/init.lua +++ b/lua/rocks/operations/init.lua @@ -63,6 +63,24 @@ local function get_percentage(counter, total) return counter > 0 and math.min(100, math.floor((counter / total) * 100)) or 0 end +---@param outdated_rocks table +---@return table +local function add_dev_rocks_for_update(outdated_rocks) + return vim.iter(config.get_user_rocks()):fold(outdated_rocks, function(acc, name, spec) + ---@cast acc table + ---@cast name rock_name + ---@cast spec RockSpec + if spec.version == "scm" then + acc[name] = { + name = spec.name, + version = spec.version, + target_version = spec.version, + } + end + return acc + end) +end + --- Synchronizes the user rocks with the physical state on the current machine. --- - Installs missing rocks --- - Ensures that the correct versions are installed @@ -335,6 +353,9 @@ operations.update = function() local user_rocks = parse_rocks_toml() local outdated_rocks = state.outdated_rocks() + if config.reinstall_dev_rocks_on_update then + outdated_rocks = add_dev_rocks_for_update(outdated_rocks) + end local external_update_handlers = handlers.get_update_handler_callbacks(user_rocks) local total_update_count = #outdated_rocks + #external_update_handlers @@ -365,7 +386,9 @@ operations.update = function() user_rocks.plugins[rock_name] = ret.version end progress_handle:report({ - message = ("Updated %s: %s -> %s"):format(rock.name, rock.version, rock.target_version), + message = rock.version == rock.target_version + and ("Updated rock %s: %s"):format(rock.name, rock.version) + or ("Updated %s: %s -> %s"):format(rock.name, rock.version, rock.target_version), percentage = get_percentage(ct, total_update_count), }) else