summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-08-08 22:16:02 +0800
committerruki <[email protected]>2026-08-09 18:28:59 +0800
commit4fc05750f6da0568df7a0fa836e114128d3ba849 (patch)
tree98f6f401a2c6629464dcd393ddb5879483b4555e
parent6a024e442cf6e3a1fe55ab63b3385be592bb1e1e (diff)
rewrite template dist
-rw-r--r--xmake/actions/create/main.lua30
-rw-r--r--xmake/actions/create/template.lua40
-rw-r--r--xmake/core/package/addon.lua21
-rw-r--r--xmake/core/sandbox/modules/import/core/package/addon.lua2
4 files changed, 71 insertions, 22 deletions
diff --git a/xmake/actions/create/main.lua b/xmake/actions/create/main.lua
index 89a4791a6..476fd1325 100644
--- a/xmake/actions/create/main.lua
+++ b/xmake/actions/create/main.lua
@@ -79,26 +79,15 @@ function _list_templates(lang_filter)
languages = {lang_filter}
end
- -- map a template directory to its root meta (repo/global/builtin)
- local rootinfo_of = function (dir)
- if not dir then
- return
- end
- dir = path.absolute(dir)
- for _, info in ipairs(rootinfos) do
- local rootdir = path.absolute(info.dir)
- if dir == rootdir or dir:startswith(rootdir .. path.sep()) then
- return info
- end
- end
- end
+ -- map a template directory to its root meta (repo/addon/global/builtin)
+ local rootinfo_of = template.rootinfo_of
local sourcekey_of = function (info)
if not info then
return "unknown"
end
- if info.kind == "repo" then
- return "repo:" .. info.name
+ if info.kind == "repo" or info.kind == "addon" then
+ return info.kind .. ":" .. info.name
end
return info.kind or "unknown"
end
@@ -142,7 +131,10 @@ function _list_templates(lang_filter)
local info = group.info
if info and info.kind == "repo" then
local branch = info.branch and (" " .. info.branch) or ""
- cprint("${bright}%s${reset}: %s%s", info.name, info.url or "", branch)
+ local deprecated = info.deprecated and " ${yellow}(deprecated)${clear}" or ""
+ cprint("${bright}%s${reset}: %s%s%s", info.name, info.url or "", branch, deprecated)
+ elseif info and info.kind == "addon" then
+ cprint("${bright}%s${reset}: addon %s", info.name, info.version or "")
else
cprint("${bright}%s${reset}", (info and info.kind) or "unknown")
end
@@ -198,6 +190,12 @@ function _create_project(lang, templateid, targetname)
raise("template(%s/%s): not found!\nyou can try:\n - xrepo update-repo (update repositories)\n - xmake create --list (show available templates)", lang, templateid)
end
+ -- the templates in repositories are deprecated, they should be distributed as addons
+ local rootinfo = template.rootinfo_of(sourcedir)
+ if rootinfo and rootinfo.deprecated then
+ wprint("the templates in <%s>/templates are deprecated, please install them as an addon instead, e.g. xmake addon --install basic-templates", rootinfo.name or "repo")
+ end
+
-- get the builtin variables
local builtinvars = template.builtinvars(targetname)
diff --git a/xmake/actions/create/template.lua b/xmake/actions/create/template.lua
index e18015514..778028ce8 100644
--- a/xmake/actions/create/template.lua
+++ b/xmake/actions/create/template.lua
@@ -22,6 +22,7 @@
import("core.base.global")
import("core.base.hashset")
import("core.language.language")
+import("core.package.addon")
import("core.package.repository")
-- some builtin template variables in xmake.lua
@@ -33,19 +34,32 @@ end
-- get all template roots with extra meta information
--
-- priority:
--- 1. repo: <global-repo>/templates
--- 2. global: <globaldir>/templates
--- 3. builtin: <programdir>/templates
+-- 1. addon: <globaldir>/addons/<name>/<version>/templates
+-- 2. repo: <global-repo>/templates (deprecated)
+-- 3. global: <globaldir>/templates
+-- 4. builtin: <programdir>/templates
function rootinfos()
local results = {}
+ -- get template directories from the installed addons
+ for _, addoninfo in ipairs(addon.payloadinfos("templates")) do
+ if os.isdir(addoninfo.dir) then
+ table.insert(results, {kind = "addon", name = addoninfo.name, version = addoninfo.version, dir = addoninfo.dir})
+ end
+ end
+
-- get template directories from global repositories
+ --
+ -- @note it's deprecated, please distribute the templates as an addon instead,
+ -- e.g. xmake addon --install basic-templates
+ --
local repos = repository.repositories({global = true, network = false})
if repos then
for _, repo in ipairs(repos) do
local templatesdir = path.join(repo:directory(), "templates")
if os.isdir(templatesdir) then
- table.insert(results, {kind = "repo", name = repo:name(), url = repo:url(), branch = repo:branch(), dir = templatesdir})
+ table.insert(results, {kind = "repo", name = repo:name(), url = repo:url(), branch = repo:branch(),
+ dir = templatesdir, deprecated = true})
end
end
end
@@ -62,6 +76,24 @@ function rootinfos()
return results
end
+-- get the root information of the given template directory
+--
+-- @param templatedir the template directory, e.g. <programdir>/templates/c/console
+-- @return the root information, @see rootinfos
+--
+function rootinfo_of(templatedir)
+ if not templatedir then
+ return
+ end
+ templatedir = path.absolute(templatedir)
+ for _, info in ipairs(rootinfos()) do
+ local rootdir = path.absolute(info.dir)
+ if templatedir == rootdir or templatedir:startswith(rootdir .. path.sep()) then
+ return info
+ end
+ end
+end
+
-- get template root directories
function rootdirs()
local results = {}
diff --git a/xmake/core/package/addon.lua b/xmake/core/package/addon.lua
index e40a83a47..35966853d 100644
--- a/xmake/core/package/addon.lua
+++ b/xmake/core/package/addon.lua
@@ -97,12 +97,29 @@ end
--
function addon.payloads(kind)
local payloads = {}
+ for _, payloadinfo in ipairs(addon.payloadinfos(kind)) do
+ table.insert(payloads, payloadinfo.dir)
+ end
+ return payloads
+end
+
+-- get the payload information of the given kind from all installed addons
+--
+-- @param kind the payload kind, e.g. "plugins", "rules"
+-- @return the payload infos, e.g. {{name = "hello-world", version = "latest", dir = "~/.xmake/addons/hello-world/latest/plugins"}}
+--
+function addon.payloadinfos(kind)
+ local payloadinfos = {}
for name, addoninfo in pairs(addon.addons()) do
if table.contains(addoninfo.payloads or {}, kind) then
- table.insert(payloads, path.join(addon.installdir(), name, addoninfo.version, kind))
+ table.insert(payloadinfos, {
+ name = name,
+ version = addoninfo.version,
+ dir = path.join(addon.installdir(), name, addoninfo.version, kind)})
end
end
- return payloads
+ table.sort(payloadinfos, function (a, b) return a.name < b.name end)
+ return payloadinfos
end
-- get the payload directories of the given addon directory, e.g. {"plugins", "rules"}
diff --git a/xmake/core/sandbox/modules/import/core/package/addon.lua b/xmake/core/sandbox/modules/import/core/package/addon.lua
index c1267a5d4..ecbd95f2b 100644
--- a/xmake/core/sandbox/modules/import/core/package/addon.lua
+++ b/xmake/core/sandbox/modules/import/core/package/addon.lua
@@ -26,9 +26,11 @@ local addon = require("package/addon")
-- inherit some builtin interfaces
sandbox_core_package_addon.installdir = addon.installdir
+sandbox_core_package_addon.dirname = addon.dirname
sandbox_core_package_addon.registryfile = addon.registryfile
sandbox_core_package_addon.payloaddirs = addon.payloaddirs
sandbox_core_package_addon.payloads = addon.payloads
+sandbox_core_package_addon.payloadinfos = addon.payloadinfos
sandbox_core_package_addon.payloads_of = addon.payloads_of
sandbox_core_package_addon.addons = addon.addons
sandbox_core_package_addon.addondir = addon.addondir