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

155 lines
3.2 KiB
Lua
Raw Normal View History

local Process = require("lazy.manage.process")
2022-11-28 18:04:32 +08:00
---@class LazyTaskDef
---@field needed? fun(plugin:LazyPlugin, opts:RunnerOpts):any?
---@field run fun(task:LazyTask)
---@alias LazyTaskState fun():boolean?
2022-11-21 06:04:56 +08:00
---@class LazyTask
---@field plugin LazyPlugin
---@field type TaskType
2022-11-28 18:04:32 +08:00
---@field output string
---@field status string
---@field error? string
---@field private _task fun(task:LazyTask)
---@field private _running LazyPluginState[]
---@field private _started boolean
---@field private _opts TaskOptions
2022-11-21 06:04:56 +08:00
local Task = {}
2022-11-23 23:12:43 +08:00
---@alias TaskType "update"|"install"|"run"|"clean"|"log"|"docs"
---@class TaskOptions
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
---@param type TaskType
2022-11-23 23:12:43 +08:00
---@param opts? TaskOptions
2022-11-28 18:04:32 +08:00
---@param task fun(task:LazyTask)
function Task.new(plugin, type, task, opts)
2022-11-21 06:04:56 +08:00
local self = setmetatable({}, {
__index = Task,
})
2022-11-28 18:04:32 +08:00
self._opts = opts or {}
self._running = {}
self._task = task
self._started = false
2022-11-21 06:04:56 +08:00
self.plugin = plugin
self.type = type
self.output = ""
self.status = ""
plugin._.tasks = plugin._.tasks or {}
table.insert(plugin._.tasks, self)
2022-11-21 06:04:56 +08:00
return self
end
2022-11-28 18:04:32 +08:00
function Task:has_started()
return self._started
2022-11-21 06:04:56 +08:00
end
2022-11-28 18:04:32 +08:00
function Task:is_done()
return self:has_started() and not self:is_running()
2022-11-21 06:04:56 +08:00
end
2022-11-28 18:04:32 +08:00
function Task:is_running()
for _, state in ipairs(self._running) do
if state() then
return true
2022-11-21 06:04:56 +08:00
end
end
2022-11-28 18:04:32 +08:00
return false
2022-11-21 06:04:56 +08:00
end
2022-11-28 18:04:32 +08:00
function Task:start()
self._started = true
---@type boolean, string|any
local ok, err = pcall(self._task, self)
if not ok then
self.error = err or "failed"
2022-11-21 06:04:56 +08:00
end
2022-11-28 18:04:32 +08:00
self:_check()
2022-11-21 06:04:56 +08:00
end
2022-11-28 18:04:32 +08:00
---@private
function Task:_check()
if self:is_running() then
return
end
if self._opts.on_done then
self._opts.on_done(self)
2022-11-23 23:12:43 +08:00
end
2022-11-28 18:04:32 +08:00
vim.cmd("do User LazyRender")
vim.api.nvim_exec_autocmds("User", {
pattern = "LazyPlugin" .. self.type:sub(1, 1):upper() .. self.type:sub(2),
data = { plugin = self.plugin.name },
})
end
---@param fn fun()
function Task:schedule(fn)
local done = false
table.insert(self._running, function()
return not done
end)
vim.schedule(function()
---@type boolean, string|any
local ok, err = pcall(fn)
if not ok then
self.error = err or "failed"
end
done = true
self:_check()
end)
2022-11-23 23:12:43 +08:00
end
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
function opts.on_line(line)
self.status = line
if on_line then
pcall(on_line, line)
end
vim.cmd("do User LazyRender")
end
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)
2022-11-28 18:04:32 +08:00
self.output = self.output .. output
2022-11-21 06:04:56 +08:00
if not ok then
2022-11-28 18:04:32 +08:00
self.error = self.error and (self.error .. "\n" .. output) or output
2022-11-21 06:04:56 +08:00
end
if on_exit then
pcall(on_exit, ok, output)
end
2022-11-28 18:04:32 +08:00
self:_check()
2022-11-21 06:04:56 +08:00
end
2022-11-28 18:04:32 +08:00
local proc = Process.spawn(cmd, opts)
table.insert(self._running, function()
return proc and not proc:is_closing()
2022-11-21 06:04:56 +08:00
end)
2022-11-23 04:12:50 +08:00
end
2022-11-21 06:04:56 +08:00
2022-11-28 18:04:32 +08:00
---@param tasks (LazyTask?)[]
function Task.all_done(tasks)
for _, task in ipairs(tasks) do
if task and not task:is_done() then
return false
end
2022-11-23 23:12:43 +08:00
end
2022-11-28 18:04:32 +08:00
return true
2022-11-21 06:04:56 +08:00
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