[vcpkg] json format for search
MODIFY
src/vcpkg/commands.search.cpp
src/vcpkg/commands.search.cpp
@@ -15,7 +15,27 @@ using vcpkg::PortFileProvider::PathsPortFileProvider;
namespace vcpkg::Commands::Search
{
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::SourceControlFile*> source_control_files)
+ {
+ Json::Object obj;
+ for (const SourceControlFile* scf : source_control_files)
+ {
+ auto& source_paragraph = scf->core_paragraph;
+ Json::Object& library_obj = obj.insert(source_paragraph->name, Json::Object());
+ library_obj.insert("package_name", Json::Value::string(source_paragraph->name));
+ library_obj.insert("version", Json::Value::string(source_paragraph->version));
+ library_obj.insert("port_version", Json::Value::integer(source_paragraph->port_version));
+ Json::Array& desc = library_obj.insert("description", Json::Array());
+ for (const auto& line : source_paragraph->description)
+ {
+ desc.push_back(Json::Value::string(line));
+ }
+ }
+
+ System::print2(Json::stringify(obj, Json::JsonStyle{}));
+ }
static void do_print(const SourceParagraph& source_paragraph, bool full_desc)
{
auto full_version = VersionT(source_paragraph.version, source_paragraph.port_version).to_string();
@@ -59,8 +79,9 @@ namespace vcpkg::Commands::Search
}
}
- static constexpr std::array<CommandSwitch, 1> SEARCH_SWITCHES = {{
+ static constexpr std::array<CommandSwitch, 2> SEARCH_SWITCHES = {{
{OPTION_FULLDESC, "Do not truncate long text"},
+ {OPTION_JSON, "(experimental) List libraries in JSON format"},
}};
const CommandStructure COMMAND_STRUCTURE = {
@@ -77,6 +98,7 @@ namespace vcpkg::Commands::Search
{
const ParsedArguments options = args.parse_arguments(COMMAND_STRUCTURE);
const bool full_description = Util::Sets::contains(options.switches, OPTION_FULLDESC);
+ const bool enable_json = Util::Sets::contains(options.switches, OPTION_JSON);
PathsPortFileProvider provider(paths, args.overlay_ports);
auto source_paragraphs =
@@ -84,6 +106,12 @@ namespace vcpkg::Commands::Search
[](auto&& port) -> const SourceControlFile* { return port->source_control_file.get(); });
if (args.command_arguments.empty())
+ {
+ if (enable_json)
+ {
+ do_print_json(source_paragraphs);
+ }
+ else
{
for (const auto& source_control_file : source_paragraphs)
{
@@ -94,6 +122,7 @@ namespace vcpkg::Commands::Search
}
}
}
+ }
else
{
// At this point there is 1 argument
@@ -137,9 +166,12 @@ namespace vcpkg::Commands::Search
}
}
+ if (!enable_json)
+ {
System::print2(
"\nIf your library is not listed, please open an issue at and/or consider making a pull request:\n"
" https://github.com/Microsoft/vcpkg/issues\n");
+ }
Checks::exit_success(VCPKG_LINE_INFO);
}

[vcpkg] json format for search (#12509)