From 5c68815ee523bb6fff7d285016e57efbc88d483a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 5 Aug 2026 23:57:23 +0800 Subject: rewrite to install plugin from xmake-repo --- xmake/plugins/plugin/main.lua | 53 +++++++++++-------------------------------- 1 file changed, 13 insertions(+), 40 deletions(-) (limited to 'xmake/plugins/plugin/main.lua') diff --git a/xmake/plugins/plugin/main.lua b/xmake/plugins/plugin/main.lua index 5158352c0..fde93547a 100644 --- a/xmake/plugins/plugin/main.lua +++ b/xmake/plugins/plugin/main.lua @@ -24,6 +24,7 @@ import("core.base.global") import("core.package.repository") import("devel.git") import("private.action.require.impl.environment") +import("private.action.require.impl.install_packages") -- validate a plugin directory name function _check_plugin_name(name) @@ -42,36 +43,18 @@ function _repositories() return table.join(repository.repositories({global = false}), repository.repositories({global = true})) end --- find a plugin directory in the given repository directory --- --- plugins in a repository follow the same layout as packages: --- /plugins///xmake.lua -function _find_plugin_in_repo(repodir, name) - local dir = path.join(repodir, "plugins", name:sub(1, 1):lower(), name) - if os.isdir(dir) and os.isfile(path.join(dir, "xmake.lua")) then - return dir - end -end - -- install a plugin from the given repository or the first repository containing it function _install_plugins_from_repo(name, reponame) + + -- check plugin name _check_plugin_name(name) - for _, repo in ipairs(_repositories()) do - if not reponame or repo:name() == reponame then - local srcdir = _find_plugin_in_repo(repo:directory(), name) - if srcdir then - local dstdir = _get_plugindir(name) - assert(not os.isdir(dstdir), "plugin(%s) already exists!", name) - os.vcp(srcdir, dstdir) - cprint("${color.success}install ${bright}%s${clear} from repository ${bright}%s${clear} ok!", name, repo:name()) - return - end - end - end + + -- do install + local installname = name if reponame then - raise("plugin(%s): not found in repository %s!", name, reponame) + installname = reponame .. "@" .. name end - raise("plugin(%s): not found in any repository! try ${bright}xrepo update-repo${clear} first.", name) + os.execv(os.programfile(), {"lua", "private.xrepo", "install", "--plugin", installname}) end -- install a single plugin from a source directory (as the given name, default to the directory name) @@ -137,20 +120,9 @@ end function _install() local names = assert(option.get("plugins"), "please specify the plugins to be installed!") environment.enter() - try - { - function () - for _, name in ipairs(names) do - _install_one(name) - end - end, - catch - { - function (errors) - raise(errors) - end - } - } + for _, name in ipairs(names) do + _install_one(name) + end environment.leave() end @@ -171,7 +143,8 @@ function _plugin_description(dir) if os.isfile(filepath) then local content = io.readfile(filepath) if content then - return content:match("description%s*=%s*\"(.-)\"") + -- parse description from task or package scope + return content:match("description%s*=%s*\"(.-)\"") or content:match("set_description%s*%(\"(.-)\"%)") end end end -- cgit v1.3.1 From 59dd47f6429a1162b9cdd242a9dfcfecdfedb1db Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 6 Aug 2026 00:14:19 +0800 Subject: fix plugin test --- tests/plugins/repository/test.lua | 25 +++++++++++++++++++------ xmake/core/package/package.lua | 2 +- xmake/plugins/plugin/main.lua | 14 +++++++++++++- 3 files changed, 33 insertions(+), 8 deletions(-) (limited to 'xmake/plugins/plugin/main.lua') diff --git a/tests/plugins/repository/test.lua b/tests/plugins/repository/test.lua index f1cfba8c7..7b1619d5e 100644 --- a/tests/plugins/repository/test.lua +++ b/tests/plugins/repository/test.lua @@ -11,6 +11,19 @@ task("%s") io.writefile(path.join(dir, "main.lua"), string.format([[function main() print("%s") end]], name)) end +-- write a plugin package description, the plugin sources are placed in its `src` directory +-- +-- plugins in a repository are described as packages, e.g. /plugins///xmake.lua +function _write_plugin_package(dir, name) + io.writefile(path.join(dir, "xmake.lua"), string.format([[ +package("%s") + set_kind("plugin") + set_description("say hello from %s") + set_sourcedir(path.join(os.scriptdir(), "src")) +]], name, name)) + _write_plugin(path.join(dir, "src"), name) +end + -- create a temporary plugin repository (packages layout: plugins//) and register it -- -- @return reponame, names, cleanup @@ -21,7 +34,7 @@ function _mock_repo(basenames) local names = {} for _, base in ipairs(basenames) do local name = base .. "-" .. suffix - _write_plugin(path.join(repodir, "plugins", name:sub(1, 1), name), name) + _write_plugin_package(path.join(repodir, "plugins", name:sub(1, 1), name), name) table.insert(names, name) end @@ -52,12 +65,12 @@ function test_install_from_repo(t) local name = names[1] -- install by plain name (searched across all repositories) - os.runv("xmake", {"plugin", "--install", name}) + os.runv("xmake", {"plugin", "--install", "-y", name}) t:require(os.iorunv("xmake", {name}):find(name, 1, true)) -- reinstall by repo@name os.runv("xmake", {"plugin", "--remove", name}) - os.runv("xmake", {"plugin", "--install", reponame .. "@" .. name}) + os.runv("xmake", {"plugin", "--install", "-y", reponame .. "@" .. name}) t:require(os.iorunv("xmake", {name}):find(name, 1, true)) os.runv("xmake", {"plugin", "--remove", name}) @@ -86,7 +99,7 @@ function test_list(t) local reponame, names, cleanup = _mock_repo({"hello", "world"}) -- install the first plugin, leave the second only available - os.runv("xmake", {"plugin", "--install", names[1]}) + os.runv("xmake", {"plugin", "--install", "-y", names[1]}) local out = os.iorunv("xmake", {"plugin", "--list"}) t:require(out:find("the built-in plugins:", 1, true)) t:require(out:find("project", 1, true)) @@ -100,6 +113,6 @@ end -- invalid installs should fail function test_install_invalid(t) - t:require_not(try { function () os.runv("xmake", {"plugin", "--install", "plugin-test-missing"}); return true end }) - t:require_not(try { function () os.runv("xmake", {"plugin", "--install", "somerepo@.."}); return true end }) + t:require_not(try { function () os.runv("xmake", {"plugin", "--install", "-y", "plugin-test-missing"}); return true end }) + t:require_not(try { function () os.runv("xmake", {"plugin", "--install", "-y", "somerepo@.."}); return true end }) end diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 59c11b2cb..e014d9578 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -3160,7 +3160,7 @@ end -- - plat: the given platform, we need to set it to the description scope at same time, -- e.g. add_requires("zlib~mingw", {plat = "mingw"}) -- @see https://github.com/orgs/xmake-io/discussions/3439 --- - arch: the given architecture, ditto +-- - arch: the given architecture, it's the same as `opt.plat` -- - repo: the repository instance which this package belongs to -- -- @return the package instance and errors diff --git a/xmake/plugins/plugin/main.lua b/xmake/plugins/plugin/main.lua index fde93547a..facbc0b4c 100644 --- a/xmake/plugins/plugin/main.lua +++ b/xmake/plugins/plugin/main.lua @@ -54,7 +54,19 @@ function _install_plugins_from_repo(name, reponame) if reponame then installname = reponame .. "@" .. name end - os.execv(os.programfile(), {"lua", "private.xrepo", "install", "--plugin", installname}) + local argv = {"lua", "private.xrepo", "install", "--plugin"} + -- we need to pass the common options to the sub-process, e.g. -y, -v, -D + if option.get("yes") then + table.insert(argv, "-y") + end + if option.get("verbose") then + table.insert(argv, "-v") + end + if option.get("diagnosis") then + table.insert(argv, "-D") + end + table.insert(argv, installname) + os.execv(os.programfile(), argv) end -- install a single plugin from a source directory (as the given name, default to the directory name) -- cgit v1.3.1