2024-06-26 21:11:31 +08:00
|
|
|
local Async = require("lazy.async")
|
2024-06-27 03:38:28 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-27 18:02:28 +08:00
|
|
|
local Process = require("lazy.manage.process")
|
2024-06-27 03:38:28 +08:00
|
|
|
local Terminal = require("lazy.terminal")
|
|
|
|
|
|
|
|
local colors = Config.options.headless.colors
|
2022-11-28 18:04:32 +08:00
|
|
|
|
|
|
|
---@class LazyTaskDef
|
2022-11-29 05:03:44 +08:00
|
|
|
---@field skip? fun(plugin:LazyPlugin, opts?:TaskOptions):any?
|
2024-06-27 00:31:31 +08:00
|
|
|
---@field run async fun(task:LazyTask, opts:TaskOptions)
|
2022-11-28 18:04:32 +08:00
|
|
|
|
2024-06-26 23:06:56 +08:00
|
|
|
---@alias LazyTaskFn async fun(task:LazyTask, opts:TaskOptions)
|
2024-06-26 20:28:53 +08:00
|
|
|
|
2024-06-27 00:31:31 +08:00
|
|
|
---@class LazyMsg
|
|
|
|
---@field msg string
|
|
|
|
---@field level? number
|
|
|
|
|
2022-11-21 06:04:56 +08:00
|
|
|
---@class LazyTask
|
|
|
|
---@field plugin LazyPlugin
|
2022-11-29 05:03:44 +08:00
|
|
|
---@field name string
|
2024-06-27 00:31:31 +08:00
|
|
|
---@field private _log LazyMsg[]
|
2022-11-29 05:03:44 +08:00
|
|
|
---@field private _started? number
|
|
|
|
---@field private _ended? number
|
2022-11-28 18:04:32 +08:00
|
|
|
---@field private _opts TaskOptions
|
2024-06-26 23:06:56 +08:00
|
|
|
---@field private _running Async
|
2024-06-27 00:31:31 +08:00
|
|
|
---@field private _level number
|
2022-11-21 06:04:56 +08:00
|
|
|
local Task = {}
|
|
|
|
|
2022-11-29 05:03:44 +08:00
|
|
|
---@class TaskOptions: {[string]:any}
|
2022-11-28 14:35:58 +08:00
|
|
|
---@field on_done? fun(task:LazyTask)
|
2022-11-21 06:04:56 +08:00
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2022-11-29 05:03:44 +08:00
|
|
|
---@param name string
|
2022-11-23 23:12:43 +08:00
|
|
|
---@param opts? TaskOptions
|
2024-06-26 23:06:56 +08:00
|
|
|
---@param task LazyTaskFn
|
2022-11-29 05:03:44 +08:00
|
|
|
function Task.new(plugin, name, task, opts)
|
2024-06-26 23:06:56 +08:00
|
|
|
local self = setmetatable({}, { __index = Task })
|
2022-11-28 18:04:32 +08:00
|
|
|
self._opts = opts or {}
|
2024-06-27 00:31:31 +08:00
|
|
|
self._log = {}
|
2024-06-27 17:33:11 +08:00
|
|
|
self:set_level()
|
2022-11-21 06:04:56 +08:00
|
|
|
self.plugin = plugin
|
2022-11-29 05:03:44 +08:00
|
|
|
self.name = name
|
2022-12-17 21:28:41 +08:00
|
|
|
---@param other LazyTask
|
|
|
|
plugin._.tasks = vim.tbl_filter(function(other)
|
|
|
|
return other.name ~= name or other:is_running()
|
2024-06-26 20:28:53 +08:00
|
|
|
end, plugin._.tasks or {})
|
2022-11-28 18:19:50 +08:00
|
|
|
table.insert(plugin._.tasks, self)
|
2024-06-26 23:06:56 +08:00
|
|
|
self:_start(task)
|
2022-11-21 06:04:56 +08:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2024-06-27 00:31:31 +08: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-27 04:44:57 +08:00
|
|
|
local msg = ret and vim.trim(ret.msg) or ""
|
|
|
|
return msg ~= "" and msg or nil
|
2024-06-27 00:31:31 +08:00
|
|
|
end
|
|
|
|
|
2022-11-28 18:04:32 +08:00
|
|
|
function Task:has_started()
|
2022-11-29 05:03:44 +08:00
|
|
|
return self._started ~= nil
|
2022-11-21 06:04:56 +08:00
|
|
|
end
|
|
|
|
|
2024-06-26 20:28:53 +08:00
|
|
|
function Task:has_ended()
|
|
|
|
return self._ended ~= nil
|
|
|
|
end
|
|
|
|
|
2022-11-28 18:04:32 +08:00
|
|
|
function Task:is_running()
|
2024-06-26 23:06:56 +08:00
|
|
|
return not self:has_ended()
|
2022-11-21 06:04:56 +08:00
|
|
|
end
|
|
|
|
|
2024-06-27 00:31:31 +08:00
|
|
|
function Task:has_errors()
|
|
|
|
return self._level >= vim.log.levels.ERROR
|
|
|
|
end
|
|
|
|
|
|
|
|
function Task:has_warnings()
|
|
|
|
return self._level >= vim.log.levels.WARN
|
|
|
|
end
|
|
|
|
|
2024-06-27 17:33:11 +08:00
|
|
|
---@param level? number
|
|
|
|
function Task:set_level(level)
|
|
|
|
self._level = level or vim.log.levels.TRACE
|
|
|
|
end
|
|
|
|
|
2024-06-26 23:06:56 +08:00
|
|
|
---@private
|
|
|
|
---@param task LazyTaskFn
|
|
|
|
function Task:_start(task)
|
2024-06-26 20:28:53 +08:00
|
|
|
assert(not self:has_started(), "task already started")
|
|
|
|
assert(not self:has_ended(), "task already done")
|
|
|
|
|
2024-06-27 03:38:28 +08:00
|
|
|
if Config.headless() and Config.options.headless.task then
|
|
|
|
self:log("Running task " .. self.name, vim.log.levels.INFO)
|
|
|
|
end
|
|
|
|
|
2024-03-22 15:58:36 +08:00
|
|
|
self._started = vim.uv.hrtime()
|
2024-06-26 23:06:56 +08:00
|
|
|
---@async
|
|
|
|
self._running = Async.run(function()
|
|
|
|
task(self, self._opts)
|
|
|
|
end, {
|
|
|
|
on_done = function()
|
|
|
|
self:_done()
|
|
|
|
end,
|
|
|
|
on_error = function(err)
|
2024-06-27 00:31:31 +08:00
|
|
|
self:error(err)
|
2024-06-26 23:06:56 +08:00
|
|
|
end,
|
|
|
|
on_yield = function(res)
|
2024-06-27 00:31:31 +08:00
|
|
|
self:log(res)
|
2024-06-26 23:06:56 +08:00
|
|
|
end,
|
|
|
|
})
|
2022-11-21 06:04:56 +08:00
|
|
|
end
|
|
|
|
|
2024-06-25 19:23:25 +08:00
|
|
|
---@param msg string|string[]
|
2024-06-27 00:31:31 +08:00
|
|
|
---@param level? number
|
|
|
|
function Task:log(msg, level)
|
|
|
|
level = level or vim.log.levels.DEBUG
|
|
|
|
self._level = math.max(self._level or 0, level or 0)
|
2024-06-25 19:23:25 +08:00
|
|
|
msg = type(msg) == "table" and table.concat(msg, "\n") or msg
|
|
|
|
---@cast msg string
|
2024-06-27 00:31:31 +08:00
|
|
|
table.insert(self._log, { msg = msg, level = level })
|
2024-06-25 19:23:25 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
|
2024-06-27 03:38:28 +08:00
|
|
|
if Config.headless() then
|
|
|
|
self:headless()
|
|
|
|
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")
|
2024-06-25 19:23:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param msg string|string[]
|
2024-06-27 00:31:31 +08:00
|
|
|
function Task:error(msg)
|
|
|
|
self:log(msg, vim.log.levels.ERROR)
|
2024-06-25 19:23:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param msg string|string[]
|
2024-06-27 00:31:31 +08:00
|
|
|
function Task:warn(msg)
|
|
|
|
self:log(msg, vim.log.levels.WARN)
|
2024-06-25 19:23:25 +08:00
|
|
|
end
|
|
|
|
|
2024-06-26 20:28:53 +08:00
|
|
|
---@private
|
|
|
|
function Task:_done()
|
|
|
|
assert(self:has_started(), "task not started")
|
|
|
|
assert(not self:has_ended(), "task already done")
|
2024-06-26 21:11:31 +08:00
|
|
|
|
2024-06-26 23:06:56 +08:00
|
|
|
if self._running and self._running:running() then
|
|
|
|
return
|
2024-06-26 21:11:31 +08:00
|
|
|
end
|
|
|
|
|
2024-06-27 03:38:28 +08:00
|
|
|
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
|
2024-03-22 15:58:36 +08:00
|
|
|
self._ended = vim.uv.hrtime()
|
2022-11-28 18:04:32 +08:00
|
|
|
if self._opts.on_done then
|
|
|
|
self._opts.on_done(self)
|
2022-11-23 23:12:43 +08:00
|
|
|
end
|
2023-01-13 16:00:15 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
|
2022-11-28 18:04:32 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", {
|
2022-11-29 05:03:44 +08:00
|
|
|
pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2),
|
2022-11-28 18:04:32 +08:00
|
|
|
data = { plugin = self.plugin.name },
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-11-29 05:03:44 +08:00
|
|
|
function Task:time()
|
|
|
|
if not self:has_started() then
|
|
|
|
return 0
|
|
|
|
end
|
2024-06-26 20:28:53 +08:00
|
|
|
if not self:has_ended() then
|
2024-03-22 15:58:36 +08:00
|
|
|
return (vim.uv.hrtime() - self._started) / 1e6
|
2022-11-29 05:03:44 +08:00
|
|
|
end
|
|
|
|
return (self._ended - self._started) / 1e6
|
|
|
|
end
|
|
|
|
|
2024-06-26 20:28:53 +08:00
|
|
|
---@async
|
2022-11-21 06:04:56 +08:00
|
|
|
---@param cmd string
|
2022-11-28 18:04:32 +08:00
|
|
|
---@param opts? ProcessOpts
|
2022-11-21 06:04:56 +08:00
|
|
|
function Task:spawn(cmd, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local on_line = opts.on_line
|
|
|
|
local on_exit = opts.on_exit
|
|
|
|
|
2024-06-27 03:38:28 +08:00
|
|
|
local headless = Config.headless() and Config.options.headless.process
|
|
|
|
|
2022-11-21 06:04:56 +08:00
|
|
|
function opts.on_line(line)
|
2024-06-27 03:38:28 +08:00
|
|
|
if not headless then
|
|
|
|
return self:log(line, vim.log.levels.TRACE)
|
|
|
|
end
|
2022-11-21 06:04:56 +08:00
|
|
|
if on_line then
|
|
|
|
pcall(on_line, line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-27 19:06:39 +08:00
|
|
|
self._running:suspend()
|
|
|
|
|
2024-06-26 20:28:53 +08:00
|
|
|
local running = true
|
2024-06-27 17:33:11 +08:00
|
|
|
local ret = true
|
2022-11-28 18:04:32 +08:00
|
|
|
---@param output string
|
2022-11-21 06:04:56 +08:00
|
|
|
function opts.on_exit(ok, output)
|
2024-06-27 03:38:28 +08:00
|
|
|
if not headless then
|
|
|
|
self:log(vim.trim(output), ok and vim.log.levels.DEBUG or vim.log.levels.ERROR)
|
|
|
|
end
|
2022-11-21 06:04:56 +08:00
|
|
|
if on_exit then
|
|
|
|
pcall(on_exit, ok, output)
|
|
|
|
end
|
2024-06-27 17:33:11 +08:00
|
|
|
ret = ok
|
2024-06-26 20:28:53 +08:00
|
|
|
running = false
|
2024-06-27 19:06:39 +08:00
|
|
|
self._running:resume()
|
2024-06-26 20:28:53 +08:00
|
|
|
end
|
2024-06-27 03:38:28 +08:00
|
|
|
|
|
|
|
if headless then
|
|
|
|
opts.on_data = function(data)
|
|
|
|
-- prefix with plugin name
|
|
|
|
local prefix = self:prefix()
|
|
|
|
io.write(Terminal.prefix(data, prefix))
|
|
|
|
end
|
|
|
|
end
|
2024-06-26 20:28:53 +08:00
|
|
|
Process.spawn(cmd, opts)
|
2024-06-27 19:06:39 +08:00
|
|
|
coroutine.yield()
|
|
|
|
assert(not running, "process still running?")
|
2024-06-27 17:33:11 +08:00
|
|
|
return ret
|
2022-11-23 04:12:50 +08:00
|
|
|
end
|
2022-11-21 06:04:56 +08:00
|
|
|
|
2024-06-27 03:38:28 +08: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-28 18:04:32 +08:00
|
|
|
function Task:wait()
|
|
|
|
while self:is_running() do
|
|
|
|
vim.wait(10)
|
2022-11-21 06:04:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Task
|