refactor: pipelines now always run sequential

This commit is contained in:
Folke Lemaitre 2022-11-28 13:10:52 +01:00
parent 2abdc681fa
commit 3768256956
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
5 changed files with 45 additions and 57 deletions

View File

@ -47,7 +47,7 @@ end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.install(opts) function M.install(opts)
M.run({ M.run({
pipeline = { "git.install", { "plugin.docs", "plugin.run" } }, pipeline = { "git.install", "plugin.docs", "plugin.run" },
plugins = function(plugin) plugins = function(plugin)
return plugin.uri and not plugin._.installed return plugin.uri and not plugin._.installed
end, end,
@ -57,7 +57,7 @@ end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.update(opts) function M.update(opts)
M.run({ M.run({
pipeline = { "git.update", { "plugin.docs", "plugin.run" }, "git.log" }, pipeline = { "git.update", "plugin.docs", "plugin.run", "wait", "git.log" },
plugins = function(plugin) plugins = function(plugin)
return plugin.uri and plugin._.installed return plugin.uri and plugin._.installed
end, end,

View File

@ -1,7 +1,7 @@
local Task = require("lazy.manage.task") local Task = require("lazy.manage.task")
local Config = require("lazy.core.config") local Config = require("lazy.core.config")
---@alias LazyPipeline (TaskType|TaskType[])[] ---@alias LazyPipeline TaskType[]
---@class RunnerOpts ---@class RunnerOpts
---@field pipeline LazyPipeline ---@field pipeline LazyPipeline
@ -38,57 +38,36 @@ end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param pipeline LazyPipeline ---@param pipeline LazyPipeline
function Runner:_run(plugin, pipeline) function Runner:_run(plugin, pipeline)
if #pipeline == 0 then ---@type TaskType
return local op = table.remove(pipeline, 1)
end if op == "wait" then
local ops = table.remove(pipeline, 1)
if ops == "wait" then
return table.insert(self._waiting, function() return table.insert(self._waiting, function()
self:_run(plugin, pipeline) self:_run(plugin, pipeline)
end) end)
end end
self:queue(plugin, op, function(task)
ops = type(ops) == "string" and { ops } or ops if not (task and task.error) and #pipeline > 0 then
---@cast ops TaskType[] self:_run(plugin, pipeline)
---@type LazyTask[]
local tasks = {}
local function on_done()
for _, task in ipairs(tasks) do
if task.error or not task:is_done() then
return
end
end end
self:_run(plugin, pipeline) end)
end
for _, op in ipairs(ops) do
local task = self:queue(plugin, op, { on_done = on_done })
if task then
table.insert(tasks, task)
end
end
for _, task in ipairs(tasks) do
task:start()
end
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param task_type TaskType ---@param task_type TaskType
---@param opts? TaskOptions ---@param on_done fun(task?:LazyTask)
---@return LazyTask? ---@return LazyTask?
function Runner:queue(plugin, task_type, opts) function Runner:queue(plugin, task_type, on_done)
local def = vim.split(task_type, ".", { plain = true }) local def = vim.split(task_type, ".", { plain = true })
assert(#def == 2) assert(#def == 2)
---@type LazyTaskDef ---@type LazyTaskDef
local task_def = require("lazy.manage.task." .. def[1])[def[2]] local task_def = require("lazy.manage.task." .. def[1])[def[2]]
assert(task_def) assert(task_def)
if not task_def.needed or task_def.needed(plugin, self._opts) then if not (task_def.skip and task_def.skip(plugin, self._opts)) then
local task = Task.new(plugin, def[2], task_def.run, opts) local task = Task.new(plugin, def[2], task_def.run, { on_done = on_done })
table.insert(self._tasks, task) table.insert(self._tasks, task)
return task task:start()
else
on_done()
end end
end end
@ -106,10 +85,11 @@ function Runner:start()
end end
end end
if #self._waiting > 0 then if #self._waiting > 0 then
for _, cb in ipairs(self._waiting) do local waiting = self._waiting
self._waiting = {}
for _, cb in ipairs(waiting) do
cb() cb()
end end
self._waiting = {}
return return
end end
check:stop() check:stop()

View File

@ -5,11 +5,11 @@ local Git = require("lazy.manage.git")
local M = {} local M = {}
M.log = { M.log = {
needed = function(plugin, opts) skip = function(plugin, opts)
if opts.interactive ~= true or not Util.file_exists(plugin.dir .. "/.git") then if not (opts.interactive and Util.file_exists(plugin.dir .. "/.git")) then
return false return false
end end
return plugin._.updated == nil or plugin._.updated.from ~= plugin._.updated.to return plugin._.updated and plugin._.updated.from == plugin._.updated.to
end, end,
run = function(self) run = function(self)
local args = { local args = {
@ -36,7 +36,11 @@ M.log = {
M.update = { M.update = {
run = function(self) run = function(self)
if Util.file_exists(self.plugin.uri) then if self.plugin._.is_local ~= self.plugin._.is_symlink then
-- FIXME: should change here and in install
error("incorrect local")
end
if self.plugin._.is_local then
if vim.loop.fs_realpath(self.plugin.uri) ~= vim.loop.fs_realpath(self.plugin.dir) then if vim.loop.fs_realpath(self.plugin.uri) ~= vim.loop.fs_realpath(self.plugin.dir) then
vim.loop.fs_unlink(self.plugin.dir) vim.loop.fs_unlink(self.plugin.dir)
vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, { vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, {
@ -47,7 +51,6 @@ M.update = {
else else
local args = { local args = {
"pull", "pull",
"--tags",
"--recurse-submodules", "--recurse-submodules",
"--update-shallow", "--update-shallow",
"--progress", "--progress",
@ -74,29 +77,23 @@ M.update = {
M.install = { M.install = {
run = function(self) run = function(self)
if Util.file_exists(self.plugin.uri) then if self.plugin._.is_local then
vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, { vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, { dir = true })
dir = true,
})
vim.opt.runtimepath:append(self.plugin.uri) vim.opt.runtimepath:append(self.plugin.uri)
else else
local args = { local args = {
"clone", "clone",
self.plugin.uri, self.plugin.uri,
-- "--depth=1",
"--filter=blob:none", "--filter=blob:none",
-- "--filter=tree:0",
"--recurse-submodules", "--recurse-submodules",
"--single-branch", "--single-branch",
"--shallow-submodules", "--shallow-submodules",
"--no-checkout",
"--progress", "--progress",
} }
if self.plugin.branch then if self.plugin.branch then
vim.list_extend(args, { vim.list_extend(args, { "-b", self.plugin.branch })
"-b",
self.plugin.branch,
})
end end
table.insert(args, self.plugin.dir) table.insert(args, self.plugin.dir)

View File

@ -1,7 +1,7 @@
local Runner = require("lazy.manage.runner") local Runner = require("lazy.manage.runner")
describe("runner", function() describe("runner", function()
local plugins = { { name = "plugin1" }, { name = "plugin2" } } local plugins = { { name = "plugin1", _ = {} }, { name = "plugin2", _ = {} } }
---@type {plugin:string, task:string}[] ---@type {plugin:string, task:string}[]
local runs = {} local runs = {}
@ -10,6 +10,11 @@ describe("runner", function()
end) end)
package.loaded["lazy.manage.task.test"] = {} package.loaded["lazy.manage.task.test"] = {}
package.loaded["lazy.manage.task.test"]["skip"] = {
skip = function()
return true
end,
}
for i = 1, 10 do for i = 1, 10 do
package.loaded["lazy.manage.task.test"]["test" .. i] = { package.loaded["lazy.manage.task.test"]["test" .. i] = {
---@param task LazyTask ---@param task LazyTask
@ -32,6 +37,12 @@ describe("runner", function()
assert.equal(4, #runs) assert.equal(4, #runs)
end) end)
it("handles skips", function()
local runner = Runner.new({ plugins = plugins, pipeline = { "test.test1", "test.skip", "test.test2" } })
runner:start()
assert.equal(4, #runs)
end)
it("aborts on error", function() it("aborts on error", function()
local runner = Runner.new({ plugins = plugins, pipeline = { "test.test1", "test.error1", "test.test2" } }) local runner = Runner.new({ plugins = plugins, pipeline = { "test.test1", "test.error1", "test.test2" } })
runner:start() runner:start()

View File

@ -1,7 +1,7 @@
local Task = require("lazy.manage.task") local Task = require("lazy.manage.task")
describe("task", function() describe("task", function()
local plugin = { name = "test" } local plugin = { name = "test", _ = {} }
local done = false local done = false
local error = nil local error = nil