From 70c73fe03cde67fd85e082cbdd2be3a406181b1b Mon Sep 17 00:00:00 2001 From: Meha555 Date: Fri, 5 Jun 2026 22:39:37 +0800 Subject: Enable `xmake show --format=json` Fix: #7585 --- tests/plugins/show/test.lua | 21 +++++++++++++++++++++ xmake/plugins/show/showlist.lua | 8 ++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/plugins/show/test.lua b/tests/plugins/show/test.lua index dea98c204..d7a59727a 100644 --- a/tests/plugins/show/test.lua +++ b/tests/plugins/show/test.lua @@ -1,5 +1,26 @@ import("core.base.json") +function test_list_targets_json_format(t) + local outdata = os.iorunv("xmake", {"show", "-l", "targets", "--format=json"}) + local targets = json.decode(outdata) + + t:require(table.contains(targets, "app")) + t:require(table.contains(targets, "core")) + t:require(table.contains(targets, "ui")) +end + +function test_list_targets_plain_format(t) + local outdata = os.iorunv("xmake", {"show", "-l", "targets", "--format=plain"}) + t:require(outdata:find("app", 1, true)) + t:require(outdata:find("core", 1, true)) + t:require(outdata:find("ui", 1, true)) +end + +function test_list_targets_unsupported_format(t) + local ok = try { function () os.execv("xmake", {"show", "-l", "targets", "--format=dot"}) end } + t:require(not ok) +end + function test_depgraph_json(t) local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--json"}) local graph = json.decode(outdata) diff --git a/xmake/plugins/show/showlist.lua b/xmake/plugins/show/showlist.lua index 205ae21ed..6adda6bd0 100644 --- a/xmake/plugins/show/showlist.lua +++ b/xmake/plugins/show/showlist.lua @@ -49,9 +49,11 @@ function _show_json(values) end function main(values) - if option.get("json") then + local format = option.get("format") + -- List providers only support plain text and JSON output. + if option.get("json") or format == "json" then _show_json(values) - else + elseif format == nil or format == "plain" then if table.is_dictionary(values) then for k, v in pairs(values) do cprint("${bright}%s:", k) @@ -60,5 +62,7 @@ function main(values) else _show_text(values) end + else + raise("unsupported list output format: %s", format) end end -- cgit v1.3.1