Move some functions from vcpkglib.h to Paragraphs.h

This commit is contained in:
Alexander Karatarakis 2017-02-27 15:45:56 -08:00
parent 00737588cc
commit c0ae9fee7e
10 changed files with 74 additions and 73 deletions

View File

@ -2,9 +2,18 @@
#include "filesystem_fs.h"
#include <unordered_map>
#include "expected.h"
#include "BinaryParagraph.h"
#include "vcpkg_paths.h"
namespace vcpkg::Paragraphs
{
std::vector<std::unordered_map<std::string, std::string>> get_paragraphs(const fs::path& control_path);
std::vector<std::unordered_map<std::string, std::string>> parse_paragraphs(const std::string& str);
expected<SourceParagraph> try_load_port(const fs::path& control_path);
expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec);
std::vector<SourceParagraph> load_all_ports(const fs::path& ports_dir);
}

View File

@ -1,7 +1,5 @@
#pragma once
#include "package_spec.h"
#include "BinaryParagraph.h"
#include "StatusParagraphs.h"
#include "vcpkg_paths.h"
#include "ImmutableSortedVector.h"
@ -19,10 +17,4 @@ namespace vcpkg
};
std::vector<StatusParagraph_and_associated_files> get_installed_files(const vcpkg_paths& paths, const StatusParagraphs& status_db);
expected<SourceParagraph> try_load_port(const fs::path& control_path);
expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec);
std::vector<SourceParagraph> load_all_ports(const fs::path& ports_dir);
} // namespace vcpkg

View File

@ -6,9 +6,7 @@ namespace vcpkg::Paragraphs
{
struct Parser
{
Parser(const char* c, const char* e) : cur(c), end(e)
{
}
Parser(const char* c, const char* e) : cur(c), end(e) { }
private:
const char* cur;
@ -167,4 +165,56 @@ namespace vcpkg::Paragraphs
{
return Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs();
}
expected<SourceParagraph> try_load_port(const fs::path& path)
{
try
{
auto pghs = get_paragraphs(path / "CONTROL");
Checks::check_exit(pghs.size() == 1, "Invalid control file at %s\\CONTROL", path.string());
return SourceParagraph(pghs[0]);
}
catch (std::runtime_error const&) {}
return std::errc::no_such_file_or_directory;
}
expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec)
{
const fs::path path = paths.package_dir(spec) / "CONTROL";
auto control_contents_maybe = Files::read_contents(path);
if (auto control_contents = control_contents_maybe.get())
{
std::vector<std::unordered_map<std::string, std::string>> pghs;
try
{
pghs = parse_paragraphs(*control_contents);
}
catch (std::runtime_error) {}
Checks::check_exit(pghs.size() == 1, "Invalid control file at %s", path.string());
return BinaryParagraph(pghs[0]);
}
return control_contents_maybe.error_code();
}
std::vector<SourceParagraph> load_all_ports(const fs::path& ports_dir)
{
std::vector<SourceParagraph> output;
for (auto it = fs::directory_iterator(ports_dir); it != fs::directory_iterator(); ++it)
{
const fs::path& path = it->path();
expected<SourceParagraph> source_paragraph = try_load_port(path);
if (auto srcpgh = source_paragraph.get())
{
output.emplace_back(std::move(*srcpgh));
}
else
{
Checks::exit_with_message("Error loading port from %s: %s", path.generic_string(), source_paragraph.error_code().message());
}
}
return output;
}
}

View File

@ -9,6 +9,7 @@
#include "vcpkg_Environment.h"
#include "metrics.h"
#include "vcpkg_Enums.h"
#include "PostBuildLint_BuildInfo.h"
namespace vcpkg::Commands::Build
{
@ -127,7 +128,7 @@ namespace vcpkg::Commands::Build
exit(EXIT_SUCCESS);
}
const expected<SourceParagraph> maybe_spgh = try_load_port(port_dir);
const expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(port_dir);
Checks::check_exit(!maybe_spgh.error_code(), "Could not find package named %s: %s", spec, maybe_spgh.error_code().message());
const SourceParagraph& spgh = *maybe_spgh.get();

View File

@ -7,6 +7,7 @@
#include "vcpkg_Dependencies.h"
#include "vcpkg_Input.h"
#include "vcpkg_Chrono.h"
#include "PostBuildLint_BuildInfo.h"
namespace vcpkg::Commands::CI
{
@ -70,7 +71,7 @@ namespace vcpkg::Commands::CI
System::println(System::color::error, Build::create_error_message(result, action.spec));
continue;
}
const BinaryParagraph bpgh = try_load_cached_package(paths, action.spec).get_or_throw();
const BinaryParagraph bpgh = Paragraphs::try_load_cached_package(paths, action.spec).get_or_throw();
Install::install_package(paths, bpgh, &status_db);
System::println(System::color::success, "Package %s is installed", action.spec);
}

View File

