From fe6d0b1745cb8171c441e81168df23a09238fc9e Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 23 Nov 2022 16:12:43 +0100 Subject: [PATCH] feat: task docs and options for logs --- lua/lazy/manager.lua | 27 ++++++++++++++++++++++---- lua/lazy/task.lua | 46 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/lua/lazy/manager.lua b/lua/lazy/manager.lua index 59ffae6f..291ec0b3 100644 --- a/lua/lazy/manager.lua +++ b/lua/lazy/manager.lua @@ -49,12 +49,20 @@ function M.run(operation, opts, filter) runner:wait(function() -- check if we need to do any post-install hooks for _, plugin in ipairs(runner:plugins()) do - if plugin.dirty and (plugin.opt == false or plugin.run) then - runner:add(Task.new(plugin, "run")) + if plugin.dirty then + runner:add(Task.new(plugin, "docs")) + if plugin.opt == false or plugin.run then + runner:add(Task.new(plugin, "run")) + end end plugin.dirty = false - if operation == "update" then - runner:add(Task.new(plugin, "log")) + if opts.show and operation == "update" and plugin.updated and plugin.updated.from ~= plugin.updated.to then + runner:add(Task.new(plugin, "log", { + log = { + from = plugin.updated.from, + to = plugin.updated.to, + }, + })) end end -- wait for post-install to finish @@ -96,6 +104,14 @@ function M.log(opts) end) end +---@param opts? ManagerOpts +function M.docs(opts) + ---@param plugin LazyPlugin + M.run("docs", opts, function(plugin) + return plugin.installed + end) +end + ---@param opts? ManagerOpts function M.clean(opts) opts = opts or {} @@ -133,6 +149,8 @@ end function M.clear() for _, plugin in pairs(Config.plugins) do + -- clear updated status + plugin.updated = nil -- clear finished tasks if plugin.tasks then ---@param task LazyTask @@ -142,6 +160,7 @@ function M.clear() end end M.to_clean = {} + vim.cmd([[do User LazyRender]]) end return M diff --git a/lua/lazy/task.lua b/lua/lazy/task.lua index eeda669a..79a9b2af 100644 --- a/lua/lazy/task.lua +++ b/lua/lazy/task.lua @@ -6,16 +6,30 @@ local Util = require("lazy.core.util") ---@field plugin LazyPlugin ---@field type TaskType ---@field running boolean +---@field opts TaskOptions local Task = {} ----@alias TaskType "update"|"install"|"run"|"clean"|"log" +---@alias TaskType "update"|"install"|"run"|"clean"|"log"|"docs" + +---@class TaskOptions +local options = { + log = { + since = "7 days ago", + ---@type string + from = nil, + ---@type string + to = nil, + }, +} ---@param plugin LazyPlugin ---@param type TaskType -function Task.new(plugin, type) +---@param opts? TaskOptions +function Task.new(plugin, type, opts) local self = setmetatable({}, { __index = Task, }) + self.opts = vim.tbl_deep_extend("force", {}, options, opts or {}) self.plugin = plugin self.type = type self.output = "" @@ -94,7 +108,7 @@ function Task:install() end function Task:run() - Loader.load(self.plugin) + Loader.load(self.plugin, { task = "run" }) local run = self.plugin.run if run then @@ -115,6 +129,14 @@ function Task:run() self:_done() end +function Task:docs() + local docs = self.plugin.dir .. "/doc/" + if Util.file_exists(docs) then + self.output = vim.api.nvim_cmd({ cmd = "helptags", args = { docs } }, { output = true }) + end + self:_done() +end + ---@param cmd string ---@param opts ProcessOpts function Task:spawn(cmd, opts) @@ -162,6 +184,8 @@ function Task:start() self:clean() elseif self.type == "log" then self:log() + elseif self.type == "docs" then + self:docs() end end) @@ -184,8 +208,14 @@ function Task:log() "--decorate", "--date=short", "--color=never", - "--since='7 days ago'", } + + if self.opts.log.from then + table.insert(args, self.opts.log.from .. ".." .. (self.opts.log.to or "HEAD")) + else + table.insert(args, "--since=" .. self.opts.log.since) + end + self:spawn("git", { args = args, cwd = self.plugin.dir, @@ -209,14 +239,18 @@ function Task:update() "--update-shallow", "--progress", } - local git = Util.git_info(self.plugin.dir) + local git = assert(Util.git_info(self.plugin.dir)) self:spawn("git", { args = args, cwd = self.plugin.dir, on_exit = function(ok) if ok then - local git_new = Util.git_info(self.plugin.dir) + local git_new = assert(Util.git_info(self.plugin.dir)) + self.plugin.updated = { + from = git.hash, + to = git_new.hash, + } self.plugin.dirty = not vim.deep_equal(git, git_new) end end,