From f23220b1abbb51882f1912533b397f2f2d38b66b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 3 Mar 2026 23:36:42 +0800 Subject: add xmake create --list --- xmake/actions/create/main.lua | 29 +++++++++++++++++++++++++++++ xmake/actions/create/xmake.lua | 21 +++++---------------- 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()}) -- cgit v1.3.1 From 5abb591b562a07ef59213cc1ddc8b74dd181f7ac Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 3 Mar 2026 23:42:58 +0800 Subject: get templates from xmake-repo --- xmake/actions/create/main.lua | 86 +++++++++++++++++++++++++++++++++++++-- xmake/actions/create/template.lua | 38 +++++++++++++++-- 2 files changed, 117 insertions(+), 7 deletions(-) diff --git a/xmake/actions/create/main.lua b/xmake/actions/create/main.lua index fc75e06a4..72b69f3b2 100644 --- a/xmake/actions/create/main.lua +++ b/xmake/actions/create/main.lua @@ -63,7 +63,13 @@ function _get_targetname() end -- list all supported templates for each language +-- +-- output is in tree format: +-- +-- +--