diff options
| author | dan-shaw <51385773+dan-shaw@users.noreply.github.com> | 2020-09-05 07:35:40 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-05 07:35:40 -0700 |
| commit | bd8af0789804dad0f76dced3f837845f086350c5 (patch) | |
| tree | 1df1497b96010f5cfd627498ab81a7eeb44e7cef | |
| parent | 95f6e7d0c75b4d7f4bf529362a6f44d023b79fd0 (diff) | |
| download | vcpkg-bd8af0789804dad0f76dced3f837845f086350c5.tar.gz vcpkg-bd8af0789804dad0f76dced3f837845f086350c5.zip | |
[vcpkg] json format for search (#12509)
| -rw-r--r-- | toolsrc/src/vcpkg/commands.search.cpp | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/toolsrc/src/vcpkg/commands.search.cpp b/toolsrc/src/vcpkg/commands.search.cpp index 4a1defadd..9f493bf35 100644 --- a/toolsrc/src/vcpkg/commands.search.cpp +++ b/toolsrc/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 = @@ -85,12 +107,19 @@ namespace vcpkg::Commands::Search if (args.command_arguments.empty()) { - for (const auto& source_control_file : source_paragraphs) + if (enable_json) { - do_print(*source_control_file->core_paragraph, full_description); - for (auto&& feature_paragraph : source_control_file->feature_paragraphs) + do_print_json(source_paragraphs); + } + else + { + for (const auto& source_control_file : source_paragraphs) { - do_print(source_control_file->core_paragraph->name, *feature_paragraph, full_description); + do_print(*source_control_file->core_paragraph, full_description); + for (auto&& feature_paragraph : source_control_file->feature_paragraphs) + { + do_print(source_control_file->core_paragraph->name, *feature_paragraph, full_description); + } } } } @@ -137,9 +166,12 @@ namespace vcpkg::Commands::Search } } - 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"); + 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); } |
