feat: lots of improvements to pipeline runner and converted all tasks to new system

This commit is contained in:
Folke Lemaitre 2022-11-28 22:03:44 +01:00
parent 4de10f9578
commit fb84c081b0
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
13 changed files with 381 additions and 200 deletions

View file

@ -0,0 +1,50 @@
local Util = require("lazy.util")
---@type table<string, LazyTaskDef>
local M = {}
M.clean = {
run = function(self)
local dir = self.plugin.dir:gsub("/+$", "")
local stat = vim.loop.fs_lstat(dir)
if stat.type == "directory" then
Util.walk(dir, function(path, _, type)
if type == "directory" then
vim.loop.fs_rmdir(path)
else
vim.loop.fs_unlink(path)
end
end)
vim.loop.fs_rmdir(dir)
else
vim.loop.fs_unlink(dir)
end
self.plugin._.installed = false
end,
}
M.symlink = {
skip = function(plugin)
if not plugin._.is_local then
return true
end
return not plugin._.is_symlink and plugin._.installed
end,
run = function(self)
local stat = vim.loop.fs_lstat(self.plugin.dir)
if stat then
assert(stat.type == "link")
if vim.loop.fs_realpath(self.plugin.uri) == vim.loop.fs_realpath(self.plugin.dir) then
return
else
vim.loop.fs_unlink(self.plugin.dir)
end
end
vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, { dir = true })
vim.opt.runtimepath:append(self.plugin.uri)
end,
}
return M

View file

