From 724cea8b33cbf06c645f5095fa29773697da9761 Mon Sep 17 00:00:00 2001 From: Billy O'Neal Date: Wed, 8 Dec 2021 17:21:05 -0800 Subject: [PATCH] Add x-regenerate command. (#294) * Add x-regenerate command. * Fix build :) * :sigh: I need to test better. * Fix Robert code review feedback. --- include/vcpkg/commands.regenerate.h | 11 ++++ src/vcpkg-test/commands.cpp | 66 +++++++++++------------ src/vcpkg/commands.cpp | 3 ++ src/vcpkg/commands.regenerate.cpp | 81 +++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 33 deletions(-) create mode 100644 include/vcpkg/commands.regenerate.h create mode 100644 src/vcpkg/commands.regenerate.cpp diff --git a/include/vcpkg/commands.regenerate.h b/include/vcpkg/commands.regenerate.h new file mode 100644 index 0000000000..ca3048fd07 --- /dev/null +++ b/include/vcpkg/commands.regenerate.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace vcpkg +{ + struct RegenerateCommand : Commands::PathsCommand + { + void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) const override; + }; +} diff --git a/src/vcpkg-test/commands.cpp b/src/vcpkg-test/commands.cpp index 70bafcde58..2f0d308923 100644 --- a/src/vcpkg-test/commands.cpp +++ b/src/vcpkg-test/commands.cpp @@ -35,52 +35,52 @@ TEST_CASE ("list of commands is correct", "[commands]") // clang-format off std::set expected_commands{ + "/?", + "activate", + "add", + "autocomplete", + "build", + "build-external", + "cache", + "ci", "contact", - "search", - "version", + "create", + "depend-info", + "edit", + "env", + "export", + "fetch", + "find", + "format-manifest", "hash", - "x-download", - "x-init-registry", - "x-generate-default-message-map", - "/?", "help", - "list", + "install", "integrate", - "new", "list", + "new", "owns", - "update", - "edit", - "create", - "cache", "portsdiff", - "autocomplete", - "fetch", - "format-manifest", + "remove", + "search", + "update", + "upgrade", + "use", + "version", + "x-add-version", + "x-check-support", "x-ci-clean", + "x-ci-verify-versions", + "x-download", + "x-generate-default-message-map", "x-history", + "x-init-registry", "x-package-info", - "x-vsinstances", - "x-ci-verify-versions", - "x-add-version", - "install", + "x-regenerate", "x-set-installed", - "ci", - "remove", - "upgrade", - "build", - "env", - "build-external", - "export", - "depend-info", - "activate", - "find", - "use", - "add", + "x-vsinstances", + "z-bootstrap-standalone", "z-ce", - "x-check-support", "z-print-config", - "z-bootstrap-standalone", #if defined(_WIN32) "x-upload-metrics", #endif // defined(_WIN32) diff --git a/src/vcpkg/commands.cpp b/src/vcpkg/commands.cpp index f24ce43dc6..5ab13a5baa 100644 --- a/src/vcpkg/commands.cpp +++ b/src/vcpkg/commands.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -99,6 +100,7 @@ namespace vcpkg::Commands static const Owns::OwnsCommand owns{}; static const PortHistory::PortHistoryCommand porthistory{}; static const PortsDiff::PortsDiffCommand portsdiff{}; + static const RegenerateCommand regenerate{}; static const SearchCommand search{}; static const Update::UpdateCommand update{}; static const UseCommand use{}; @@ -130,6 +132,7 @@ namespace vcpkg::Commands {"x-ci-verify-versions", &ci_verify_versions}, {"x-history", &porthistory}, {"x-package-info", &info}, + {"x-regenerate", ®enerate}, {"x-vsinstances", &vsinstances}, {"z-ce", &ce}, }; diff --git a/src/vcpkg/commands.regenerate.cpp b/src/vcpkg/commands.regenerate.cpp new file mode 100644 index 0000000000..86bde95a55 --- /dev/null +++ b/src/vcpkg/commands.regenerate.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace +{ + using namespace vcpkg; + + constexpr StringLiteral DRY_RUN = "dry-run"; + constexpr StringLiteral FORCE = "force"; + constexpr StringLiteral PROJECT = "project"; + constexpr StringLiteral REGISTRY = "registry"; + + constexpr std::array command_switches = {{ + {FORCE, "proceeds with the (potentially dangerous) action without confirmation"}, + {DRY_RUN, "does not actually perform the action, shows only what would be done"}, + }}; + + constexpr std::array command_settings = {{ + {PROJECT, "override the path to the project folder"}, + {REGISTRY, "override the path to the registry"}, + }}; + + static const CommandStructure command_structure = { + Strings::format("Regenerates an artifact registry.\n%s\n", create_example_string("x-regenerate")), + 0, + 0, + {command_switches, command_settings}, + nullptr, + }; + + static void forward_setting(std::vector& forwarded_args, + const std::unordered_map& settings, + StringLiteral name) + { + auto found_setting = settings.find(name); + if (found_setting != settings.end()) + { + forwarded_args.push_back(Strings::concat("--", name)); + forwarded_args.push_back(found_setting->second); + } + } +} + +namespace vcpkg +{ + void RegenerateCommand::perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) const + { + std::vector forwarded_args; + forwarded_args.push_back("regenerate"); + const auto parsed = args.parse_arguments(command_structure); + if (Debug::g_debugging) + { + forwarded_args.push_back("--debug"); + } + + if (Util::Sets::contains(parsed.switches, FORCE)) + { + forwarded_args.push_back("--force"); + } + + if (Util::Sets::contains(parsed.switches, DRY_RUN)) + { + forwarded_args.push_back("--what-if"); + } + + forward_setting(forwarded_args, parsed.settings, PROJECT); + forward_setting(forwarded_args, parsed.settings, REGISTRY); + + Checks::exit_with_code(VCPKG_LINE_INFO, run_configure_environment_command(paths, forwarded_args)); + } +}