refactor(pack): simplify plugin loading
This commit is contained in:
+25
-28
@@ -42,20 +42,35 @@ local function normalize_name(name)
|
|||||||
return name:lower()
|
return name:lower()
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin ow.Pack.Plugin
|
---@param name string
|
||||||
local function load(plugin)
|
---@return string?
|
||||||
local name = normalize_name(plugin.name)
|
local function plugin_config_path(name)
|
||||||
if not name then
|
local normalized = normalize_name(name)
|
||||||
log.error("Invalid plugin name: %s", plugin.name)
|
if not normalized then
|
||||||
|
log.error("Invalid plugin name: %s", name)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local path = vim.fs.joinpath(config_dir, "plugins", normalized .. ".lua")
|
||||||
|
|
||||||
|
return path
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param required boolean
|
||||||
|
local function load(name, required)
|
||||||
|
local path = plugin_config_path(name)
|
||||||
|
if not path then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local path = vim.fs.joinpath(config_dir, "plugins", name .. ".lua")
|
|
||||||
|
|
||||||
if vim.uv.fs_stat(path) then
|
if vim.uv.fs_stat(path) then
|
||||||
local ok, err = exec(path)
|
local ok, err = exec(path)
|
||||||
if not ok then
|
if not ok then
|
||||||
log.error("Failed to load %s: %s", plugin.name, err)
|
log.error("Failed to load %s: %s", name, err)
|
||||||
end
|
end
|
||||||
|
elseif required then
|
||||||
|
log.error("No config file found for %s", name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -162,25 +177,7 @@ end
|
|||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
function M.reload(name)
|
function M.reload(name)
|
||||||
local normalized = normalize_name(name)
|
load(name, true)
|
||||||
if not normalized then
|
|
||||||
log.error("Invalid plugin name: %s", name)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local path = string.format("%s/lua/plugins/%s.lua", config_dir, normalized)
|
|
||||||
|
|
||||||
if not vim.uv.fs_stat(path) then
|
|
||||||
log.error("No config file found for %s", name)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, err = exec(path)
|
|
||||||
if ok then
|
|
||||||
log.info("Reloaded %s", name)
|
|
||||||
else
|
|
||||||
log.error("Failed to reload %s: %s", name, err)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.watch()
|
function M.watch()
|
||||||
@@ -188,7 +185,7 @@ function M.watch()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local plugins_dir = vim.fs.joinpath(config_dir, "lua/plugins")
|
local plugins_dir = vim.fs.joinpath(config_dir, "plugins")
|
||||||
|
|
||||||
local err_msg, err_name
|
local err_msg, err_name
|
||||||
watcher, err_msg, err_name = vim.uv.new_fs_event()
|
watcher, err_msg, err_name = vim.uv.new_fs_event()
|
||||||
@@ -292,7 +289,7 @@ function M.setup(specs)
|
|||||||
if ev then
|
if ev then
|
||||||
process_events(plugin, ev)
|
process_events(plugin, ev)
|
||||||
end
|
end
|
||||||
load(plugin)
|
load(plugin.name, false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user