feat: task docs and options for logs

This commit is contained in:
Folke Lemaitre 2022-11-23 16:12:43 +01:00
parent 6f835ab87b
commit fe6d0b1745
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 63 additions and 10 deletions

View File

@ -49,12 +49,20 @@ function M.run(operation, opts, filter)
runner:wait(function() runner:wait(function()
-- check if we need to do any post-install hooks -- check if we need to do any post-install hooks
for _, plugin in ipairs(runner:plugins()) do for _, plugin in ipairs(runner:plugins()) do
if plugin.dirty and (plugin.opt == false or plugin.run) then if plugin.dirty then
runner:add(Task.new(plugin, "docs"))
if plugin.opt == false or plugin.run then
runner:add(Task.new(plugin, "run")) runner:add(Task.new(plugin, "run"))
end end
end
plugin.dirty = false plugin.dirty = false
if operation == "update" then if opts.show and operation == "update" and plugin.updated and plugin.updated.from ~= plugin.updated.to then
runner:add(Task.new(plugin, "log")) runner:add(Task.new(plugin, "log", {
log = {
from = plugin.updated.from,
to = plugin.updated.to,
},
}))
end end
end end
-- wait for post-install to finish -- wait for post-install to finish
@ -96,6 +104,14 @@ function M.log(opts)
end) end)
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 ---@param opts? ManagerOpts
function M.clean(opts) function M.clean(opts)
opts = opts or {} opts = opts or {}
@ -133,6 +149,8 @@ end
function M.clear() function M.clear()
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
-- clear updated status
plugin.updated = nil
-- clear finished tasks -- clear finished tasks
if plugin.tasks then if plugin.tasks then
---@param task LazyTask ---@param task LazyTask
@ -142,6 +160,7 @@ function M.clear()
end end
end end
M.to_clean = {} M.to_clean = {}
vim.cmd([[do User LazyRender]])
end end
return M return M

View File

@ -6,16 +6,30 @@ local Util = require("lazy.core.util")
---@field plugin LazyPlugin ---@field plugin LazyPlugin
---@field type TaskType ---@field type TaskType
---@field running boolean ---@field running boolean
---@field opts TaskOptions
local Task = {} 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 plugin LazyPlugin
---@param type TaskType ---@param type TaskType
function Task.new(plugin, type) ---@param opts? TaskOptions
function Task.new(plugin, type, opts)
local self = setmetatable({}, { local self = setmetatable({}, {
__index = Task, __index = Task,
}) })
self.opts = vim.tbl_deep_extend("force", {}, options, opts or {})
self.plugin = plugin self.plugin = plugin
self.type = type self.type = type
self.output = "" self.output = ""
@ -94,7 +108,7 @@ function Task:install()
end end
function Task:run() function Task:run()
Loader.load(self.plugin) Loader.load(self.plugin, { task = "run" })
local run = self.plugin.run local run = self.plugin.run
if run then if run then
@ -115,6 +129,14 @@ function Task:run()
self:_done() self:_done()
end 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 cmd string
---@param opts ProcessOpts ---@param opts ProcessOpts
function Task:spawn(cmd, opts) function Task:spawn(cmd, opts)
@ -162,6 +184,8 @@ function Task:start()
self:clean() self:clean()
elseif self.type == "log" then elseif self.type == "log" then
self:log() self:log()
elseif self.type == "docs" then
self:docs()
end end
end) end)
@ -184,8 +208,14 @@ function Task:log()
"--decorate", "--decorate",
"--date=short", "--date=short",
"--color=never", "--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", { self:spawn("git", {
args = args, args = args,
cwd = self.plugin.dir, cwd = self.plugin.dir,
@ -209,14 +239,18 @@ function Task:update()
"--update-shallow", "--update-shallow",
"--progress", "--progress",
} }
local git = Util.git_info(self.plugin.dir) local git = assert(Util.git_info(self.plugin.dir))
self:spawn("git", { self:spawn("git", {
args = args, args = args,
cwd = self.plugin.dir, cwd = self.plugin.dir,
on_exit = function(ok) on_exit = function(ok)
if ok then 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) self.plugin.dirty = not vim.deep_equal(git, git_new)
end end
end, end,