@ -5,13 +5,15 @@ local Git = require("lazy.manage.git")
local M = {}
M.log = {
---@param opts {since?: string, updated?:boolean}
skip = function(plugin, opts)
if not (opts.interactive and Util.file_exists(plugin.dir .. "/.git")) then
return false
if opts.updated and not (plugin._.updated and plugin._.updated.from ~= plugin._.updated.to) then
return true
end
return plugin._.updated and plugin._.updated.from == plugin._.updated.to
return not Util.file_exists(plugin.dir .. "/.git")
end,
run = function(self)
---@param opts {since?: string, updated?:boolean}
run = function(self, opts)
local args = {
"log",
"--pretty=format:%h %s (%cr)",
@ -21,10 +23,10 @@ M.log = {
"--color=never",
}
if self.plugin._.updated then
if opts.updated then
table.insert(args, self.plugin._.updated.from .. ".." .. (self.plugin._.updated.to or "HEAD"))
else
table.insert(args, "--since=7 days ago")
table.insert(args, "--since=" .. (opts.since or "7 days ago"))
end
self:spawn("git", {
@ -34,79 +36,125 @@ M.log = {
end,
}
M.update = {
M.clone = {
skip = function(plugin)
return plugin._.installed or plugin._.is_local
end,
run = function(self)
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
vim.loop.fs_unlink(self.plugin.dir)
vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, {
dir = true,
})
vim.opt.runtimepath:append(self.plugin.uri)
end
else
local args = {
"pull",
"--recurse-submodules",
"--update-shallow",
"--progress",
}
local git = assert(Git.info(self.plugin.dir))
local args = {
"clone",
self.plugin.uri,
"--filter=blob:none",
"--recurse-submodules",
"--single-branch",
"--shallow-submodules",
"--no-checkout",
"--progress",
}
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
on_exit = function(ok)
if ok then
local git_new = assert(Git.info(self.plugin.dir))
self.plugin._.updated = {
from = git.commit,
to = git_new.commit,
}
self.plugin._.dirty = not vim.deep_equal(git, git_new)
end
end,
})
if self.plugin.branch then
vim.list_extend(args, { "-b", self.plugin.branch })
end
table.insert(args, self.plugin.dir)
self:spawn("git", {
args = args,
on_exit = function(ok)
if ok then
self.plugin._.cloned = true
self.plugin._.installed = true
self.plugin._.dirty = true
end
end,
})
end,
}
M.install = {
run = function(self)
if self.plugin._.is_local then
vim.loop.fs_symlink(self.plugin.uri, self.plugin.dir, { dir = true })
vim.opt.runtimepath:append(self.plugin.uri)
else
local args = {
"clone",
self.plugin.uri,
"--filter=blob:none",
"--recurse-submodules",
"--single-branch",
"--shallow-submodules",
"--no-checkout",
"--progress",
}
if self.plugin.branch then
vim.list_extend(args, { "-b", self.plugin.branch })
end
table.insert(args, self.plugin.dir)
self:spawn("git", {
args = args,
on_exit = function(ok)
if ok then
self.plugin._.installed = true
self.plugin._.dirty = true
end
end,
})
M.branch = {
skip = function(plugin)
if not plugin._.installed or plugin._.is_local then
return true
end
local branch = assert(Git.get_branch(plugin))
return branch and branch.commit
end,
run = function(self)
local branch = assert(Git.get_branch(self.plugin))
local args = {
"remote",
"set-branches",
"--add",
"origin",
branch.branch,
}
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end,
}
M.fetch = {
skip = function(plugin)
return not plugin._.installed or plugin._.is_local
end,
run = function(self)
local args = {
"fetch",
"--recurse-submodules",
"--update-shallow",
"--progress",
}
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
})
end,
}
M.checkout = {
skip = function(plugin)
return not plugin._.installed or plugin._.is_local
end,
run = function(self)
local info = assert(Git.info(self.plugin.dir))
local target = assert(Git.get_target(self.plugin))
if not self.plugin._.cloned and info.commit == target.commit then
return
end
local args = {
"checkout",
"--progress",
}
if target.tag then
table.insert(args, "tags/" .. target.tag)
elseif self.plugin.commit then
table.insert(args, self.plugin.commit)
elseif target.branch then
table.insert(args, target.branch)
end
self:spawn("git", {
args = args,
cwd = self.plugin.dir,
on_exit = function(ok)
if ok then
local new_info = assert(Git.info(self.plugin.dir))
if not self.plugin._.cloned then
self.plugin._.updated = {
from = info.commit,
to = new_info.commit,
}
end
self.plugin._.dirty = true
end
end,
})
end,
}
return M

View file

@ -1,42 +1,42 @@
local Process = require("lazy.manage.process")
---@class LazyTaskDef
---@field skip? fun(plugin:LazyPlugin, opts:RunnerOpts):any?
---@field run fun(task:LazyTask)
---@field skip? fun(plugin:LazyPlugin, opts?:TaskOptions):any?
---@field run fun(task:LazyTask, opts:TaskOptions)
---@alias LazyTaskState fun():boolean?
---@class LazyTask
---@field plugin LazyPlugin
---@field type TaskType
---@field name string
---@field type string
---@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 _started? number
---@field private _ended? number
---@field private _opts TaskOptions
local Task = {}
---@alias TaskType "update"|"install"|"run"|"clean"|"log"|"docs"
---@class TaskOptions
---@class TaskOptions: {[string]:any}
---@field on_done? fun(task:LazyTask)
---@param plugin LazyPlugin
---@param type TaskType
---@param name string
---@param opts? TaskOptions
---@param task fun(task:LazyTask)
function Task.new(plugin, type, task, opts)
function Task.new(plugin, name, task, opts)
local self = setmetatable({}, {
__index = Task,
})
self._opts = opts or {}
self._running = {}
self._task = task
self._started = false
self._started = nil
self.plugin = plugin
self.type = type
self.name = name
self.output = ""
self.status = ""
plugin._.tasks = plugin._.tasks or {}
@ -45,7 +45,7 @@ function Task.new(plugin, type, task, opts)
end
function Task:has_started()
return self._started
return self._started ~= nil
end
function Task:is_done()
@ -62,9 +62,14 @@ function Task:is_running()
end
function Task:start()
self._started = true
if vim.in_fast_event() then
return vim.schedule(function()
self:start()
end)
end
self._started = vim.loop.hrtime()
---@type boolean, string|any
local ok, err = pcall(self._task, self)
local ok, err = pcall(self._task, self, self._opts)
if not ok then
self.error = err or "failed"
end
@ -76,16 +81,27 @@ function Task:_check()
if self:is_running() then
return
end
self._ended = vim.loop.hrtime()
if self._opts.on_done then
self._opts.on_done(self)
end
vim.cmd("do User LazyRender")
vim.api.nvim_exec_autocmds("User", {
pattern = "LazyPlugin" .. self.type:sub(1, 1):upper() .. self.type:sub(2),
pattern = "LazyPlugin" .. self.name:sub(1, 1):upper() .. self.name:sub(2),
data = { plugin = self.plugin.name },
})
end
function Task:time()
if not self:has_started() then
return 0
end
if not self:is_done() then
return (vim.loop.hrtime() - self._started) / 1e6
end
return (self._ended - self._started) / 1e6
end
---@param fn fun()
function Task:schedule(fn)
local done = false

View file

@ -29,28 +29,6 @@ M.run = {
end,
}
M.clean = {
run = function(self)
local dir = self.plugin.dir:gsub("/+$", "")
local stat = vim.loop.fs_lstat(dir)
if stat.type == "directory" then
Util.walk(dir, function(path, _, type)
if type == "directory" then
vim.loop.fs_rmdir(path)
else
vim.loop.fs_unlink(path)
end
end)
vim.loop.fs_rmdir(dir)
else
vim.loop.fs_unlink(dir)
end
self.plugin._.installed = false
end,
}
M.docs = {
skip = function(plugin)
return not plugin._.dirty