2022-11-23 04:28:08 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-21 05:33:47 +08:00
|
|
|
local Task = require("lazy.task")
|
|
|
|
local Runner = require("lazy.runner")
|
2022-11-25 05:04:23 +08:00
|
|
|
local State = require("lazy.core.state")
|
2022-11-21 05:33:47 +08:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@alias ManagerOpts {wait?: boolean, plugins?: LazyPlugin[], clear?: boolean, show?: boolean}
|
|
|
|
|
|
|
|
---@param operation TaskType
|
|
|
|
---@param opts? ManagerOpts
|
|
|
|
---@param filter? fun(plugin:LazyPlugin):boolean?
|
|
|
|
function M.run(operation, opts, filter)
|
2022-11-21 05:34:55 +08:00
|
|
|
opts = opts or {}
|
|
|
|
local plugins = opts.plugins or Config.plugins
|
|
|
|
|
|
|
|
if opts.clear then
|
|
|
|
M.clear()
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.show then
|
|
|
|
require("lazy.view").show()
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type Runner
|
|
|
|
local runner = Runner.new()
|
|
|
|
|
|
|
|
local on_done = function()
|
|
|
|
vim.cmd([[do User LazyRender]])
|
|
|
|
end
|
|
|
|
|
|
|
|
-- install missing plugins
|
|
|
|
for _, plugin in pairs(plugins) do
|
|
|
|
if filter == nil or filter(plugin) then
|
|
|
|
runner:add(Task.new(plugin, operation))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if runner:is_empty() then
|
|
|
|
return on_done()
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.cmd([[do User LazyRender]])
|
|
|
|
|
|
|
|
-- wait for install to finish
|
|
|
|
runner:wait(function()
|
|
|
|
-- check if we need to do any post-install hooks
|
|
|
|
for _, plugin in ipairs(runner:plugins()) do
|
2022-11-23 23:12:43 +08:00
|
|
|
if plugin.dirty then
|
|
|
|
runner:add(Task.new(plugin, "docs"))
|
|
|
|
if plugin.opt == false or plugin.run then
|
|
|
|
runner:add(Task.new(plugin, "run"))
|
|
|
|
end
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
|
|
|
plugin.dirty = false
|
2022-11-23 23:12:43 +08:00
|
|
|
if opts.show and operation == "update" and plugin.updated and plugin.updated.from ~= plugin.updated.to then
|
|
|
|
runner:add(Task.new(plugin, "log", {
|
|
|
|
log = {
|
|
|
|
from = plugin.updated.from,
|
|
|
|
to = plugin.updated.to,
|
|
|
|
},
|
|
|
|
}))
|
2022-11-23 04:12:50 +08:00
|
|
|
end
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
|
|
|
-- wait for post-install to finish
|
|
|
|
runner:wait(on_done)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if opts.wait then
|
|
|
|
runner:wait()
|
|
|
|
end
|
|
|
|
return runner
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.install(opts)
|
2022-11-21 05:34:55 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
M.run("install", opts, function(plugin)
|
|
|
|
return plugin.uri and not plugin.installed
|
|
|
|
end)
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.update(opts)
|
2022-11-21 05:34:55 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
M.run("update", opts, function(plugin)
|
|
|
|
return plugin.uri and plugin.installed
|
|
|
|
end)
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-11-23 04:12:50 +08:00
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.log(opts)
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
M.run("log", opts, function(plugin)
|
|
|
|
return plugin.uri and plugin.installed
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-11-23 23:12:43 +08:00
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.docs(opts)
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
M.run("docs", opts, function(plugin)
|
|
|
|
return plugin.installed
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-11-21 05:33:47 +08:00
|
|
|
---@param opts? ManagerOpts
|
|
|
|
function M.clean(opts)
|
2022-11-21 07:27:28 +08:00
|
|
|
opts = opts or {}
|
2022-11-25 05:04:23 +08:00
|
|
|
State.update_state(true)
|
|
|
|
opts.plugins = vim.tbl_values(Config.to_clean)
|
2022-11-21 07:27:28 +08:00
|
|
|
M.run("clean", opts)
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.clear()
|
2022-11-21 07:27:28 +08:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-11-23 23:12:43 +08:00
|
|
|
-- clear updated status
|
|
|
|
plugin.updated = nil
|
2022-11-21 05:34:55 +08:00
|
|
|
-- clear finished tasks
|
|
|
|
if plugin.tasks then
|
2022-11-21 06:25:56 +08:00
|
|
|
---@param task LazyTask
|
2022-11-21 05:34:55 +08:00
|
|
|
plugin.tasks = vim.tbl_filter(function(task)
|
|
|
|
return task.running
|
|
|
|
end, plugin.tasks)
|
|
|
|
end
|
|
|
|
end
|
2022-11-23 23:12:43 +08:00
|
|
|
vim.cmd([[do User LazyRender]])
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|