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

[vcpkg] print list in json #12179

Merged
merged 6 commits into from
Jul 17, 2020
Merged
Changes from 4 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
73 changes: 62 additions & 11 deletions toolsrc/src/vcpkg/commands.list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,51 @@
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/base/json.h>

namespace vcpkg::Commands::List
{
static constexpr StringLiteral OPTION_FULLDESC =
"--x-full-desc"; // TODO: This should find a better home, eventually

static constexpr StringLiteral OPTION_JSON = "--json";
dan-shaw marked this conversation as resolved.
Show resolved Hide resolved

static void do_print_json(std::vector<const vcpkg::StatusParagraph*> installed_packages)
{
Json::Object obj;
strega-nil marked this conversation as resolved.
Show resolved Hide resolved

for (const StatusParagraph* status_paragraph : installed_packages)
{
auto current_spec = status_paragraph->package.spec;
if (!obj.contains(current_spec.to_string()))
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
{
Json::Object& library_obj = obj.insert(current_spec.to_string(), Json::Object());
library_obj.insert("package_name", Json::Value::string(current_spec.to_string()));
library_obj.insert("triplet", Json::Value::string(current_spec.triplet().to_string()));
library_obj.insert("version", Json::Value::string(status_paragraph->package.version));
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
Json::Array arr;
if (status_paragraph->package.is_feature())
{
arr.push_back(Json::Value::string(status_paragraph->package.feature));
}
library_obj.insert("features", std::move(arr));
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
library_obj.insert("desc",
Json::Value::string(Strings::join("\n", status_paragraph->package.description)));
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
if (status_paragraph->package.is_feature())
{
auto library_obj = obj.get(current_spec.to_string());
auto& feature_list = library_obj->object().get("feature")->array();
strega-nil marked this conversation as resolved.
Show resolved Hide resolved
feature_list.push_back(Json::Value::string(status_paragraph->package.feature));
}
}
}

System::printf("%s\n", Json::stringify(obj, Json::JsonStyle{}));
dan-shaw marked this conversation as resolved.
Show resolved Hide resolved
}

static void do_print(const StatusParagraph& pgh, const bool full_desc)
{
if (full_desc)
Expand All @@ -33,9 +72,8 @@ namespace vcpkg::Commands::List
}
}

static constexpr std::array<CommandSwitch, 1> LIST_SWITCHES = {{
{OPTION_FULLDESC, "Do not truncate long text"},
}};
static constexpr std::array<CommandSwitch, 2> LIST_SWITCHES = {
{{OPTION_FULLDESC, "Do not truncate long text"}, {OPTION_JSON, "List libraries in JSON format"}}};
dan-shaw marked this conversation as resolved.
Show resolved Hide resolved

const CommandStructure COMMAND_STRUCTURE = {
Strings::format(
Expand Down Expand Up @@ -72,26 +110,39 @@ namespace vcpkg::Commands::List
});

const auto enable_fulldesc = Util::Sets::contains(options.switches, OPTION_FULLDESC.to_string());
const auto enable_json = Util::Sets::contains(options.switches, OPTION_JSON.to_string());

dan-shaw marked this conversation as resolved.
Show resolved Hide resolved
if (args.command_arguments.empty())
{
for (const StatusParagraph* status_paragraph : installed_packages)
if (enable_json)
{
do_print(*status_paragraph, enable_fulldesc);
do_print_json(installed_packages);
}
else
{
for (const StatusParagraph* status_paragraph : installed_packages)
{
do_print(*status_paragraph, enable_fulldesc);
}
}
}
else
{
// At this point there is 1 argument
for (const StatusParagraph* status_paragraph : installed_packages)
auto& query = args.command_arguments[0];
auto pghs = Util::filter(installed_packages, [query](const StatusParagraph* status_paragraph) {
return Strings::case_insensitive_ascii_contains(status_paragraph->package.displayname(), query);
});
if (enable_json)
{
const std::string displayname = status_paragraph->package.displayname();
if (!Strings::case_insensitive_ascii_contains(displayname, args.command_arguments[0]))
do_print_json(pghs);
}
else
{
for (const StatusParagraph* status_paragraph : pghs)
{
continue;
do_print(*status_paragraph, enable_fulldesc);
}

do_print(*status_paragraph, enable_fulldesc);
}
}

Expand Down