@ -7,6 +7,7 @@
#include "vcpkg_System.h"
#include "vcpkg_Dependencies.h"
#include "vcpkg_Input.h"
#include "PostBuildLint_BuildInfo.h"
namespace vcpkg::Commands::Install
{
@ -224,7 +225,7 @@ namespace vcpkg::Commands::Install
System::println(Build::create_user_troubleshooting_message(action.spec));
exit(EXIT_FAILURE);
}
const BinaryParagraph bpgh = try_load_cached_package(paths, action.spec).get_or_throw();
const BinaryParagraph bpgh = Paragraphs::try_load_cached_package(paths, action.spec).get_or_throw();
install_package(paths, bpgh, &status_db);
System::println(System::color::success, "Package %s is installed", action.spec);
}

View File

@ -4,7 +4,7 @@
#include "vcpkg_Maps.h"
#include "SourceParagraph.h"
#include "vcpkg_Environment.h"
#include "vcpkglib.h"
#include "PostBuildLint_BuildInfo.h"
namespace vcpkg::Commands::PortsDiff
{
@ -44,7 +44,7 @@ namespace vcpkg::Commands::PortsDiff
{
std::map<std::string, std::string> names_and_versions;
std::vector<SourceParagraph> ports = load_all_ports(ports_folder_path);
std::vector<SourceParagraph> ports = Paragraphs::load_all_ports(ports_folder_path);
for (SourceParagraph& port : ports)
{
names_and_versions.emplace(std::move(port.name), std::move(port.version));

View File

@ -4,7 +4,6 @@
#include "Paragraphs.h"
#include "vcpkglib_helpers.h"
#include "SourceParagraph.h"
#include "vcpkglib.h"
namespace vcpkg::Commands::Search
{
@ -60,7 +59,7 @@ namespace vcpkg::Commands::Search
args.check_max_arg_count(1, example);
const std::unordered_set<std::string> options = args.check_and_get_optional_command_arguments({ OPTION_GRAPH });
const std::vector<SourceParagraph> source_paragraphs = load_all_ports(paths.ports);
const std::vector<SourceParagraph> source_paragraphs = Paragraphs::load_all_ports(paths.ports);
if (options.find(OPTION_GRAPH) != options.cend())
{
const std::string graph_as_string = create_graph_as_string(source_paragraphs);

View File

@ -5,7 +5,7 @@
#include "package_spec.h"
#include "StatusParagraphs.h"
#include "vcpkg_Files.h"
#include "vcpkglib.h"
#include "PostBuildLint_BuildInfo.h"
namespace vcpkg::Dependencies
{
@ -72,7 +72,7 @@ namespace vcpkg::Dependencies
continue;
}
expected<BinaryParagraph> maybe_bpgh = try_load_cached_package(paths, spec);
expected<BinaryParagraph> maybe_bpgh = Paragraphs::try_load_cached_package(paths, spec);
if (BinaryParagraph* bpgh = maybe_bpgh.get())
{
process_dependencies(bpgh->depends);
@ -80,7 +80,7 @@ namespace vcpkg::Dependencies
continue;
}
expected<SourceParagraph> maybe_spgh = try_load_port(paths.port_dir(spec));
expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(paths.port_dir(spec));
SourceParagraph* spgh = maybe_spgh.get();
Checks::check_exit(spgh != nullptr, "Cannot find package %s", spec.name());
process_dependencies(filter_dependencies(spgh->depends, spec.target_triplet()));

View File

@ -200,56 +200,4 @@ namespace vcpkg
return installed_files;
}
expected<SourceParagraph> try_load_port(const fs::path& path)
{
try
{
auto pghs = Paragraphs::get_paragraphs(path / "CONTROL");
Checks::check_exit(pghs.size() == 1, "Invalid control file at %s\\CONTROL", path.string());
return SourceParagraph(pghs[0]);
}
catch (std::runtime_error const&) { }
return std::errc::no_such_file_or_directory;
}
expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec)
{
const fs::path path = paths.package_dir(spec) / "CONTROL";
auto control_contents_maybe = Files::read_contents(path);
if (auto control_contents = control_contents_maybe.get())
{
std::vector<std::unordered_map<std::string, std::string>> pghs;
try
{
pghs = Paragraphs::parse_paragraphs(*control_contents);
}
catch (std::runtime_error) { }
Checks::check_exit(pghs.size() == 1, "Invalid control file at %s", path.string());
return BinaryParagraph(pghs[0]);
}
return control_contents_maybe.error_code();
}
std::vector<SourceParagraph> load_all_ports(const fs::path& ports_dir)
{
std::vector<SourceParagraph> output;
for (auto it = fs::directory_iterator(ports_dir); it != fs::directory_iterator(); ++it)
{
const fs::path& path = it->path();
expected<SourceParagraph> source_paragraph = try_load_port(path);
if (auto srcpgh = source_paragraph.get())
{
output.emplace_back(std::move(*srcpgh));
}
else
{
Checks::exit_with_message("Error loading port from %s: %s", path.generic_string(), source_paragraph.error_code().message());
}
}
return output;
}
}