Refactor package removal code to eliminate its duplication

This commit is contained in:
Alexander Karatarakis 2017-09-13 16:10:25 -07:00
parent e4ea3bd3fa
commit 43dde3f489
3 changed files with 63 additions and 86 deletions

View File

@ -82,6 +82,19 @@ namespace vcpkg::Commands
namespace Remove
{
enum class Purge
{
NO = 0,
YES
};
inline Purge to_purge(const bool value) { return value ? Purge::YES : Purge::NO; }
void perform_remove_plan_action(const VcpkgPaths& paths,
const Dependencies::RemovePlanAction& action,
const Purge purge,
StatusParagraphs& status_db);
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const Triplet& default_triplet);
void remove_package(const VcpkgPaths& paths, const PackageSpec& spec, StatusParagraphs* status_db);
}

View File

@ -524,72 +524,27 @@ namespace vcpkg::Commands::Install
Checks::exit_success(VCPKG_LINE_INFO);
}
// execute the plan
if (GlobalState::feature_packages)
for (const auto& action : action_plan)
{
for (const auto& action : action_plan)
if (auto install_action = action.install_plan.get())
{
if (auto install_action = action.install_plan.get())
{
const BuildResult result =
perform_install_plan_action(paths, *install_action, install_plan_options, status_db);
if (result != BuildResult::SUCCEEDED)
{
System::println(Build::create_user_troubleshooting_message(install_action->spec));
Checks::exit_fail(VCPKG_LINE_INFO);
}
}
else if (auto remove_action = action.remove_plan.get())
{
static const std::string OPTION_PURGE = "--purge";
static const std::string OPTION_NO_PURGE = "--no-purge";
const bool alsoRemoveFolderFromPackages = options.find(OPTION_NO_PURGE) == options.end();
if (options.find(OPTION_PURGE) != options.end() && !alsoRemoveFolderFromPackages)
{
// User specified --purge and --no-purge
System::println(System::Color::error, "Error: cannot specify both --no-purge and --purge.");
System::print(EXAMPLE);
Checks::exit_fail(VCPKG_LINE_INFO);
}
const std::string display_name = remove_action->spec.to_string();
switch (remove_action->plan_type)
{
case RemovePlanType::NOT_INSTALLED:
System::println(System::Color::success, "Package %s is not installed", display_name);
break;
case RemovePlanType::REMOVE:
System::println("Removing package %s... ", display_name);
Commands::Remove::remove_package(paths, remove_action->spec, &status_db);
System::println(System::Color::success, "Removing package %s... done", display_name);
break;
case RemovePlanType::UNKNOWN:
default: Checks::unreachable(VCPKG_LINE_INFO);
}
if (alsoRemoveFolderFromPackages)
{
System::println("Purging package %s... ", display_name);
Files::Filesystem& fs = paths.get_filesystem();
std::error_code ec;
fs.remove_all(paths.packages / remove_action->spec.dir(), ec);
System::println(System::Color::success, "Purging package %s... done", display_name);
}
}
}
}
else
{
for (const auto& action : action_plan)
{
const auto& iaction = action.install_plan.value_or_exit(VCPKG_LINE_INFO);
const BuildResult result = perform_install_plan_action(paths, iaction, install_plan_options, status_db);
const BuildResult result =
perform_install_plan_action(paths, *install_action, install_plan_options, status_db);
if (result != BuildResult::SUCCEEDED)
{
System::println(Build::create_user_troubleshooting_message(iaction.spec));
System::println(Build::create_user_troubleshooting_message(install_action->spec));
Checks::exit_fail(VCPKG_LINE_INFO);
}
}
else if (auto remove_action = action.remove_plan.get())
{
Checks::check_exit(VCPKG_LINE_INFO, GlobalState::feature_packages);
Remove::perform_remove_plan_action(paths, *remove_action, Remove::Purge::YES, status_db);
}
else
{
Checks::unreachable(VCPKG_LINE_INFO);
}
}
Checks::exit_success(VCPKG_LINE_INFO);

View File

@ -129,6 +129,37 @@ namespace vcpkg::Commands::Remove
}
}
void perform_remove_plan_action(const VcpkgPaths& paths,
const RemovePlanAction& action,
const Purge purge,
StatusParagraphs& status_db)
{
const std::string display_name = action.spec.to_string();
switch (action.plan_type)
{
case RemovePlanType::NOT_INSTALLED:
System::println(System::Color::success, "Package %s is not installed", display_name);
break;
case RemovePlanType::REMOVE:
System::println("Removing package %s... ", display_name);
remove_package(paths, action.spec, &status_db);
System::println(System::Color::success, "Removing package %s... done", display_name);
break;
case RemovePlanType::UNKNOWN:
default: Checks::unreachable(VCPKG_LINE_INFO);
}
if (purge == Purge::YES)
{
System::println("Purging package %s... ", display_name);
Files::Filesystem& fs = paths.get_filesystem();
std::error_code ec;
fs.remove_all(paths.packages / action.spec.dir(), ec);
System::println(System::Color::success, "Purging package %s... done", display_name);
}
}
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const Triplet& default_triplet)
{
static const std::string OPTION_PURGE = "--purge";
@ -166,14 +197,15 @@ namespace vcpkg::Commands::Remove
Input::check_triplet(spec.triplet(), paths);
}
const bool alsoRemoveFolderFromPackages = options.find(OPTION_NO_PURGE) == options.end();
if (options.find(OPTION_PURGE) != options.end() && !alsoRemoveFolderFromPackages)
const bool no_purge_was_passed = options.find(OPTION_NO_PURGE) != options.end();
const bool purge_was_passed = options.find(OPTION_PURGE) != options.end();
if (purge_was_passed && no_purge_was_passed)
{
// User specified --purge and --no-purge
System::println(System::Color::error, "Error: cannot specify both --no-purge and --purge.");
System::print(EXAMPLE);
Checks::exit_fail(VCPKG_LINE_INFO);
}
const Purge purge = to_purge(purge_was_passed || !no_purge_was_passed);
const bool isRecursive = options.find(OPTION_RECURSE) != options.cend();
const bool dryRun = options.find(OPTION_DRY_RUN) != options.cend();
@ -209,30 +241,7 @@ namespace vcpkg::Commands::Remove
for (const RemovePlanAction& action : remove_plan)
{
const std::string display_name = action.spec.to_string();
switch (action.plan_type)
{
case RemovePlanType::NOT_INSTALLED:
System::println(System::Color::success, "Package %s is not installed", display_name);
break;
case RemovePlanType::REMOVE:
System::println("Removing package %s... ", display_name);
remove_package(paths, action.spec, &status_db);
System::println(System::Color::success, "Removing package %s... done", display_name);
break;
case RemovePlanType::UNKNOWN:
default: Checks::unreachable(VCPKG_LINE_INFO);
}
if (alsoRemoveFolderFromPackages)
{
System::println("Purging package %s... ", display_name);
Files::Filesystem& fs = paths.get_filesystem();
std::error_code ec;
fs.remove_all(paths.packages / action.spec.dir(), ec);
System::println(System::Color::success, "Purging package %s... done", display_name);
}
perform_remove_plan_action(paths, action, purge, status_db);
}
Checks::exit_success(VCPKG_LINE_INFO);