Skip to content

Commit

Permalink
Add x-regenerate command. (#294)
Browse files Browse the repository at this point in the history
* Add x-regenerate command.

* Fix build :)

* :sigh: I need to test better.

* Fix Robert code review feedback.
  • Loading branch information
BillyONeal authored Dec 9, 2021
1 parent 583d996 commit 724cea8
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 33 deletions.
11 changes: 11 additions & 0 deletions include/vcpkg/commands.regenerate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vcpkg/commands.interface.h>

namespace vcpkg
{
struct RegenerateCommand : Commands::PathsCommand
{
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) const override;
};
}
66 changes: 33 additions & 33 deletions src/vcpkg-test/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,52 @@ TEST_CASE ("list of commands is correct", "[commands]")

// clang-format off
std::set<std::string> 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)
Expand Down
3 changes: 3 additions & 0 deletions src/vcpkg/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <vcpkg/commands.owns.h>
#include <vcpkg/commands.porthistory.h>
#include <vcpkg/commands.portsdiff.h>
#include <vcpkg/commands.regenerate.h>
#include <vcpkg/commands.search.h>
#include <vcpkg/commands.setinstalled.h>
#include <vcpkg/commands.upgrade.h>
Expand Down Expand Up @@ -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{};
Expand Down Expand Up @@ -130,6 +132,7 @@ namespace vcpkg::Commands
{"x-ci-verify-versions", &ci_verify_versions},
{"x-history", &porthistory},
{"x-package-info", &info},
{"x-regenerate", &regenerate},
{"x-vsinstances", &vsinstances},
{"z-ce", &ce},
};
Expand Down
81 changes: 81 additions & 0 deletions src/vcpkg/commands.regenerate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <vcpkg/base/basic_checks.h>
#include <vcpkg/base/stringliteral.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/util.h>

#include <vcpkg/commands.regenerate.h>
#include <vcpkg/configure-environment.h>
#include <vcpkg/vcpkgcmdarguments.h>

#include <array>
#include <string>
#include <vector>

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<CommandSwitch, 2> 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<CommandSetting, 2> 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<std::string>& forwarded_args,
const std::unordered_map<std::string, std::string>& 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<std::string> 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));
}
}

0 comments on commit 724cea8

Please sign in to comment.