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

229 lines
5.4 KiB
Lua
Raw Normal View History

local Config = require("lazy.core.config")
local Runner = require("lazy.manage.runner")
local Plugin = require("lazy.core.plugin")
2022-11-21 05:33:47 +08:00
local M = {}
2022-11-28 18:04:32 +08:00
---@class ManagerOpts
---@field wait? boolean
---@field clear? boolean
---@field show? boolean
2022-11-29 17:30:14 +08:00
---@field mode? string
---@field plugins? (LazyPlugin|string)[]
---@field concurrency? number
---@field lockfile? boolean
2022-11-21 05:33:47 +08:00
2022-11-28 18:04:32 +08:00
---@param ropts RunnerOpts
2022-11-21 05:33:47 +08:00
---@param opts? ManagerOpts
2022-11-28 18:04:32 +08:00
function M.run(ropts, opts)
2022-11-21 05:34:55 +08:00
opts = opts or {}
local mode = opts.mode
local event = mode and ("Lazy" .. mode:sub(1, 1):upper() .. mode:sub(2))
if event then
vim.api.nvim_exec_autocmds("User", { pattern = event .. "Pre", modeline = false })
end
if opts.plugins then
---@param plugin string|LazyPlugin
opts.plugins = vim.tbl_map(function(plugin)
return type(plugin) == "string" and Config.plugins[plugin] or plugin
end, vim.tbl_values(opts.plugins))
ropts.plugins = opts.plugins
end
ropts.concurrency = ropts.concurrency or opts.concurrency or Config.options.concurrency
2022-11-21 05:34:55 +08:00
if opts.clear then
M.clear(opts.plugins)
2022-11-21 05:34:55 +08:00
end
if opts.show ~= false then
vim.schedule(function()
2022-11-29 17:30:14 +08:00
require("lazy.view").show(opts.mode)
end)
2022-11-21 05:34:55 +08:00
end
---@type Runner
2022-11-28 18:04:32 +08:00
local runner = Runner.new(ropts)
runner:start()
2022-11-21 05:34:55 +08:00
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
2022-11-21 05:34:55 +08:00
2022-11-28 18:04:32 +08:00
-- wait for post-install to finish
2022-11-21 05:34:55 +08:00
runner:wait(function()
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
Plugin.update_state()
require("lazy.manage.checker").fast_check({ report = false })
if event then
vim.api.nvim_exec_autocmds("User", { pattern = event, modeline = false })
end
2022-11-21 05:34:55 +08:00
end)
if opts.wait then
runner:wait()
end
2022-11-29 07:15:13 +08:00
return runner
2022-11-21 05:33:47 +08:00
end
---@generic O: ManagerOpts
---@param opts? O
---@param defaults? ManagerOpts
---@return O
function M.opts(opts, defaults)
return vim.tbl_deep_extend("force", { clear = true }, defaults or {}, opts or {})
end
2022-11-21 05:33:47 +08:00
---@param opts? ManagerOpts
function M.install(opts)
opts = M.opts(opts, { mode = "install" })
return M.run({
pipeline = {
"git.clone",
{ "git.checkout", lockfile = opts.lockfile },
"plugin.docs",
"wait",
"plugin.build",
},
2022-11-28 18:04:32 +08:00
plugins = function(plugin)
2022-12-06 18:12:54 +08:00
return plugin.url and not plugin._.installed
2022-11-28 18:04:32 +08:00
end,
}, opts):wait(function()
require("lazy.manage.lock").update()
require("lazy.help").update()
end)
2022-11-21 05:33:47 +08:00
end
---@param opts? ManagerOpts
2022-11-21 05:33:47 +08:00
function M.update(opts)
opts = M.opts(opts, { mode = "update" })
return M.run({
pipeline = {
"git.origin",
"git.branch",
"git.fetch",
2022-11-29 07:15:13 +08:00
{ "git.checkout", lockfile = opts.lockfile },
"plugin.docs",
"wait",
"plugin.build",
{ "git.log", updated = true },
},
2022-11-28 18:04:32 +08:00
plugins = function(plugin)
2022-12-06 18:12:54 +08:00
return plugin.url and plugin._.installed
2022-11-28 18:04:32 +08:00
end,
2022-11-29 07:15:13 +08:00
}, opts):wait(function()
require("lazy.manage.lock").update()
require("lazy.help").update()
2022-11-29 07:15:13 +08:00
end)
2022-11-21 05:33:47 +08:00
end
--
---@param opts? ManagerOpts
function M.restore(opts)
opts = M.opts(opts, { mode = "restore", lockfile = true })
return M.update(opts)
end
2022-11-21 05:33:47 +08:00
---@param opts? ManagerOpts
function M.check(opts)
opts = M.opts(opts, { mode = "check" })
opts = opts or {}
return M.run({
pipeline = {
{ "git.origin", check = true },
"git.fetch",
"wait",
{ "git.log", check = true },
},
plugins = function(plugin)
2022-12-06 18:12:54 +08:00
return plugin.url and plugin._.installed
end,
}, opts)
end
---@param opts? ManagerOpts | {check?:boolean}
2022-11-23 04:12:50 +08:00
function M.log(opts)
opts = M.opts(opts, { mode = "log" })
return M.run({
pipeline = {
{ "git.origin", check = true },
{ "git.log", check = opts.check },
},
2022-11-28 18:04:32 +08:00
plugins = function(plugin)
2022-12-06 18:12:54 +08:00
return plugin.url and plugin._.installed
2022-11-28 18:04:32 +08:00
end,
}, opts)
2022-11-23 23:12:43 +08:00
end
---@param opts? ManagerOpts
function M.build(opts)
opts = M.opts(opts, { mode = "build" })
return M.run({
pipeline = { { "plugin.build", force = true } },
plugins = function()
return false
end,
}, opts)
end
---@param opts? ManagerOpts
function M.sync(opts)
opts = M.opts(opts)
if opts.clear then
M.clear()
opts.clear = false
end
if opts.show ~= false then
vim.schedule(function()
require("lazy.view").show("sync")
end)
opts.show = false
end
vim.api.nvim_exec_autocmds("User", { pattern = "LazySyncPre", modeline = false })
local clean_opts = vim.deepcopy(opts)
clean_opts.plugins = nil
local clean = M.clean(clean_opts)
local install = M.install(opts)
local update = M.update(opts)
clean:wait(function()
install:wait(function()
update:wait(function()
vim.api.nvim_exec_autocmds("User", { pattern = "LazySync", modeline = false })
end)
end)
end)
end
2022-11-21 05:33:47 +08:00
---@param opts? ManagerOpts
function M.clean(opts)
opts = M.opts(opts, { mode = "clean" })
return M.run({
pipeline = { "fs.clean" },
2022-11-28 18:04:32 +08:00
plugins = Config.to_clean,
}, opts):wait(function()
require("lazy.manage.lock").update()
end)
2022-11-21 05:33:47 +08:00
end
---@param plugins? LazyPlugin[]
function M.clear(plugins)
for _, plugin in pairs(plugins or Config.plugins) do
plugin._.updates = nil
plugin._.updated = nil
2022-11-29 07:15:13 +08:00
plugin._.cloned = nil
plugin._.dirty = 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
plugin._.tasks = vim.tbl_filter(function(task)
2022-11-28 18:04:32 +08:00
return task:is_running()
end, plugin._.tasks)
2022-11-21 05:34:55 +08:00
end
end
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
2022-11-21 05:33:47 +08:00
end
return M