summaryrefslogtreecommitdiff
path: root/lua/tjk/pluginloader.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/tjk/pluginloader.lua')
-rw-r--r--lua/tjk/pluginloader.lua47
1 files changed, 47 insertions, 0 deletions
diff --git a/lua/tjk/pluginloader.lua b/lua/tjk/pluginloader.lua
new file mode 100644
index 0000000..92ed0af
--- /dev/null
+++ b/lua/tjk/pluginloader.lua
@@ -0,0 +1,47 @@
+local pluginsMod = "tjk.plugins" -- module containing plugin modules
+
+-- fancy plugin loader
+function require_plugin(moduleName, source, setup, loadFn)
+ local ok, mod = pcall(require, moduleName)
+ if not ok then
+ if not source then
+ vim.notify("Failed to load plugin module without source " .. moduleName, vim.log.levels.ERROR)
+ return
+ end
+ vim.pack.add { source }
+ ok, mod = pcall(require, moduleName)
+ if not ok then
+ vim.notify(mod, vim.log.levels.ERROR)
+ return
+ end
+ end
+ if setup and mod.setup then
+ mod.setup(setup == true and {} or setup)
+ end
+ if loadFn then
+ loadFn()
+ end
+end
+
+-- helper for handling dependencies and parsing module
+function require_plugin_from_module(mod)
+ -- mod == true means it returned nothing
+ if mod == true or mod.enabled == false then
+ return
+ end
+ for _, dMod in ipairs(mod.dependencies or {}) do
+ require_plugin_from_module(dMod)
+ end
+ require_plugin(mod.moduleName or mod[1], mod.source or mod[2], mod.setup, mod.loadFn)
+ for _, dMod in ipairs(mod.dependents or {}) do
+ require_plugin_from_module(dMod)
+ end
+end
+
+-- loop over plugins
+local pluginsPath = vim.fn.stdpath("config") .. "/lua/" .. pluginsMod:gsub("%.", "/")
+for _, f in ipairs(vim.fn.glob(pluginsPath .. "/*.lua", false, true)) do
+ local name = vim.fn.fnamemodify(f, ":t:r")
+ local modName = pluginsMod .. "." .. name
+ require_plugin_from_module(require(modName))
+end