refine module support and package loading

This commit is contained in:
2025-06-24 01:48:30 -04:00
parent eba21a8021
commit 6bd6db3c02
6 changed files with 200 additions and 101 deletions

View File

@ -3,107 +3,16 @@ local git = require('dep.git')
local fs = require('dep.fs')
local packager = require('dep.package')
local h = require('dep.helpers')
local spec_man = require("dep.spec")
local modules = require("dep.modules")
local bench = require("dep.bench")
-- all functions for convenience
local M = {}
-- TODO: actually use this (ideally make a view that shows startuptime and
-- which plugins are currently loaded)
-- performance logging
local perf = {}
-- TODO: maybe add the ability to get a lockfile? it's useful to make a config
-- rebuildable, but idk if it's actually useful for a neovim config
-- (look into how ofter people who use lazy.nvim us it)
--- get execution time of a function
---@param name string name of performance output
---@param code function function to run
---@vararg any arguments for code
function M.benchmark(name, code, ...)
local start = os.clock()
code(...)
perf[name] = os.clock() - start
end
--- recurse over all packages and register them
---@param speclist speclist table of specs
---@param overrides spec? a package spec that is used to override options
function M.registertree(speclist, overrides)
overrides = overrides or {}
-- recurse the packages
local over = overrides
for _, spec in ipairs(speclist) do
-- make sure the overrides override and take into account the packages spec
---@diagnostic disable-next-line: missing-fields
over = {
pin = overrides.pin or spec.pin,
disable = overrides.disable or spec.disable
}
-- While a package can fail to load we just don't care, it will work itself
-- out. The goal is to make sure every plugin that can load does load, not
-- keep working plugins from loading because an unrelated one doesn't load.
packager:new(spec, over)
end
if speclist.modules then
for _, module in ipairs(speclist.modules) do
local name = "<unnamed module>"
if type(module) == "string" then
if speclist.modules.prefix then
if speclist.modules.prefix:sub(#speclist.modules.prefix) ~= "." and
module:sub(1, 2) ~= "." then
module = "."..module
end
module = speclist.modules.prefix..module
end
name, module = module, require(module)
end
name = module.name or name
-- allow a module to be a spec
if spec_man.check(module, true) ~= false then
---@diagnostic disable-next-line: cast-local-type
module = { module }
end
local ok, err = pcall(M.registertree, module, overrides)
if not ok then
error(string.format("%s <- %s", err, name))
end
end
end
end
--- reload all packages in package table spec
---@param force boolean? force all packages to load
function M.reload(force)
local reloaded = packager.get_root():loadtree(force)
if reloaded then
local ok, err
M.benchmark("reload", function()
ok, err = pcall(vim.cmd,
[[
silent! helptags ALL
silent! UpdateRemotePlugins
]])
end)
if ok then
logger:log("vim", "reloaded helptags and remote plugins")
else
logger:log("error",
"failed to reload helptags and remote plugins; reason: %s", err)
end
end
end
--- sync a tree of plugins
---@param tree package[] tree of plugins
---@param cb function? callback
@ -123,7 +32,9 @@ function M.synctree(tree, cb)
end
fs:clean(packager)
M.reload()
for _, package in pairs(tree) do
package:reload()
end
if cb then
cb()
@ -148,6 +59,7 @@ end
-- basically the main function of our program
return function(opts)
logger.pipe = logger:setup()
bench:setup()
--- make comparison for table.sort
---@param a package package spec a
@ -162,7 +74,7 @@ return function(opts)
local initialized, err = pcall(function()
packager.set_base_dir(opts.base_dir or vim.fn.stdpath("data").."/site/pack/deps/opt/")
M.benchmark("load", function()
bench.mark("load", function()
-- register all packages
local root = packager:new({
"squibid/dep",
@ -174,7 +86,12 @@ return function(opts)
logger:log("error", "couldn't register root package")
return
end
M.registertree(opts)
-- setup all packages and modules
if opts.modules then
modules:setup(opts)
end
packager.register_speclist(opts)
-- sort package dependencies
for _, package in pairs(packager.get_packages()) do
@ -190,7 +107,9 @@ return function(opts)
end)
-- load packages
M.reload(false)
for _, package in pairs(packager.get_packages()) do
package:reload()
end
--- check if a package should be synced
---@param package table package table spec
@ -244,13 +163,14 @@ return function(opts)
end, {})
vim.api.nvim_create_user_command("DepReload", function()
M.reload()
for _, package in pairs(packager.get_packages()) do
package:reload()
end
end, {})
vim.api.nvim_create_user_command("DepClean", function()
-- clean AND reload to make sure that all old packages are gone
fs:clean(packager)
M.reload()
end, {})
logger:cleanup()