lazy.nvim/lua/lazy/manage/task/init.lua

241 lines
5.8 KiB
Lua
Raw Permalink Normal View History

2024-06-26 14:11:31 +01:00
local Async = require("lazy.async")
local Config = require("lazy.core.config")
local Process = require("lazy.manage.process")
local Terminal = require("lazy.terminal")
local colors = Config.options.headless.colors
2022-11-28 10:04:32 +00:00
---@class LazyTaskDef
---@field skip? fun(plugin:LazyPlugin, opts?:TaskOptions):any?
2024-06-26 17:31:31 +01:00
---@field run async fun(task:LazyTask, opts:TaskOptions)
2022-11-28 10:04:32 +00:00
---@alias LazyTaskFn async fun(task:LazyTask, opts:TaskOptions)
2024-06-26 13:28:53 +01:00
2024-06-26 17:31:31 +01:00
---@class LazyMsg
---@field msg string
---@field level? number
2024-06-28 15:08:26 +01:00
---@class LazyTask: Async
2022-11-20 22:04:56 +00:00
---@field plugin LazyPlugin
---@field name string
2024-06-26 17:31:31 +01:00
---@field private _log LazyMsg[]
2024-06-28 15:08:26 +01:00
---@field private _started number
---@field private _ended? number
2022-11-28 10:04:32 +00:00
---@field private _opts TaskOptions
2024-06-26 17:31:31 +01:00
---@field private _level number
2024-06-28 15:08:26 +01:00
local Task = setmetatable({}, { __index = Async.Async })
2022-11-20 22:04:56 +00:00
---@class TaskOptions: {[string]:any}
2022-11-28 06:35:58 +00:00
---@field on_done? fun(task:LazyTask)
2022-11-20 22:04:56 +00:00
---@param plugin LazyPlugin
---@param name string
2022-11-23 15:12:43 +00:00
---@param opts? TaskOptions
---@param task LazyTaskFn
function Task.new(plugin, name, task, opts)
local self = setmetatable({}, { __index = Task })
2024-06-28 15:08:26 +01:00
---@async
Task.init(self, function()
self:_run(task)
end)
self:set_level()
2022-11-28 10:04:32 +00:00
self._opts = opts or {}
2024-06-26 17:31:31 +01:00
self._log = {}
2022-11-20 22:04:56 +00:00
self.plugin = plugin
self.name = name
2024-06-28 15:08:26 +01:00
self._started = vim.uv.hrtime()
---@param other LazyTask
plugin._.tasks = vim.tbl_filter(function(other)
2024-06-28 15:08:26 +01:00
return other.name ~= name or other:running()
2024-06-26 13:28:53 +01:00
end, plugin._.tasks or {})
table.insert(plugin._.tasks, self)
2024-06-28 16:44:21 +01:00
self:render()
2022-11-20 22:04:56 +00:00
return self
end
2024-06-26 17:31:31 +01:00
---@param level? number
---@return LazyMsg[]
function Task:get_log(level)
level = level or vim.log.levels.DEBUG
return vim.tbl_filter(function(msg)
return msg.level >= level
end, self._log)
end
---@param level? number
function Task:output(level)
return table.concat(
---@param m LazyMsg
vim.tbl_map(function(m)
return m.msg
end, self:get_log(level)),
"\n"
)
end
function Task:status()
local ret = self._log[#self._log]
2024-06-26 21:44:57 +01:00
local msg = ret and vim.trim(ret.msg) or ""
return msg ~= "" and msg or nil
2024-06-26 17:31:31 +01:00
end
function Task:has_errors()
return self._level >= vim.log.levels.ERROR
end
function Task:has_warnings()
return self._level >= vim.log.levels.WARN
end
---@param level? number
function Task:set_level(level)
self._level = level or vim.log.levels.TRACE
end
2024-06-28 15:08:26 +01:00
---@async
---@param task LazyTaskFn
2024-06-28 15:08:26 +01:00
function Task:_run(task)
if Config.headless() and Config.options.headless.task then
self:log("Running task " .. self.name, vim.log.levels.INFO)
end
2024-06-28 15:08:26 +01:00
self
:on("done", function()
self:_done()
2024-06-28 15:08:26 +01:00
end)
:on("error", function(err)
2024-06-26 17:31:31 +01:00
self:error(err)
2024-06-28 15:08:26 +01:00
end)
:on("yield", function(msg)
self:log(msg)
end)
task(self, self._opts)
2022-11-20 22:04:56 +00:00
end
---@param msg string|string[]|LazyMsg
2024-06-26 17:31:31 +01:00
---@param level? number
function Task:log(msg, level)
if type(msg) == "table" and msg.msg then
level = msg.level or level
msg = msg.msg
end
2024-06-26 17:31:31 +01:00
level = level or vim.log.levels.DEBUG
self._level = math.max(self._level or 0, level or 0)
msg = type(msg) == "table" and table.concat(msg, "\n") or msg
---@cast msg string
2024-06-26 17:31:31 +01:00
table.insert(self._log, { msg = msg, level = level })
2024-06-28 16:44:21 +01:00
self:render()
if Config.headless() then
self:headless()
end
end
2024-06-28 16:44:21 +01:00
function Task:render()
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
end)
end
function Task:headless()
if not Config.options.headless.log then
return
end
local msg = self._log[#self._log]
if not msg or msg.level == vim.log.levels.TRACE then
return
end
local map = {
[vim.log.levels.ERROR] = Terminal.red,
[vim.log.levels.WARN] = Terminal.yellow,
[vim.log.levels.INFO] = Terminal.blue,
}
local color = Config.options.headless.colors and map[msg.level]
io.write(Terminal.prefix(color and color(msg.msg) or msg.msg, self:prefix()))
io.write("\n")
end
---@param msg string|string[]
2024-06-26 17:31:31 +01:00
function Task:error(msg)
self:log(msg, vim.log.levels.ERROR)
end
---@param msg string|string[]
2024-06-26 17:31:31 +01:00
function Task:warn(msg)
self:log(msg, vim.log.levels.WARN)
end
2024-06-26 13:28:53 +01:00
---@private
function Task:_done()
if Config.headless() and Config.options.headless.task then
local ms = math.floor(self:time() + 0.5)
self:log("Finished task " .. self.name .. " in " .. ms .. "ms", vim.log.levels.INFO)
end
self._ended = vim.uv.hrtime()
2022-11-28 10:04:32 +00:00
if self._opts.on_done then
self._opts.on_done(self)
2022-11-23 15:12:43 +00:00
end
self:render()
2024-06-28 16:44:21 +01:00
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2),
data = { plugin = self.plugin.name },
})
end)
2022-11-28 10:04:32 +00:00
end
function Task:time()
2024-06-28 15:08:26 +01:00
return ((self._ended or vim.uv.hrtime()) - self._started) / 1e6
end
2024-06-26 13:28:53 +01:00
---@async
2022-11-20 22:04:56 +00:00
---@param cmd string
2022-11-28 10:04:32 +00:00
---@param opts? ProcessOpts
2022-11-20 22:04:56 +00:00
function Task:spawn(cmd, opts)
opts = opts or {}
local on_line = opts.on_line
local headless = Config.headless() and Config.options.headless.process
2022-11-20 22:04:56 +00:00
function opts.on_line(line)
if not headless then
return self:log(line, vim.log.levels.TRACE)
end
2022-11-20 22:04:56 +00:00
if on_line then
pcall(on_line, line)
end
end
if headless then
opts.on_data = function(data)
-- prefix with plugin name
2024-06-28 15:08:26 +01:00
io.write(Terminal.prefix(data, self:prefix()))
end
end
2024-06-28 15:08:26 +01:00
local proc = Process.spawn(cmd, opts)
proc:wait()
local ok = proc.code == 0 and proc.signal == 0
if not headless then
local msg = vim.trim(proc.data)
if #msg > 0 then
self:log(vim.trim(proc.data), ok and vim.log.levels.DEBUG or vim.log.levels.ERROR)
end
end
if opts.on_exit then
pcall(opts.on_exit, ok, proc.data)
end
2024-06-28 15:08:26 +01:00
return ok
2022-11-22 20:12:50 +00:00
end
2022-11-20 22:04:56 +00:00
function Task:prefix()
local plugin = "[" .. self.plugin.name .. "] "
local task = string.rep(" ", 20 - #(self.name .. self.plugin.name)) .. self.name
return colors and Terminal.magenta(plugin) .. Terminal.cyan(task) .. Terminal.bright_black(" | ")
or plugin .. " " .. task .. " | "
end
2022-11-20 22:04:56 +00:00
return Task