PluginLoader: use directory name for enabled/disabled, deprecate metadata name
Presumably fairly similar to <https://github.com/OGKevin/koreader/commit/20ff60da3e7a776ce953e41e6912fe3eee3aa3ce> given the similar goal.
This commit is contained in:
1 parent
42bc2af95a
commit
eb6fb1d7d3
2 files changed
+171
-6
No files matched your search
@@ -14,7 +14,6 @@ Plugins are controlled by the following settings.
|
||||
local dbg = require("dbg")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local logger = require("logger")
|
||||
local util = require("util")
|
||||
local _ = require("gettext")
|
||||
|
||||
local DEFAULT_PLUGIN_PATH = "plugins"
|
||||
@@ -166,19 +165,19 @@ function PluginLoader:_discover()
|
||||
if mode == "directory" and entry:sub(-9) == ".koplugin" then
|
||||
local mainfile = plugin_root.."/main.lua"
|
||||
local metafile = plugin_root.."/_meta.lua"
|
||||
local plugin_name = entry:sub(1, -10)
|
||||
local disabled = false
|
||||
if plugins_disabled and plugins_disabled[entry:sub(1, -10)] then
|
||||
if plugins_disabled and plugins_disabled[plugin_name] then
|
||||
mainfile = metafile
|
||||
disabled = true
|
||||
end
|
||||
local __, name = util.splitFilePathName(plugin_root)
|
||||
|
||||
table.insert(discovered, {
|
||||
["main"] = mainfile,
|
||||
["meta"] = metafile,
|
||||
["path"] = plugin_root,
|
||||
["disabled"] = disabled,
|
||||
["name"] = name,
|
||||
["name"] = plugin_name,
|
||||
})
|
||||
end
|
||||
end
|
||||
@@ -204,14 +203,18 @@ function PluginLoader:_load(t)
|
||||
logger.warn("Error when loading", mainfile, plugin_module)
|
||||
elseif type(plugin_module.disabled) ~= "boolean" or not plugin_module.disabled then
|
||||
plugin_module.path = plugin_root
|
||||
plugin_module.name = plugin_module.name or plugin_root:match("/(.-)%.koplugin")
|
||||
plugin_module.name = v.name
|
||||
if disabled then
|
||||
table.insert(self.disabled_plugins, plugin_module)
|
||||
else
|
||||
local ok_meta, plugin_metamodule = pcall(dofile, metafile)
|
||||
if ok_meta and plugin_metamodule then
|
||||
for k, module in pairs(plugin_metamodule) do
|
||||
plugin_module[k] = module
|
||||
if k ~= "name" then
|
||||
plugin_module[k] = module
|
||||
else
|
||||
logger.warn("PluginLoader:", plugin_module.name, "name in _meta.lua, is deprecated and will be ignored.")
|
||||
end
|
||||
end
|
||||
end
|
||||
sandboxPluginEventHandlers(plugin_module)
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
describe("PluginLoader module", function()
|
||||
local PluginLoader, UIManager
|
||||
local original_lfs
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
UIManager = require("ui/uimanager")
|
||||
original_lfs = require("libs/libkoreader-lfs")
|
||||
end)
|
||||
|
||||
before_each(function()
|
||||
local lfs_mock = {
|
||||
dir_results = {},
|
||||
attributes_results = {},
|
||||
}
|
||||
|
||||
function lfs_mock.dir(path)
|
||||
local entries = lfs_mock.dir_results[path] or {}
|
||||
local index = 0
|
||||
return function()
|
||||
index = index + 1
|
||||
return entries[index]
|
||||
end
|
||||
end
|
||||
|
||||
function lfs_mock.attributes(path, key)
|
||||
local attrs = lfs_mock.attributes_results[path]
|
||||
if not attrs then
|
||||
return nil
|
||||
end
|
||||
if key then
|
||||
return attrs[key]
|
||||
end
|
||||
return attrs
|
||||
end
|
||||
|
||||
G_reader_settings:reset({})
|
||||
G_reader_settings:saveSetting("extra_plugin_paths", {})
|
||||
|
||||
package.replace("libs/libkoreader-lfs", lfs_mock)
|
||||
PluginLoader = package.reload("pluginloader")
|
||||
PluginLoader.show_info = true
|
||||
PluginLoader.enabled_plugins = {}
|
||||
PluginLoader.disabled_plugins = {}
|
||||
PluginLoader.loaded_plugins = {}
|
||||
PluginLoader.all_plugins = nil
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
package.replace("libs/libkoreader-lfs", original_lfs)
|
||||
package.unload("pluginloader")
|
||||
end)
|
||||
|
||||
it("discovers plugins by directory id and matches disabled settings against that id", function()
|
||||
local lfs_mock = require("libs/libkoreader-lfs")
|
||||
lfs_mock.dir_results.plugins = {
|
||||
".",
|
||||
"..",
|
||||
"pluginone.koplugin",
|
||||
"plugintwo.koplugin",
|
||||
}
|
||||
lfs_mock.attributes_results["plugins/pluginone.koplugin"] = { mode = "directory" }
|
||||
lfs_mock.attributes_results["plugins/plugintwo.koplugin"] = { mode = "directory" }
|
||||
|
||||
G_reader_settings:saveSetting("plugins_disabled", {
|
||||
plugintwo = true,
|
||||
})
|
||||
|
||||
local discovered = PluginLoader:_discover()
|
||||
|
||||
assert.equals("pluginone", discovered[1].name)
|
||||
assert.is_false(discovered[1].disabled)
|
||||
assert.equals("plugintwo", discovered[2].name)
|
||||
assert.is_true(discovered[2].disabled)
|
||||
end)
|
||||
|
||||
it("normalizes the internal id for enabled plugins", function()
|
||||
stub(_G, "dofile", function(path)
|
||||
if path == "plugins/test.koplugin/main.lua" then
|
||||
return {
|
||||
name = "wrong_main_name",
|
||||
description = "from main",
|
||||
}
|
||||
end
|
||||
if path == "plugins/test.koplugin/_meta.lua" then
|
||||
return {
|
||||
name = "wrong_meta_name",
|
||||
description = "from meta",
|
||||
}
|
||||
end
|
||||
error("unexpected path: " .. path)
|
||||
end)
|
||||
|
||||
PluginLoader:_load({ {
|
||||
main = "plugins/test.koplugin/main.lua",
|
||||
meta = "plugins/test.koplugin/_meta.lua",
|
||||
path = "plugins/test.koplugin",
|
||||
disabled = false,
|
||||
name = "test",
|
||||
} })
|
||||
|
||||
local plugin = PluginLoader.enabled_plugins[1]
|
||||
assert.equals("test", plugin.name)
|
||||
assert.equals("from meta", plugin.description)
|
||||
|
||||
_G.dofile:revert()
|
||||
end)
|
||||
|
||||
it("normalizes the internal id for disabled plugins", function()
|
||||
stub(_G, "dofile", function(path)
|
||||
if path == "plugins/test.koplugin/_meta.lua" then
|
||||
return {
|
||||
name = "wrong_meta_name",
|
||||
description = "from meta",
|
||||
}
|
||||
end
|
||||
error("unexpected path: " .. path)
|
||||
end)
|
||||
|
||||
PluginLoader:_load({ {
|
||||
main = "plugins/test.koplugin/_meta.lua",
|
||||
meta = "plugins/test.koplugin/_meta.lua",
|
||||
path = "plugins/test.koplugin",
|
||||
disabled = true,
|
||||
name = "test",
|
||||
} })
|
||||
|
||||
local plugin = PluginLoader.disabled_plugins[1]
|
||||
assert.equals("test", plugin.name)
|
||||
assert.equals("from meta", plugin.description)
|
||||
|
||||
_G.dofile:revert()
|
||||
end)
|
||||
|
||||
it("stores plugin manager toggles by internal plugin id", function()
|
||||
stub(PluginLoader, "loadPlugins", function()
|
||||
return {
|
||||
{
|
||||
name = "test",
|
||||
fullname = "Pretty Test",
|
||||
description = "from meta",
|
||||
path = "plugins/test.koplugin",
|
||||
},
|
||||
}, {}
|
||||
end)
|
||||
stub(PluginLoader, "getPluginInstance", function()
|
||||
return nil
|
||||
end)
|
||||
stub(UIManager, "askForRestart")
|
||||
|
||||
local plugin_items = PluginLoader:genPluginManagerSubItem()
|
||||
plugin_items[1].callback()
|
||||
|
||||
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled")
|
||||
assert.is_true(plugins_disabled.test)
|
||||
assert.is_nil(plugins_disabled["Pretty Test"])
|
||||
|
||||
UIManager.askForRestart:revert()
|
||||
PluginLoader.getPluginInstance:revert()
|
||||
PluginLoader.loadPlugins:revert()
|
||||
end)
|
||||
end)
|
||||
Reference in new issue
Block a user