From fea8ad3d654e497ba83315f26902647dd64f561e Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Apr 2026 21:52:57 +0800 Subject: add --dot output --- tests/plugins/show/src/core.c | 1 + tests/plugins/show/src/main.c | 1 + tests/plugins/show/src/ui.c | 1 + tests/plugins/show/test.lua | 58 +++++++++++++++++++++++++ tests/plugins/show/xmake.lua | 15 +++++++ tests/projects/plugins/show/depgraph/src/core.c | 1 - tests/projects/plugins/show/depgraph/src/main.c | 1 - tests/projects/plugins/show/depgraph/src/ui.c | 1 - tests/projects/plugins/show/depgraph/test.lua | 33 -------------- tests/projects/plugins/show/depgraph/xmake.lua | 15 ------- xmake/plugins/show/info/depgraph.lua | 58 ++++++++++++++++++------- xmake/plugins/show/xmake.lua | 1 + 12 files changed, 119 insertions(+), 67 deletions(-) create mode 100644 tests/plugins/show/src/core.c create mode 100644 tests/plugins/show/src/main.c create mode 100644 tests/plugins/show/src/ui.c create mode 100644 tests/plugins/show/test.lua create mode 100644 tests/plugins/show/xmake.lua delete mode 100644 tests/projects/plugins/show/depgraph/src/core.c delete mode 100644 tests/projects/plugins/show/depgraph/src/main.c delete mode 100644 tests/projects/plugins/show/depgraph/src/ui.c delete mode 100644 tests/projects/plugins/show/depgraph/test.lua delete mode 100644 tests/projects/plugins/show/depgraph/xmake.lua diff --git a/tests/plugins/show/src/core.c b/tests/plugins/show/src/core.c new file mode 100644 index 000000000..87a413095 --- /dev/null +++ b/tests/plugins/show/src/core.c @@ -0,0 +1 @@ +int core(void) { return 0; } diff --git a/tests/plugins/show/src/main.c b/tests/plugins/show/src/main.c new file mode 100644 index 000000000..78f2de106 --- /dev/null +++ b/tests/plugins/show/src/main.c @@ -0,0 +1 @@ +int main(void) { return 0; } diff --git a/tests/plugins/show/src/ui.c b/tests/plugins/show/src/ui.c new file mode 100644 index 000000000..c15794f4d --- /dev/null +++ b/tests/plugins/show/src/ui.c @@ -0,0 +1 @@ +int ui(void) { return 0; } diff --git a/tests/plugins/show/test.lua b/tests/plugins/show/test.lua new file mode 100644 index 000000000..451b13ade --- /dev/null +++ b/tests/plugins/show/test.lua @@ -0,0 +1,58 @@ +import("core.base.json") + +function test_depgraph_json(t) + local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--json"}) + local graph = json.decode(outdata) + + t:are_equal(graph.root_targets, {"app"}) + t:require(#graph.targets == 3) + + local entries = {} + for _, target in ipairs(graph.targets) do + entries[target.name] = target + end + t:are_equal(entries.core.deps, {}) + t:are_equal(entries.ui.deps, {"core"}) + t:are_equal(entries.app.deps, {"core", "ui"}) +end + +function test_depgraph_json_for_single_target(t) + local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--target=app", "--json", }) + local graph = json.decode(outdata) + + t:are_equal(graph.root_targets, {"app"}) + t:require(#graph.targets == 3) + + local names = {} + for _, target in ipairs(graph.targets) do + table.insert(names, target.name) + end + t:are_equal(names, {"core", "ui", "app"}) +end + +function test_depgraph_tree(t) + local outdata = os.iorunv("xmake", {"show", "--info=depgraph", }) + t:require(outdata:find("app", 1, true)) + t:require(outdata:find("core", 1, true)) + t:require(outdata:find("ui", 1, true)) + t:require(outdata:find("├── ", 1, true) or outdata:find("+-- ", 1, true)) + t:require(outdata:find("└── ", 1, true) or outdata:find("\\-- ", 1, true)) +end + +function test_depgraph_dot(t) + local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--dot", }) + t:require(outdata:find("digraph {", 1, true)) + t:require(outdata:find('"core"', 1, true)) + t:require(outdata:find('"ui" -> "core"', 1, true)) + t:require(outdata:find('"app" -> "core"', 1, true)) + t:require(outdata:find('"app" -> "ui"', 1, true)) + t:require(outdata:find("}", 1, true)) +end + +function test_depgraph_dot_for_single_target(t) + local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--target=ui", "--dot", }) + t:require(outdata:find("digraph {", 1, true)) + t:require(outdata:find('"core"', 1, true)) + t:require(outdata:find('"ui" -> "core"', 1, true)) + t:require(not outdata:find('"app"', 1, true)) +end diff --git a/tests/plugins/show/xmake.lua b/tests/plugins/show/xmake.lua new file mode 100644 index 000000000..7077424f3 --- /dev/null +++ b/tests/plugins/show/xmake.lua @@ -0,0 +1,15 @@ +add_rules("mode.debug", "mode.release") + +target("core") + set_kind("static") + add_files("src/core.c") + +target("ui") + set_kind("static") + add_deps("core") + add_files("src/ui.c") + +target("app") + set_kind("binary") + add_deps("core", "ui") + add_files("src/main.c") diff --git a/tests/projects/plugins/show/depgraph/src/core.c b/tests/projects/plugins/show/depgraph/src/core.c deleted file mode 100644 index 87a413095..000000000 --- a/tests/projects/plugins/show/depgraph/src/core.c +++ /dev/null @@ -1 +0,0 @@ -int core(void) { return 0; } diff --git a/tests/projects/plugins/show/depgraph/src/main.c b/tests/projects/plugins/show/depgraph/src/main.c deleted file mode 100644 index 78f2de106..000000000 --- a/tests/projects/plugins/show/depgraph/src/main.c +++ /dev/null @@ -1 +0,0 @@ -int main(void) { return 0; } diff --git a/tests/projects/plugins/show/depgraph/src/ui.c b/tests/projects/plugins/show/depgraph/src/ui.c deleted file mode 100644 index c15794f4d..000000000 --- a/tests/projects/plugins/show/depgraph/src/ui.c +++ /dev/null @@ -1 +0,0 @@ -int ui(void) { return 0; } diff --git a/tests/projects/plugins/show/depgraph/test.lua b/tests/projects/plugins/show/depgraph/test.lua deleted file mode 100644 index 229e715de..000000000 --- a/tests/projects/plugins/show/depgraph/test.lua +++ /dev/null @@ -1,33 +0,0 @@ -import("core.base.json") - -function test_depgraph_json(t) - local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--json"}) - local graph = json.decode(outdata) - - t:are_equal(graph.root_targets, {"app"}) - t:require(#graph.targets == 3) - - local entries = {} - for _, target in ipairs(graph.targets) do - entries[target.name] = target - end - t:are_equal(entries.core.deps, {}) - t:are_equal(entries.core.kind, "static") - t:are_equal(entries.ui.deps, {"core"}) - t:are_equal(entries.app.deps, {"core", "ui"}) - t:are_equal(entries.app.kind, "binary") -end - -function test_depgraph_json_for_single_target(t) - local outdata = os.iorunv("xmake", {"show", "--info=depgraph", "--target=app", "--json"}) - local graph = json.decode(outdata) - - t:are_equal(graph.root_targets, {"app"}) - t:require(#graph.targets == 3) - - local names = {} - for _, target in ipairs(graph.targets) do - table.insert(names, target.name) - end - t:are_equal(names, {"core", "ui", "app"}) -end diff --git a/tests/projects/plugins/show/depgraph/xmake.lua b/tests/projects/plugins/show/depgraph/xmake.lua deleted file mode 100644 index 7077424f3..000000000 --- a/tests/projects/plugins/show/depgraph/xmake.lua +++ /dev/null @@ -1,15 +0,0 @@ -add_rules("mode.debug", "mode.release") - -target("core") - set_kind("static") - add_files("src/core.c") - -target("ui") - set_kind("static") - add_deps("core") - add_files("src/ui.c") - -target("app") - set_kind("binary") - add_deps("core", "ui") - add_files("src/main.c") diff --git a/xmake/plugins/show/info/depgraph.lua b/xmake/plugins/show/info/depgraph.lua index 60e667002..199754772 100644 --- a/xmake/plugins/show/info/depgraph.lua +++ b/xmake/plugins/show/info/depgraph.lua @@ -28,12 +28,8 @@ import("private.detect.check_targetname") function _collect_target_entry(target) local deps = table.wrap(target:get("deps")) json.mark_as_array(deps) - return { name = target:name(), - kind = target:kind(), - group = target:get("group"), - default = target:is_default(), deps = deps } end @@ -83,22 +79,49 @@ function _collect_target_graph(root_target) } end +function _print_dep_tree(targets_map, name, prefix, expanded) + expanded[name] = true + local entry = targets_map[name] + local deps = entry and entry.deps or {} + for i, dep in ipairs(deps) do + local is_last = (i == #deps) + local connector = is_last and "└── " or "├── " + local next_prefix = prefix .. (is_last and " " or "│ ") + local dep_entry = targets_map[dep] + local dep_deps = dep_entry and dep_entry.deps or {} + if expanded[dep] and #dep_deps > 0 then + cprint("%s%s${color.dump.string}%s${clear} ${dim}(*)${clear}", prefix, connector, dep) + else + cprint("%s%s${color.dump.string}%s${clear}", prefix, connector, dep) + _print_dep_tree(targets_map, dep, next_prefix, expanded) + end + end +end + function _print_target_graph(graph) - print("The dependency graph of targets:") - cprint("") - cprint(" ${color.dump.string}root targets${clear}: %s", table.concat(graph.root_targets, ", ")) - cprint("") + local targets_map = {} for _, target in ipairs(graph.targets) do - cprint(" ${color.dump.string}%s${clear}:", target.name) - cprint(" ${dim}kind${clear}: %s", target.kind) - if target.group then - cprint(" ${dim}group${clear}: %s", target.group) - end - cprint(" ${dim}default${clear}: %s", tostring(target.default)) - if #target.deps > 0 then - cprint(" ${dim}deps${clear}: %s", table.concat(target.deps, ", ")) + targets_map[target.name] = target + end + local expanded = {} + for _, root in ipairs(graph.root_targets) do + cprint("${color.dump.string}%s${clear}", root) + _print_dep_tree(targets_map, root, "", expanded) + end +end + +function _print_dot_graph(graph) + print("digraph {") + for _, target in ipairs(graph.targets) do + if #target.deps == 0 then + print(string.format(" \"%s\"", target.name)) + else + for _, dep in ipairs(target.deps) do + print(string.format(" \"%s\" -> \"%s\"", target.name, dep)) + end end end + print("}") end function main(name) @@ -110,12 +133,15 @@ function main(name) end local graph = _collect_target_graph(root_target) + assert(not (option.get("json") and option.get("dot")), "--json and --dot are mutually exclusive") if option.get("json") then local json_opt if option.get("pretty") then json_opt = {pretty = true, orderkeys = true} end print(json.encode(graph, json_opt)) + elseif option.get("dot") then + _print_dot_graph(graph) else _print_target_graph(graph) end diff --git a/xmake/plugins/show/xmake.lua b/xmake/plugins/show/xmake.lua index 77d2be2b3..de7a5a1c5 100644 --- a/xmake/plugins/show/xmake.lua +++ b/xmake/plugins/show/xmake.lua @@ -32,6 +32,7 @@ task("show") {'g', "group", "kv", nil, "Filter targets by the given group name."}, {nil, "json", "k", false, "Show information with json format."}, {nil, "pretty", "k", false, "Enable pretty formatted json output."}, + {nil, "dot", "k", false, "Output dependency graph in graphviz DOT format."}, {'i', "info", "kv", nil, "Show the given information.", values = function (complete, opt) return import("list").infos() -- cgit v1.3.1