Skip to content

Commit

Permalink
feat: task docs and options for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 23, 2022
1 parent 6f835ab commit fe6d0b1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
27 changes: 23 additions & 4 deletions lua/lazy/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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
Expand All @@ -142,6 +160,7 @@ function M.clear()
end
end
M.to_clean = {}
vim.cmd([[do User LazyRender]])
end

return M
46 changes: 40 additions & 6 deletions lua/lazy/task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit fe6d0b1

Please sign in to comment.