Compare commits
2 Commits
eba21a8021
...
fd1c5d6e79
Author | SHA1 | Date | |
---|---|---|---|
fd1c5d6e79
|
|||
6bd6db3c02
|
118
lua/dep.lua
118
lua/dep.lua
@ -3,107 +3,16 @@ local git = require('dep.git')
|
|||||||
local fs = require('dep.fs')
|
local fs = require('dep.fs')
|
||||||
local packager = require('dep.package')
|
local packager = require('dep.package')
|
||||||
local h = require('dep.helpers')
|
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
|
-- all functions for convenience
|
||||||
local M = {}
|
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
|
-- 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
|
-- rebuildable, but idk if it's actually useful for a neovim config
|
||||||
-- (look into how ofter people who use lazy.nvim us it)
|
-- (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
|
--- sync a tree of plugins
|
||||||
---@param tree package[] tree of plugins
|
---@param tree package[] tree of plugins
|
||||||
---@param cb function? callback
|
---@param cb function? callback
|
||||||
@ -123,7 +32,9 @@ function M.synctree(tree, cb)
|
|||||||
end
|
end
|
||||||
|
|
||||||
fs:clean(packager)
|
fs:clean(packager)
|
||||||
M.reload()
|
for _, package in pairs(tree) do
|
||||||
|
package:reload()
|
||||||
|
end
|
||||||
|
|
||||||
if cb then
|
if cb then
|
||||||
cb()
|
cb()
|
||||||
@ -148,6 +59,7 @@ end
|
|||||||
-- basically the main function of our program
|
-- basically the main function of our program
|
||||||
return function(opts)
|
return function(opts)
|
||||||
logger.pipe = logger:setup()
|
logger.pipe = logger:setup()
|
||||||
|
bench:setup()
|
||||||
|
|
||||||
--- make comparison for table.sort
|
--- make comparison for table.sort
|
||||||
---@param a package package spec a
|
---@param a package package spec a
|
||||||
@ -162,7 +74,7 @@ return function(opts)
|
|||||||
|
|
||||||
local initialized, err = pcall(function()
|
local initialized, err = pcall(function()
|
||||||
packager.set_base_dir(opts.base_dir or vim.fn.stdpath("data").."/site/pack/deps/opt/")
|
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
|
-- register all packages
|
||||||
local root = packager:new({
|
local root = packager:new({
|
||||||
"squibid/dep",
|
"squibid/dep",
|
||||||
@ -174,7 +86,12 @@ return function(opts)
|
|||||||
logger:log("error", "couldn't register root package")
|
logger:log("error", "couldn't register root package")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
M.registertree(opts)
|
|
||||||
|
-- setup all packages and modules
|
||||||
|
if opts.modules then
|
||||||
|
modules:setup(opts)
|
||||||
|
end
|
||||||
|
packager.register_speclist(opts)
|
||||||
|
|
||||||
-- sort package dependencies
|
-- sort package dependencies
|
||||||
for _, package in pairs(packager.get_packages()) do
|
for _, package in pairs(packager.get_packages()) do
|
||||||
@ -190,7 +107,9 @@ return function(opts)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
-- load packages
|
-- load packages
|
||||||
M.reload(false)
|
for _, package in pairs(packager.get_packages()) do
|
||||||
|
package:reload()
|
||||||
|
end
|
||||||
|
|
||||||
--- check if a package should be synced
|
--- check if a package should be synced
|
||||||
---@param package table package table spec
|
---@param package table package table spec
|
||||||
@ -244,13 +163,14 @@ return function(opts)
|
|||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("DepReload", function()
|
vim.api.nvim_create_user_command("DepReload", function()
|
||||||
M.reload()
|
for _, package in pairs(packager.get_packages()) do
|
||||||
|
package:reload()
|
||||||
|
end
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("DepClean", function()
|
vim.api.nvim_create_user_command("DepClean", function()
|
||||||
-- clean AND reload to make sure that all old packages are gone
|
-- clean AND reload to make sure that all old packages are gone
|
||||||
fs:clean(packager)
|
fs:clean(packager)
|
||||||
M.reload()
|
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
logger:cleanup()
|
logger:cleanup()
|
||||||
|
35
lua/dep/bench.lua
Normal file
35
lua/dep/bench.lua
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
-- TODO: actually use this (ideally make a view that shows startuptime and
|
||||||
|
-- which plugins are currently loaded)
|
||||||
|
-- performance logging
|
||||||
|
|
||||||
|
---@class bench
|
||||||
|
---@field perf number[] list of all perfs
|
||||||
|
local bench = {}
|
||||||
|
local b
|
||||||
|
|
||||||
|
function bench:setup()
|
||||||
|
local o = {}
|
||||||
|
self = {}
|
||||||
|
self.__index = self
|
||||||
|
setmetatable(o, self)
|
||||||
|
|
||||||
|
o.perf = {}
|
||||||
|
o.inited = true
|
||||||
|
|
||||||
|
b = o
|
||||||
|
end
|
||||||
|
|
||||||
|
--- benchmark a peice of code
|
||||||
|
---@param name string the name of the benchmark
|
||||||
|
---@param f function the code to benchmark
|
||||||
|
---@vararg any args for f
|
||||||
|
---@return any ret the result of f
|
||||||
|
function bench.mark(name, f, ...)
|
||||||
|
local start = os.clock()
|
||||||
|
local ret = f(...)
|
||||||
|
b.perf[name] = os.clock() - start
|
||||||
|
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
return bench
|
39
lua/dep/modules/init.lua
Normal file
39
lua/dep/modules/init.lua
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
local module = require("dep.modules.module")
|
||||||
|
|
||||||
|
---@class modules
|
||||||
|
---@field modules module[] all modules in dep
|
||||||
|
local modules = {}
|
||||||
|
|
||||||
|
--- Initialize all the modules
|
||||||
|
---@param self table?
|
||||||
|
---@param speclist table
|
||||||
|
---@param overrides spec? overrides
|
||||||
|
---@return modules modules manager
|
||||||
|
---@nodisacard
|
||||||
|
function modules:setup(speclist, overrides)
|
||||||
|
overrides = overrides or {}
|
||||||
|
|
||||||
|
local o = {}
|
||||||
|
self = {}
|
||||||
|
self.__index = self
|
||||||
|
setmetatable(o, self)
|
||||||
|
|
||||||
|
-- create a list of modules
|
||||||
|
o.modules = {}
|
||||||
|
|
||||||
|
-- loop through all modules and initialize them
|
||||||
|
for _, modpath in ipairs(speclist.modules) do
|
||||||
|
local mod = module.new(
|
||||||
|
nil,
|
||||||
|
modpath,
|
||||||
|
speclist.modules.prefix,
|
||||||
|
overrides
|
||||||
|
)
|
||||||
|
|
||||||
|
table.insert(o.modules, mod)
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
return modules
|
55
lua/dep/modules/module.lua
Normal file
55
lua/dep/modules/module.lua
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
local spec_man = require("dep.spec")
|
||||||
|
local packager = require("dep.package")
|
||||||
|
|
||||||
|
---@class module
|
||||||
|
---@field name string name of the module
|
||||||
|
---@field path string path to the module
|
||||||
|
---@field mod table the module
|
||||||
|
local module = {}
|
||||||
|
|
||||||
|
--- Initialize a module
|
||||||
|
---@param self table?
|
||||||
|
---@param modpath string path to the module
|
||||||
|
---@param prefix string? the prefix to all modules
|
||||||
|
---@param overrides spec? a module override
|
||||||
|
---@return module|false module false on failure to load module
|
||||||
|
---@nodiscard
|
||||||
|
function module:new(modpath, prefix, overrides)
|
||||||
|
local ok, err
|
||||||
|
local o = {}
|
||||||
|
self = {}
|
||||||
|
self.__index = self
|
||||||
|
setmetatable(o, self)
|
||||||
|
|
||||||
|
o.name = "<unnamed module>"
|
||||||
|
if type(modpath) == "string" then
|
||||||
|
if prefix ~= nil then
|
||||||
|
if prefix:sub(#prefix) ~= "." and modpath:sub(1, 2) ~= "." then
|
||||||
|
modpath = "."..modpath
|
||||||
|
end
|
||||||
|
o.path = prefix..modpath
|
||||||
|
else
|
||||||
|
o.path = modpath
|
||||||
|
end
|
||||||
|
o.name = modpath
|
||||||
|
ok, o.mod = pcall(require, o.path)
|
||||||
|
if not ok then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
o.name = o.mod.name or o.name
|
||||||
|
|
||||||
|
-- allow a module to be a spec
|
||||||
|
if spec_man.check(o.mod, true) ~= false then
|
||||||
|
o.mod = { o.mod }
|
||||||
|
end
|
||||||
|
|
||||||
|
ok, err = pcall(packager.register_speclist, o.mod, overrides)
|
||||||
|
if not ok then
|
||||||
|
error(string.format("%s <- %s", err, o.name))
|
||||||
|
end
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
return module
|
@ -1,5 +1,6 @@
|
|||||||
local logger = require('dep.log')
|
local logger = require('dep.log')
|
||||||
local spec_man = require("dep.spec")
|
local spec_man = require("dep.spec")
|
||||||
|
local bench = require("dep.bench")
|
||||||
|
|
||||||
---@class package
|
---@class package
|
||||||
---@field id string id of the package
|
---@field id string id of the package
|
||||||
@ -443,4 +444,51 @@ function package.findcycle(pkgs)
|
|||||||
return false
|
return false
|
||||||
end
|
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 package.register_speclist(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.
|
||||||
|
package:new(spec, over)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- reload the package
|
||||||
|
---@param self package the package to reload
|
||||||
|
---@param force boolean? force all packages to load
|
||||||
|
function package:reload(force)
|
||||||
|
local reloaded = self:loadtree(force)
|
||||||
|
|
||||||
|
if reloaded then
|
||||||
|
local ok, err
|
||||||
|
-- TODO: make a benchmark function
|
||||||
|
bench.mark("reload", function()
|
||||||
|
ok, err = pcall(vim.cmd,
|
||||||
|
[[
|
||||||
|
silent! helptags ALL
|
||||||
|
silent! UpdateRemotePlugins
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
logger:log("error",
|
||||||
|
"failed to reload helptags and remote plugins; reason: %s", err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return package
|
return package
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
local logger = require("dep.log")
|
local logger = require("dep.log")
|
||||||
|
|
||||||
---@class modules
|
---@class specmodules
|
||||||
---@field prefix string prefix to prepend to the modules
|
---@field prefix string prefix to prepend to the modules
|
||||||
---@field [integer] string list of all modules to load
|
---@field [integer] string list of all modules to load
|
||||||
|
|
||||||
---@class speclist
|
---@class speclist
|
||||||
---@field modules modules a list of modules
|
---@field modules specmodules a list of modules
|
||||||
---@field [integer] spec a spec
|
---@field [integer] spec a spec
|
||||||
|
|
||||||
---@class spec
|
---@class spec
|
||||||
@ -53,6 +53,8 @@ function spec:correct_spec()
|
|||||||
repeat
|
repeat
|
||||||
if type(self[1]) ~= "string" then
|
if type(self[1]) ~= "string" then
|
||||||
self = self[1]
|
self = self[1]
|
||||||
|
elseif self[1] == nil then
|
||||||
|
break
|
||||||
end
|
end
|
||||||
until type(self[1]) == "string"
|
until type(self[1]) == "string"
|
||||||
end
|
end
|
||||||
|
43
lua/lazy/short.lua
Normal file
43
lua/lazy/short.lua
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
--- shorthands for when you only need to define one callback
|
||||||
|
local short = {}
|
||||||
|
|
||||||
|
--- create a usercommand which will trigger the plugin to load
|
||||||
|
---@param name string the name of the command
|
||||||
|
---@param opts vim.api.keyset.user_command? options
|
||||||
|
---@return function callback lazy setup
|
||||||
|
function short:cmd(name, opts)
|
||||||
|
return function(load)
|
||||||
|
load:cmd(name, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create an auto command which will trigger the plugin to load
|
||||||
|
---@param event string the event to trigger on
|
||||||
|
---@param opts vim.api.keyset.create_autocmd? options
|
||||||
|
---@return function callback lazy setup
|
||||||
|
function short:auto(event, opts)
|
||||||
|
return function(load)
|
||||||
|
load:auto(event, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create an auto command which will trigger on filetype
|
||||||
|
---@param filetype string filetype to register the auto on
|
||||||
|
function short:ft(filetype)
|
||||||
|
return function(load)
|
||||||
|
load:ft(filetype)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- create a keybind which will trigger the plugin to load
|
||||||
|
---@param mode string the mode to trigger in
|
||||||
|
---@param bind string the binding to use
|
||||||
|
---@param opts vim.keymap.set.Opts? options
|
||||||
|
---@return function callback lazy setup
|
||||||
|
function short:keymap(mode, bind, opts)
|
||||||
|
return function(load)
|
||||||
|
load:keymap(mode, bind, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return short
|
@ -46,7 +46,7 @@ function lazy:cmd(name, opts)
|
|||||||
table.insert(self.command_ids, name)
|
table.insert(self.command_ids, name)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- user an auto command which will trigger the plugin to load
|
--- create an auto command which will trigger the plugin to load
|
||||||
---@param event string the event to trigger on
|
---@param event string the event to trigger on
|
||||||
---@param opts vim.api.keyset.create_autocmd? options
|
---@param opts vim.api.keyset.create_autocmd? options
|
||||||
function lazy:auto(event, opts)
|
function lazy:auto(event, opts)
|
||||||
@ -60,6 +60,14 @@ function lazy:auto(event, opts)
|
|||||||
table.insert(self.auto_ids, vim.api.nvim_create_autocmd(event, opts))
|
table.insert(self.auto_ids, vim.api.nvim_create_autocmd(event, opts))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- create an auto command which will trigger on filetype
|
||||||
|
---@param filetype string filetype to register the auto on
|
||||||
|
function lazy:ft(filetype)
|
||||||
|
lazy:auto("FileType", {
|
||||||
|
pattern = filetype
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
--- create a keybind which will trigger the plugin to load
|
--- create a keybind which will trigger the plugin to load
|
||||||
---@param mode string the mode to trigger in
|
---@param mode string the mode to trigger in
|
||||||
---@param bind string the binding to use
|
---@param bind string the binding to use
|
||||||
|
Reference in New Issue
Block a user