From c02f70d078661f9e8ebf75bbf1be3b4b11bd470e Mon Sep 17 00:00:00 2001 From: Jim wang Date: Fri, 19 Apr 2024 15:02:57 +0800 Subject: [PATCH 1/2] Add json format output to the depend-info command --- include/vcpkg/commands.depend-info.h | 3 +- src/vcpkg/commands.depend-info.cpp | 45 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/include/vcpkg/commands.depend-info.h b/include/vcpkg/commands.depend-info.h index 2ecaca8abd..2a147ebef2 100644 --- a/include/vcpkg/commands.depend-info.h +++ b/include/vcpkg/commands.depend-info.h @@ -39,7 +39,8 @@ namespace vcpkg Tree, Dot, Dgml, - Mermaid + Mermaid, + Json }; struct DependInfoStrategy diff --git a/src/vcpkg/commands.depend-info.cpp b/src/vcpkg/commands.depend-info.cpp index 6901930946..da99eebc71 100644 --- a/src/vcpkg/commands.depend-info.cpp +++ b/src/vcpkg/commands.depend-info.cpp @@ -267,6 +267,7 @@ namespace vcpkg static constexpr StringLiteral SwitchFormatDot = "dot"; static constexpr StringLiteral SwitchFormatDgml = "dgml"; static constexpr StringLiteral SwitchFormatMermaid = "mermaid"; + static constexpr StringLiteral SwitchFormatJson = "json"; auto& settings = args.settings; @@ -296,6 +297,10 @@ namespace vcpkg { maybe_format.emplace(DependInfoFormat::Mermaid); } + else if (as_lower == SwitchFormatJson) + { + maybe_format.emplace(DependInfoFormat::Json); + } else { return msg::format_error(msgCmdDependInfoFormatInvalid, msg::value = it->second); @@ -499,7 +504,47 @@ namespace vcpkg Checks::exit_success(VCPKG_LINE_INFO); } + + if (strategy.format == DependInfoFormat::Json) + { + std::string prifex_str = ""; + for (auto&& info : depend_info) + { + if (info.depth < 0) + { + continue; + } + + if (strategy.show_depth) + { + msg::write_unlocalized_text(Color::error, fmt::format("({})", info.depth)); + } + if (!info.features.empty()) + { + msg::write_unlocalized_text(Color::warning, "[" + Strings::join(", ", info.features) + "]"); + } + + if (info.dependencies.empty()) + { + continue; + } + prifex_str = "{ \n \"name\": \"" + info.package + "\",\n" + " \"dependency\": [ \n"; + for (auto i = info.dependencies.begin(); i != info.dependencies.end(); ++i) + { + if (i == info.dependencies.end() - 1) + { + break; + } + prifex_str += " \"" + *i + "\",\n"; + } + auto m = info.dependencies.end()-1; + prifex_str += " \"" + *m + "\"\n" + " ]\n\}\n"; + msg::write_unlocalized_text(Color::none, prifex_str); + } + Checks::exit_success(VCPKG_LINE_INFO); + } + if (strategy.format != DependInfoFormat::List) { Checks::unreachable(VCPKG_LINE_INFO); From ae0364c9787e478926e98f689dfbb38cef47487a Mon Sep 17 00:00:00 2001 From: Jim wang Date: Fri, 19 Apr 2024 15:13:21 +0800 Subject: [PATCH 2/2] modify error --- src/vcpkg/commands.depend-info.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vcpkg/commands.depend-info.cpp b/src/vcpkg/commands.depend-info.cpp index da99eebc71..cd95882675 100644 --- a/src/vcpkg/commands.depend-info.cpp +++ b/src/vcpkg/commands.depend-info.cpp @@ -539,7 +539,7 @@ namespace vcpkg prifex_str += " \"" + *i + "\",\n"; } auto m = info.dependencies.end()-1; - prifex_str += " \"" + *m + "\"\n" + " ]\n\}\n"; + prifex_str += " \"" + *m + "\"\n" + " ]\n}\n"; msg::write_unlocalized_text(Color::none, prifex_str); } Checks::exit_success(VCPKG_LINE_INFO);