mirror of
https://github.com/microsoft/vcpkg.git
synced 2024-12-27 10:21:07 +08:00
[vcpkg] print list in json (#12179)
* print list command in json * fix spacing * remove reference * review * fixes based on review
This commit is contained in:
parent
273b5eddaa
commit
751fc627ef
@ -11,6 +11,46 @@ 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 = "--x-json";
|
||||
|
||||
static void do_print_json(std::vector<const vcpkg::StatusParagraph*> installed_packages)
|
||||
{
|
||||
Json::Object obj;
|
||||
for (const StatusParagraph* status_paragraph : installed_packages)
|
||||
{
|
||||
auto current_spec = status_paragraph->package.spec;
|
||||
if (obj.contains(current_spec.to_string()))
|
||||
{
|
||||
if (status_paragraph->package.is_feature())
|
||||
{
|
||||
Json::Value* value_obj = obj.get(current_spec.to_string());
|
||||
auto& feature_list = value_obj->object()["features"].array();
|
||||
feature_list.push_back(Json::Value::string(status_paragraph->package.feature));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Json::Object& library_obj = obj.insert(current_spec.to_string(), Json::Object());
|
||||
library_obj.insert("package_name", Json::Value::string(current_spec.name()));
|
||||
library_obj.insert("triplet", Json::Value::string(current_spec.triplet().to_string()));
|
||||
library_obj.insert("version", Json::Value::string(status_paragraph->package.version));
|
||||
library_obj.insert("port_version", Json::Value::integer(status_paragraph->package.port_version));
|
||||
Json::Array& features_array = library_obj.insert("features", Json::Array());
|
||||
if (status_paragraph->package.is_feature())
|
||||
{
|
||||
features_array.push_back(Json::Value::string(status_paragraph->package.feature));
|
||||
}
|
||||
Json::Array& desc = library_obj.insert("desc", Json::Array());
|
||||
for (const auto& line : status_paragraph->package.description)
|
||||
{
|
||||
desc.push_back(Json::Value::string(line));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System::print2(Json::stringify(obj, Json::JsonStyle{}));
|
||||
}
|
||||
|
||||
static void do_print(const StatusParagraph& pgh, const bool full_desc)
|
||||
{
|
||||
auto full_version = VersionT(pgh.package.version, pgh.package.port_version).to_string();
|
||||
@ -35,8 +75,9 @@ namespace vcpkg::Commands::List
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr std::array<CommandSwitch, 1> LIST_SWITCHES = {{
|
||||
static constexpr std::array<CommandSwitch, 2> LIST_SWITCHES = {{
|
||||
{OPTION_FULLDESC, "Do not truncate long text"},
|
||||
{OPTION_JSON, "List libraries in JSON format"},
|
||||
}};
|
||||
|
||||
const CommandStructure COMMAND_STRUCTURE = {
|
||||
@ -74,25 +115,25 @@ 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());
|
||||
|
||||
if (args.command_arguments.empty())
|
||||
if (!args.command_arguments.empty())
|
||||
{
|
||||
for (const StatusParagraph* status_paragraph : installed_packages)
|
||||
{
|
||||
do_print(*status_paragraph, enable_fulldesc);
|
||||
}
|
||||
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);
|
||||
});
|
||||
installed_packages = pghs;
|
||||
}
|
||||
|
||||
if (enable_json)
|
||||
{
|
||||
do_print_json(installed_packages);
|
||||
}
|
||||
else
|
||||
{
|
||||
// At this point there is 1 argument
|
||||
for (const StatusParagraph* status_paragraph : installed_packages)
|
||||
{
|
||||
const std::string displayname = status_paragraph->package.displayname();
|
||||
if (!Strings::case_insensitive_ascii_contains(displayname, args.command_arguments[0]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
do_print(*status_paragraph, enable_fulldesc);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user