summaryrefslogtreecommitdiff
path: root/lua/tjk/pluginloader.lua
blob: 92ed0af6f9a02fb1716da5d797c893afd2b8db2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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