Refactor update and portsdiff
MODIFY
src/commands_portsdiff.cpp
src/commands_portsdiff.cpp
#include "vcpkg_Maps.h"
#include "SourceParagraph.h"
#include "Paragraphs.h"
+#include "ImmutableSortedVector.h"
namespace vcpkg::Commands::PortsDiff
{
- static void do_print_name_and_version(const std::vector<std::string>& ports_to_print, const std::map<std::string, version_t>& names_and_versions)
+ template <class T>
+ struct set_element_presence
{
- for (const std::string& name : ports_to_print)
+ static set_element_presence create(std::vector<T> left, std::vector<T> right)
{
- const version_t& version = names_and_versions.at(name);
- System::println("%-20s %-16s", name, version);
- }
+ // TODO: This can be done with one pass instead of three passes
+ set_element_presence output;
+ std::set_difference(left.cbegin(), left.cend(), right.cbegin(), right.cend(), std::back_inserter(output.only_left));
+ std::set_intersection(left.cbegin(), left.cend(), right.cbegin(), right.cend(), std::back_inserter(output.both));
+ std::set_difference(right.cbegin(), right.cend(), left.cbegin(), left.cend(), std::back_inserter(output.only_right));
+
+ return output;
}
- static void do_print_name_and_previous_version_and_current_version(const std::vector<std::string>& ports_to_print,
+ std::vector<T> only_left;
+ std::vector<T> both;
+ std::vector<T> only_right;
+ };
+
+ static std::vector<name_and_version_diff_t> find_updated_ports(const std::vector<std::string>& ports,
const std::map<std::string, version_t>& previous_names_and_versions,
const std::map<std::string, version_t>& current_names_and_versions)
{
- for (const std::string& name : ports_to_print)
+ std::vector<name_and_version_diff_t> output;
+ for (const std::string& name : ports)
{
const version_t& previous_version = previous_names_and_versions.at(name);
const version_t& current_version = current_names_and_versions.at(name);
- System::println("%-20s %-16s -> %s", name, previous_version, current_version);
+ if (previous_version == current_version)
+ {
+ continue;
+ }
+
+ output.push_back({ name, version_diff_t(previous_version, current_version) });
+ }
+
+ return output;
+ }
+
+ static void do_print_name_and_version(const std::vector<std::string>& ports_to_print, const std::map<std::string, version_t>& names_and_versions)
+ {
+ for (const std::string& name : ports_to_print)
+ {
+ const version_t& version = names_and_versions.at(name);
+ System::println("%-20s %-16s", name, version);
}
}
@@ -83,48 +111,32 @@ namespace vcpkg::Commands::PortsDiff
std::vector<std::string> current_ports = Maps::extract_keys(current_names_and_versions);
std::vector<std::string> previous_ports = Maps::extract_keys(previous_names_and_versions);
- std::vector<std::string> added_ports;
- std::set_difference(
- current_ports.cbegin(), current_ports.cend(),
- previous_ports.cbegin(), previous_ports.cend(),
- std::back_inserter(added_ports));
+ const set_element_presence<std::string> setp = set_element_presence<std::string>::create(current_ports, previous_ports);
+ const std::vector<std::string>& added_ports = setp.only_left;
if (!added_ports.empty())
{
System::println("\nThe following %d ports were added:\n", added_ports.size());
do_print_name_and_version(added_ports, current_names_and_versions);
}
- std::vector<std::string> removed_ports;
- std::set_difference(
- previous_ports.cbegin(), previous_ports.cend(),
- current_ports.cbegin(), current_ports.cend(),
- std::back_inserter(removed_ports));
-
+ const std::vector<std::string>& removed_ports = setp.only_right;
if (!removed_ports.empty())
{
System::println("\nThe following %d ports were removed:\n", removed_ports.size());
do_print_name_and_version(removed_ports, previous_names_and_versions);
}
- std::vector<std::string> potentially_updated_ports;
- std::set_intersection(
- current_ports.cbegin(), current_ports.cend(),
- previous_ports.cbegin(), previous_ports.cend(),
- std::back_inserter(potentially_updated_ports));
-
- std::vector<std::string> updated_ports;
- std::copy_if(potentially_updated_ports.cbegin(), potentially_updated_ports.cend(), std::back_inserter(updated_ports),
- [&](const std::string& port) -> bool
- {
- return current_names_and_versions.at(port) != previous_names_and_versions.at(port);
- }
- );
+ const std::vector<std::string>& common_ports = setp.both;
+ const std::vector<name_and_version_diff_t> updated_ports = find_updated_ports(common_ports, previous_names_and_versions, current_names_and_versions);
if (!updated_ports.empty())
{
System::println("\nThe following %d ports were updated:\n", updated_ports.size());
- do_print_name_and_previous_version_and_current_version(updated_ports, previous_names_and_versions, current_names_and_versions);
+ for (const name_and_version_diff_t& p : updated_ports)
+ {
+ System::println("%-20s %-16s", p.name, p.version_diff.toString());
+ }
}
if (added_ports.empty() && removed_ports.empty() && updated_ports.empty())

MODIFY
src/commands_update.cpp
src/commands_update.cpp
namespace vcpkg::Commands::Update
{
- std::map<std::string, version_diff_t> find_outdated_packages(const StatusParagraphs& status_db)
+ std::vector<name_and_version_diff_t> find_outdated_packages(const vcpkg_paths& paths, const StatusParagraphs& status_db)
{
- std::map<std::string, version_diff_t> output;
- for (auto&& pgh : status_db)
+ const std::vector<SourceParagraph> source_paragraphs = Paragraphs::load_all_ports(paths.ports);
+ const std::map<std::string, version_t> src_names_to_versions = Paragraphs::extract_port_names_and_versions(source_paragraphs);
+ const std::vector<StatusParagraph*> installed_packages = get_installed_ports(status_db);
+
+ std::vector<name_and_version_diff_t> output;
+ for (const StatusParagraph* pgh : installed_packages)
{
- if (pgh->state == install_state_t::not_installed && pgh->want == want_t::purge)
- continue;
auto it = src_names_to_versions.find(pgh->package.spec.name());
if (it == src_names_to_versions.end())
{
@@ -22,14 +24,11 @@ namespace vcpkg::Commands::Update
}
if (it->second != pgh->package.version)
{
- packages_output.push_back(Strings::format("%-27s %s -> %s",
- pgh->package.displayname(),
- pgh->package.version,
- it->second));
- packages_list.append(" " + pgh->package.displayname());
+ output.push_back({ pgh->package.displayname(), version_diff_t(pgh->package.version, it->second) });
}
}
- std::sort(packages_output.begin(), packages_output.end());
+
+ return output;
}
void perform_and_exit(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths)
@@ -38,41 +37,21 @@ namespace vcpkg::Commands::Update
args.check_and_get_optional_command_arguments({});
System::println("Using local portfile versions. To update the local portfiles, use `git pull`.");
- StatusParagraphs status_db = database_load_check(paths);
+ const StatusParagraphs status_db = database_load_check(paths);
- const std::vector<SourceParagraph> source_paragraphs = Paragraphs::load_all_ports(paths.ports);
- const std::map<std::string, version_t> src_names_to_versions = Paragraphs::extract_port_names_and_versions(source_paragraphs);
-
- std::vector<StatusParagraph*> installed_packages = get_installed_ports(status_db);
+ const auto outdated_packages = ImmutableSortedVector<name_and_version_diff_t>::create(find_outdated_packages(paths, status_db),
+ &name_and_version_diff_t::compare_by_name);
- std::vector<std::string> packages_output;
- for (const StatusParagraph* pgh : installed_packages)
- {
- auto it = src_names_to_versions.find(pgh->package.spec.name());
- if (it == src_names_to_versions.end())
- {
- // Package was not installed from portfile
- continue;
- }
- if (it->second != pgh->package.version)
- {
- packages_output.push_back(Strings::format("%-27s %s -> %s",
- pgh->package.displayname(),
- pgh->package.version,
- it->second));
- }
- }
- std::sort(packages_output.begin(), packages_output.end());
- if (packages_output.empty())
+ if (outdated_packages.empty())
{
System::println("No packages need updating.");
}
else
{
System::println("The following packages differ from their port versions:");
- for (auto&& package : packages_output)
+ for (auto&& package : outdated_packages)
{
- System::println(" %s", package);
+ System::println(" %-27s %s", package.name, package.version_diff.toString());
}
System::println("\nTo update these packages, run\n vcpkg remove --purge <pkgs>...\n vcpkg install <pkgs>...");
}

Refactor update and portsdiff