2022-11-28 10:04:32 +00:00
|
|
|
local Task = require("lazy.manage.task")
|
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
|
|
|
|
---@class RunnerOpts
|
2022-11-28 21:03:44 +00:00
|
|
|
---@field pipeline (string|{[1]:string, [string]:any})[]
|
2022-11-28 10:04:32 +00:00
|
|
|
---@field plugins? LazyPlugin[]|fun(plugin:LazyPlugin):any?
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
---@alias PipelineStep {task:string, opts?:TaskOptions}
|
|
|
|
---@alias LazyRunnerTask {co:thread, status: {task?:LazyTask, waiting?:boolean}}
|
|
|
|
|
2022-11-20 22:04:56 +00:00
|
|
|
---@class Runner
|
2022-11-28 10:04:32 +00:00
|
|
|
---@field _plugins LazyPlugin[]
|
2022-11-28 21:03:44 +00:00
|
|
|
---@field _running LazyRunnerTask[]
|
|
|
|
---@field _pipeline PipelineStep[]
|
2022-11-28 10:04:32 +00:00
|
|
|
---@field _on_done fun()[]
|
|
|
|
---@field _opts RunnerOpts
|
2022-11-20 22:04:56 +00:00
|
|
|
local Runner = {}
|
|
|
|
|
2022-11-28 10:04:32 +00:00
|
|
|
---@param opts RunnerOpts
|
|
|
|
function Runner.new(opts)
|
|
|
|
local self = setmetatable({}, { __index = Runner })
|
|
|
|
self._opts = opts or {}
|
2022-11-20 22:04:56 +00:00
|
|
|
|
2022-11-28 10:04:32 +00:00
|
|
|
local plugins = self._opts.plugins
|
|
|
|
if type(plugins) == "function" then
|
|
|
|
self._plugins = vim.tbl_filter(plugins, Config.plugins)
|
|
|
|
else
|
|
|
|
self._plugins = plugins or Config.plugins
|
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
self._running = {}
|
2022-11-28 10:04:32 +00:00
|
|
|
self._on_done = {}
|
2022-11-28 21:03:44 +00:00
|
|
|
|
|
|
|
---@param step string|(TaskOptions|{[1]:string})
|
|
|
|
self._pipeline = vim.tbl_map(function(step)
|
|
|
|
return type(step) == "string" and { task = step } or { task = step[1], opts = step }
|
|
|
|
end, self._opts.pipeline)
|
|
|
|
|
2022-11-20 22:04:56 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
---@param entry LazyRunnerTask
|
|
|
|
function Runner:_resume(entry)
|
|
|
|
if entry.status.task and not entry.status.task:is_done() then
|
|
|
|
return true
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
local ok, status = coroutine.resume(entry.co)
|
|
|
|
entry.status = ok and status
|
|
|
|
return entry.status ~= nil
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
function Runner:resume(waiting)
|
|
|
|
local running = false
|
|
|
|
for _, entry in ipairs(self._running) do
|
|
|
|
if entry.status then
|
|
|
|
if waiting and entry.status.waiting then
|
|
|
|
entry.status.waiting = false
|
|
|
|
end
|
|
|
|
if not entry.status.waiting and self:_resume(entry) then
|
|
|
|
running = true
|
|
|
|
end
|
|
|
|
end
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
2022-11-28 21:03:44 +00:00
|
|
|
return running or (not waiting and self:resume(true))
|
2022-11-20 22:04:56 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 10:04:32 +00:00
|
|
|
function Runner:start()
|
|
|
|
for _, plugin in pairs(self._plugins) do
|
2022-11-28 21:03:44 +00:00
|
|
|
local co = coroutine.create(self.run_pipeline)
|
|
|
|
local ok, status = coroutine.resume(co, self, plugin)
|
|
|
|
if ok then
|
|
|
|
table.insert(self._running, { co = co, status = status })
|
|
|
|
end
|
2022-11-28 10:04:32 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
local check = vim.loop.new_check()
|
2022-11-28 10:04:32 +00:00
|
|
|
check:start(function()
|
2022-11-28 21:03:44 +00:00
|
|
|
if self:resume() then
|
2022-11-28 10:04:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
check:stop()
|
2022-11-28 21:03:44 +00:00
|
|
|
self._running = {}
|
2022-11-28 10:04:32 +00:00
|
|
|
for _, cb in ipairs(self._on_done) do
|
|
|
|
vim.schedule(cb)
|
|
|
|
end
|
|
|
|
self._on_done = {}
|
|
|
|
end)
|
2022-11-20 22:04:56 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
---@async
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function Runner:run_pipeline(plugin)
|
|
|
|
for _, step in ipairs(self._pipeline) do
|
|
|
|
if step.task == "wait" then
|
|
|
|
coroutine.yield({ waiting = true })
|
|
|
|
else
|
|
|
|
local task = self:queue(plugin, step.task, step.opts)
|
|
|
|
if task then
|
|
|
|
coroutine.yield({ task = task })
|
|
|
|
assert(task:is_done())
|
|
|
|
if task.error then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-11-20 22:04:56 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 21:03:44 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param task_type string
|
|
|
|
---@param task_opts? TaskOptions
|
|
|
|
---@return LazyTask?
|
|
|
|
function Runner:queue(plugin, task_type, task_opts)
|
|
|
|
assert(self._running)
|
|
|
|
local def = vim.split(task_type, ".", { plain = true })
|
|
|
|
---@type LazyTaskDef
|
|
|
|
local task_def = require("lazy.manage.task." .. def[1])[def[2]]
|
|
|
|
assert(task_def)
|
|
|
|
if not (task_def.skip and task_def.skip(plugin, task_opts)) then
|
|
|
|
local task = Task.new(plugin, def[2], task_def.run, task_opts)
|
|
|
|
task:start()
|
|
|
|
return task
|
|
|
|
end
|
2022-11-20 22:04:56 +00:00
|
|
|
end
|
|
|
|
|
2022-11-28 10:04:32 +00:00
|
|
|
-- Execute the callback async when done.
|
|
|
|
-- When no callback is specified, this will wait sync
|
2022-11-20 22:04:56 +00:00
|
|
|
---@param cb? fun()
|
|
|
|
function Runner:wait(cb)
|
2022-11-28 21:03:44 +00:00
|
|
|
if #self._running == 0 then
|
2022-11-20 22:04:56 +00:00
|
|
|
return cb and cb()
|
|
|
|
end
|
|
|
|
|
2022-11-28 10:04:32 +00:00
|
|
|
if cb then
|
|
|
|
table.insert(self._on_done, cb)
|
|
|
|
else
|
|
|
|
-- sync wait
|
2022-11-28 21:03:44 +00:00
|
|
|
while #self._running > 0 do
|
|
|
|
vim.wait(10)
|
2022-11-20 22:04:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Runner
|