Rework print_plan() for `remove` and `install`
MODIFY
src/commands_install.cpp
src/commands_install.cpp
@@ -140,47 +140,41 @@ namespace vcpkg::Commands::Install
static void print_plan(const std::vector<InstallPlanAction>& plan)
{
- std::vector<const InstallPlanAction*> already_installed;
- std::vector<const InstallPlanAction*> build_and_install;
- std::vector<const InstallPlanAction*> install;
+ static constexpr std::array<InstallPlanType, 3> order = { InstallPlanType::ALREADY_INSTALLED, InstallPlanType::BUILD_AND_INSTALL, InstallPlanType::INSTALL };
- for (const InstallPlanAction& i : plan)
+ std::map<InstallPlanType, std::vector<const InstallPlanAction*>> group_by_plan_type;
+ Util::group_by(plan, &group_by_plan_type, [](const InstallPlanAction& p) { return p.plan_type; });
+
+ for (const InstallPlanType plan_type : order)
+ {
+ auto it = group_by_plan_type.find(plan_type);
+ if (it == group_by_plan_type.cend())
{
- switch (i.plan_type)
+ continue;
+ }
+
+ std::vector<const InstallPlanAction*> cont = it->second;
+ std::sort(cont.begin(), cont.end(), &InstallPlanAction::compare_by_name);
+ const std::string as_string = Strings::join("\n", cont, [](const InstallPlanAction* p)
+ {
+ return Dependencies::to_output_string(p->request_type, p->spec.to_string());
+ });
+
+ switch (plan_type)
{
case InstallPlanType::ALREADY_INSTALLED:
- already_installed.push_back(&i);
+ System::println("The following packages are already installed:\n%s", as_string);
continue;
case InstallPlanType::BUILD_AND_INSTALL:
- build_and_install.push_back(&i);
+ System::println("The following packages will be built and installed:\n%s", as_string);
continue;
case InstallPlanType::INSTALL:
- install.push_back(&i);
+ System::println("The following packages will be installed:\n%s", as_string);
continue;
default:
Checks::unreachable(VCPKG_LINE_INFO);
}
}
-
- auto print_lambda = [](const InstallPlanAction* p) { return Dependencies::to_output_string(p->request_type, p->spec.to_string()); };
-
- if (!already_installed.empty())
- {
- std::sort(already_installed.begin(), already_installed.end(), &InstallPlanAction::compare_by_name);
- System::println("The following packages are already installed:\n%s", Strings::join("\n", already_installed, print_lambda));
- }
-
- if (!build_and_install.empty())
- {
- std::sort(build_and_install.begin(), build_and_install.end(), &InstallPlanAction::compare_by_name);
- System::println("The following packages will be built and installed:\n%s", Strings::join("\n", build_and_install, print_lambda));
- }
-
- if (!install.empty())
- {
- std::sort(install.begin(), install.end(), &InstallPlanAction::compare_by_name);
- System::println("The following packages will be installed:\n%s", Strings::join("\n", install, print_lambda));
- }
}
void install_package(const VcpkgPaths& paths, const BinaryParagraph& binary_paragraph, StatusParagraphs* status_db)

MODIFY
src/commands_remove.cpp
src/commands_remove.cpp
@@ -103,37 +103,38 @@ namespace vcpkg::Commands::Remove
static void print_plan(const std::vector<RemovePlanAction>& plan)
{
- std::vector<const RemovePlanAction*> not_installed;
- std::vector<const RemovePlanAction*> remove;
+ static constexpr std::array<RemovePlanType, 2> order = { RemovePlanType::NOT_INSTALLED, RemovePlanType::REMOVE };
- for (const RemovePlanAction& i : plan)
+ std::map<RemovePlanType, std::vector<const RemovePlanAction*>> group_by_plan_type;
+ Util::group_by(plan, &group_by_plan_type, [](const RemovePlanAction& p) { return p.plan_type; });
+
+ for (const RemovePlanType plan_type : order)
+ {
+ auto it = group_by_plan_type.find(plan_type);
+ if (it == group_by_plan_type.cend())
{
- switch (i.plan_type)
+ continue;
+ }
+
+ std::vector<const RemovePlanAction*> cont = it->second;
+ std::sort(cont.begin(), cont.end(), &RemovePlanAction::compare_by_name);
+ const std::string as_string = Strings::join("\n", cont, [](const RemovePlanAction* p)
+ {
+ return Dependencies::to_output_string(p->request_type, p->spec.to_string());
+ });
+
+ switch (plan_type)
{
case RemovePlanType::NOT_INSTALLED:
- not_installed.push_back(&i);
+ System::println("The following packages are not installed, so not removed:\n%s", as_string);
continue;
case RemovePlanType::REMOVE:
- remove.push_back(&i);
+ System::println("The following packages will be removed:\n%s", as_string);
continue;
default:
Checks::unreachable(VCPKG_LINE_INFO);
}
}
-
- auto print_lambda = [](const RemovePlanAction* p) { return Dependencies::to_output_string(p->request_type, p->spec.to_string()); };
-
- if (!not_installed.empty())
- {
- std::sort(not_installed.begin(), not_installed.end(), &RemovePlanAction::compare_by_name);
- System::println("The following packages are not installed, so not removed:\n%s", Strings::join("\n", not_installed, print_lambda));
- }
-
- if (!remove.empty())
- {
- std::sort(remove.begin(), remove.end(), &RemovePlanAction::compare_by_name);
- System::println("The following packages will be removed:\n%s", Strings::join("\n", remove, print_lambda));
- }
}
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths, const Triplet& default_triplet)

Rework print_plan() for `remove` and `install`