summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-03-03 23:36:42 +0800
committerruki <[email protected]>2026-03-03 23:36:42 +0800
commitf23220b1abbb51882f1912533b397f2f2d38b66b (patch)
treef587e8a17d7645ed61530706edd966df9bc2af55
parent83dcecd2bde90ca5a78fd263ba0f79401c5c223c (diff)
add xmake create --list
-rw-r--r--xmake/actions/create/main.lua29
-rw-r--r--xmake/actions/create/xmake.lua21
2 files changed, 34 insertions, 16 deletions
diff --git a/xmake/actions/create/main.lua b/xmake/actions/create/main.lua
index b5a7b1302..fc75e06a4 100644
--- a/xmake/actions/create/main.lua
+++ b/xmake/actions/create/main.lua
@@ -62,6 +62,28 @@ function _get_targetname()
return option.get("target") or path.basename(project.directory()) or "demo"
end
+-- list all supported templates for each language
+function _list_templates(lang_filter)
+ local languages = template.languages()
+ if lang_filter then
+ _validate_template_component("language", lang_filter)
+ if not table.contains(languages, lang_filter) then
+ raise("unknown language(%s), supported languages: %s", lang_filter, table.concat(languages, ", "))
+ end
+ languages = {lang_filter}
+ end
+ for _, lang in ipairs(languages) do
+ cprint("${bright}%s${reset}", lang)
+ local templates = template.templates(lang)
+ if templates and #templates > 0 then
+ for _, name in ipairs(templates) do
+ cprint(" - %s", name)
+ end
+ end
+ print("")
+ end
+end
+
-- create project from template
function _create_project(lang, templateid, targetname)
assert(targetname ~= ".", "you should specify ${red}-P${reset} instead of directly using ${red}.${reset}")
@@ -119,6 +141,13 @@ end
function main()
os.cd(os.workingdir())
+ -- `xmake create --list` only prints available templates and exits.
+ if option.get("list") then
+ local explicit_language = option.options() and option.options().language
+ _list_templates(explicit_language)
+ return
+ end
+
local targetname = _get_targetname()
local templateid = _get_templateid()
local lang = _get_language_from_template(templateid)
diff --git a/xmake/actions/create/xmake.lua b/xmake/actions/create/xmake.lua
index a59c56296..98e3b68bb 100644
--- a/xmake/actions/create/xmake.lua
+++ b/xmake/actions/create/xmake.lua
@@ -26,6 +26,10 @@ task("create")
description = "Create a new project.",
options = {
{'f', "force", "k", nil, "Force to create project in a non-empty directory."},
+ {nil, "list", "k", nil, "List all templates for each language."
+ , " e.g."
+ , " - xmake create --list"
+ , " - xmake create --list -l c++"},
{'l', "language", "kv", "c++", "The project language",
values = function (complete, opt)
import("actions.create.template", {rootdir = os.programdir()})
@@ -37,22 +41,7 @@ task("create")
return template.languages_for_template(opt.template)
end },
{'t', "template", "kv", "console", "Select the project template id or name of the given language.",
- function ()
- import("actions.create.template", {rootdir = os.programdir()})
-
- local templates = template.templates_with_languages()
- local templates_sorted = {}
- for name, languages in pairs(templates) do
- table.insert(templates_sorted, {name = name, languages = languages})
- end
- table.sort(templates_sorted, function(a, b) return a.name < b.name end)
-
- local description = {}
- for _, t in ipairs(templates_sorted) do
- table.insert(description, " - " .. t.name .. ": " .. table.concat(t.languages, ", "))
- end
- return description
- end,
+ " Use `xmake create --list` to view all templates.",
values = function (complete, opt)
if complete then
import("actions.create.template", {rootdir = os.programdir()})