[vcpkg-x-set-installed] Implement --dry-run (#12132)

* [vcpkg-x-set-installed] Implement --dry-run

* [vcpkg] Address code review comments

Co-authored-by: Robert Schumacher <roschuma@microsoft.com>
This commit is contained in:
ras0219 2020-06-27 09:50:01 -07:00 committed by GitHub
parent a0bd59c6a2
commit d47570b8d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 19 deletions

View File

@ -13,11 +13,17 @@
namespace vcpkg::Commands::SetInstalled namespace vcpkg::Commands::SetInstalled
{ {
static constexpr StringLiteral OPTION_DRY_RUN = "--dry-run";
static constexpr CommandSwitch INSTALL_SWITCHES[] = {
{OPTION_DRY_RUN, "Do not actually build or install"},
};
const CommandStructure COMMAND_STRUCTURE = { const CommandStructure COMMAND_STRUCTURE = {
create_example_string(R"(x-set-installed <package>...)"), create_example_string(R"(x-set-installed <package>...)"),
1, 0,
SIZE_MAX, SIZE_MAX,
{}, {INSTALL_SWITCHES},
nullptr, nullptr,
}; };
@ -39,6 +45,8 @@ namespace vcpkg::Commands::SetInstalled
auto binaryprovider = auto binaryprovider =
create_binary_provider_from_configs(paths, args.binarysources).value_or_exit(VCPKG_LINE_INFO); create_binary_provider_from_configs(paths, args.binarysources).value_or_exit(VCPKG_LINE_INFO);
const bool dry_run = Util::Sets::contains(options.switches, OPTION_DRY_RUN);
const Build::BuildPackageOptions install_plan_options = { const Build::BuildPackageOptions install_plan_options = {
Build::UseHeadVersion::NO, Build::UseHeadVersion::NO,
Build::AllowDownloads::YES, Build::AllowDownloads::YES,
@ -76,6 +84,7 @@ namespace vcpkg::Commands::SetInstalled
// currently (or once) installed specifications // currently (or once) installed specifications
auto status_db = database_load_check(paths); auto status_db = database_load_check(paths);
std::vector<PackageSpec> specs_to_remove; std::vector<PackageSpec> specs_to_remove;
std::set<PackageSpec> specs_installed;
for (auto&& status_pgh : status_db) for (auto&& status_pgh : status_db)
{ {
if (!status_pgh->is_installed()) continue; if (!status_pgh->is_installed()) continue;
@ -86,25 +95,33 @@ namespace vcpkg::Commands::SetInstalled
{ {
specs_to_remove.push_back(status_pgh->package.spec); specs_to_remove.push_back(status_pgh->package.spec);
} }
} else
auto remove_plan = Dependencies::create_remove_plan(specs_to_remove, status_db);
for (const auto& action : remove_plan)
{ {
Remove::perform_remove_plan_action(paths, action, Remove::Purge::NO, &status_db); specs_installed.emplace(status_pgh->package.spec);
}
} }
auto real_action_plan = Dependencies::create_feature_install_plan(provider, *cmake_vars, specs, status_db); action_plan.remove_actions = Dependencies::create_remove_plan(specs_to_remove, status_db);
for (auto& action : real_action_plan.install_actions) for (const auto& action : action_plan.remove_actions)
{ {
action.build_options = install_plan_options; // This should not technically be needed, however ensuring that all specs to be removed are not included in
// `specs_installed` acts as a sanity check
specs_installed.erase(action.spec);
} }
Dependencies::print_plan(real_action_plan, true); Util::erase_remove_if(action_plan.install_actions, [&](const Dependencies::InstallPlanAction& ipa) {
return Util::Sets::contains(specs_installed, ipa.spec);
});
const auto summary = Install::perform(real_action_plan, Dependencies::print_plan(action_plan, true, paths.ports);
if (dry_run)
{
Checks::exit_success(VCPKG_LINE_INFO);
}
const auto summary = Install::perform(action_plan,
Install::KeepGoing::NO, Install::KeepGoing::NO,
paths, paths,
status_db, status_db,

View File

@ -948,7 +948,14 @@ namespace vcpkg::Dependencies
void print_plan(const ActionPlan& action_plan, const bool is_recursive, const fs::path& default_ports_dir) void print_plan(const ActionPlan& action_plan, const bool is_recursive, const fs::path& default_ports_dir)
{ {
std::vector<const RemovePlanAction*> remove_plans; if (action_plan.remove_actions.empty() && action_plan.already_installed.empty() &&
action_plan.install_actions.empty())
{
System::print2("All requested packages are currently installed.\n");
return;
}
std::set<PackageSpec> remove_specs;
std::vector<const InstallPlanAction*> rebuilt_plans; std::vector<const InstallPlanAction*> rebuilt_plans;
std::vector<const InstallPlanAction*> only_install_plans; std::vector<const InstallPlanAction*> only_install_plans;
std::vector<const InstallPlanAction*> new_plans; std::vector<const InstallPlanAction*> new_plans;
@ -962,16 +969,16 @@ namespace vcpkg::Dependencies
for (auto&& remove_action : action_plan.remove_actions) for (auto&& remove_action : action_plan.remove_actions)
{ {
remove_plans.emplace_back(&remove_action); remove_specs.emplace(remove_action.spec);
} }
for (auto&& install_action : action_plan.install_actions) for (auto&& install_action : action_plan.install_actions)
{ {
// remove plans are guaranteed to come before install plans, so we know the plan will be contained // remove plans are guaranteed to come before install plans, so we know the plan will be contained
// if at all. // if at all.
auto it = Util::find_if(remove_plans, auto it = remove_specs.find(install_action.spec);
[&](const RemovePlanAction* plan) { return plan->spec == install_action.spec; }); if (it != remove_specs.end())
if (it != remove_plans.end())
{ {
remove_specs.erase(it);
rebuilt_plans.push_back(&install_action); rebuilt_plans.push_back(&install_action);
} }
else else
@ -988,7 +995,6 @@ namespace vcpkg::Dependencies
} }
already_installed_plans = Util::fmap(action_plan.already_installed, [](auto&& action) { return &action; }); already_installed_plans = Util::fmap(action_plan.already_installed, [](auto&& action) { return &action; });
std::sort(remove_plans.begin(), remove_plans.end(), &RemovePlanAction::compare_by_name);
std::sort(rebuilt_plans.begin(), rebuilt_plans.end(), &InstallPlanAction::compare_by_name); std::sort(rebuilt_plans.begin(), rebuilt_plans.end(), &InstallPlanAction::compare_by_name);
std::sort(only_install_plans.begin(), only_install_plans.end(), &InstallPlanAction::compare_by_name); std::sort(only_install_plans.begin(), only_install_plans.end(), &InstallPlanAction::compare_by_name);
std::sort(new_plans.begin(), new_plans.end(), &InstallPlanAction::compare_by_name); std::sort(new_plans.begin(), new_plans.end(), &InstallPlanAction::compare_by_name);
@ -1019,6 +1025,16 @@ namespace vcpkg::Dependencies
'\n'); '\n');
} }
if (!remove_specs.empty())
{
std::string msg = "The following packages will be removed:\n";
for (auto spec : remove_specs)
{
Strings::append(msg, to_output_string(RequestType::USER_REQUESTED, spec.to_string()), '\n');
}
System::print2(msg);
}
if (!rebuilt_plans.empty()) if (!rebuilt_plans.empty())
{ {
System::print2("The following packages will be rebuilt:\n", actions_to_output_string(rebuilt_plans), '\n'); System::print2("The following packages will be rebuilt:\n", actions_to_output_string(rebuilt_plans), '\n');
@ -1039,8 +1055,8 @@ namespace vcpkg::Dependencies
if (has_non_user_requested_packages) if (has_non_user_requested_packages)
System::print2("Additional packages (*) will be modified to complete this operation.\n"); System::print2("Additional packages (*) will be modified to complete this operation.\n");
bool have_removals = !remove_specs.empty() || !rebuilt_plans.empty();
if (!remove_plans.empty() && !is_recursive) if (have_removals && !is_recursive)
{ {
System::print2(System::Color::warning, System::print2(System::Color::warning,
"If you are sure you want to rebuild the above packages, run the command with the " "If you are sure you want to rebuild the above packages, run the command with the "