Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json format output to the depend-info command #1389

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/vcpkg/commands.depend-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ namespace vcpkg
Tree,
Dot,
Dgml,
Mermaid
Mermaid,
Json
};

struct DependInfoStrategy
Expand Down
45 changes: 45 additions & 0 deletions src/vcpkg/commands.depend-info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading