-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split functions to avoid 'is_manifest' control coupling. #1229
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
|
||
#include <tuple> | ||
|
||
using namespace vcpkg; | ||
|
||
static std::atomic<uint64_t> g_load_ports_stats(0); | ||
|
||
namespace vcpkg | ||
|
@@ -379,46 +381,45 @@ namespace vcpkg::Paragraphs | |
return fs.exists(maybe_directory / "CONTROL", IgnoreErrors{}) || | ||
fs.exists(maybe_directory / "vcpkg.json", IgnoreErrors{}); | ||
} | ||
} // namespace vcpkg::Paragraphs | ||
|
||
static ExpectedL<std::unique_ptr<SourceControlFile>> try_load_manifest_text(const std::string& text, | ||
StringView origin, | ||
MessageSink& warning_sink) | ||
namespace | ||
{ | ||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_any_manifest_text( | ||
StringView text, | ||
StringView origin, | ||
MessageSink& warning_sink, | ||
ParseExpected<SourceControlFile> (*do_parse)(StringView, const Json::Object&, MessageSink&)) | ||
{ | ||
auto res = Json::parse(text, origin); | ||
if (auto val = res.get()) | ||
{ | ||
if (val->value.is_object()) | ||
{ | ||
return SourceControlFile::parse_port_manifest_object( | ||
origin, val->value.object(VCPKG_LINE_INFO), warning_sink) | ||
.map_error(ToLocalizedString); | ||
} | ||
StatsTimer timer(g_load_ports_stats); | ||
return Json::parse_object(text, origin).then([&](Json::Object&& object) { | ||
return do_parse(origin, std::move(object), warning_sink).map_error(ToLocalizedString); | ||
}); | ||
} | ||
} | ||
|
||
return msg::format(msgJsonValueNotObject); | ||
} | ||
namespace vcpkg::Paragraphs | ||
{ | ||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_project_manifest_text(StringView text, | ||
StringView origin, | ||
MessageSink& warning_sink) | ||
{ | ||
return try_load_any_manifest_text(text, origin, warning_sink, SourceControlFile::parse_project_manifest_object); | ||
} | ||
|
||
return LocalizedString::from_raw(res.error()->to_string()); | ||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_port_manifest_text(StringView text, | ||
StringView origin, | ||
MessageSink& warning_sink) | ||
{ | ||
return try_load_any_manifest_text(text, origin, warning_sink, SourceControlFile::parse_port_manifest_object); | ||
} | ||
|
||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_port_text(const std::string& text, | ||
StringView origin, | ||
bool is_manifest, | ||
MessageSink& warning_sink) | ||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_control_file_text(StringView text, StringView origin) | ||
{ | ||
StatsTimer timer(g_load_ports_stats); | ||
|
||
if (is_manifest) | ||
{ | ||
return try_load_manifest_text(text, origin, warning_sink); | ||
} | ||
|
||
auto pghs = parse_paragraphs(StringView{text}, origin); | ||
if (auto vector_pghs = pghs.get()) | ||
{ | ||
return SourceControlFile::parse_control_file(origin, std::move(*vector_pghs)).map_error(ToLocalizedString); | ||
} | ||
|
||
return std::move(pghs).error(); | ||
return parse_paragraphs(text, origin).then([&](std::vector<Paragraph>&& vector_pghs) { | ||
return SourceControlFile::parse_control_file(origin, std::move(vector_pghs)).map_error(ToLocalizedString); | ||
}); | ||
} | ||
|
||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_port(const ReadOnlyFilesystem& fs, | ||
|
@@ -433,37 +434,47 @@ namespace vcpkg::Paragraphs | |
auto manifest_contents = fs.read_contents(manifest_path, ec); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this function should be able to be written with less nesting, perhaps along the lines of: std::error_code ec_manifest, ec_control;
auto content_manifest = fs.read_contents(path_manifest, ec_manifest);
const auto exists_manifest = !ec_manifest;
const auto failed_manifest = ec_manifest && ec_manifest != std::errc::no_such_file_or_directory;
auto content_control = fs.read_contents(path_control, ec_control);
const auto exists_control = !ec_control;
const auto failed_control = ec_control && ec_control != std::errc::no_such_file_or_directory;
if (failed_manifest) {
// msgFailedToAccessManifest(path_manifest)
} else if (failed_control) {
// msgFailedToAccessManifest(path_control)
} else if (exists_manifest && exists_control) {
// msgManifestConflict(port_directory)
} else if (exists_manifest) {
// success on manifest
} else if (exists_control) {
// success on control
} else {
// msgPortMissingManifest2(port_directory)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this transformation as it does a |
||
if (ec) | ||
{ | ||
const auto exists = ec != std::errc::no_such_file_or_directory; | ||
if (exists) | ||
auto manifest_exists = ec != std::errc::no_such_file_or_directory; | ||
if (manifest_exists) | ||
{ | ||
return msg::format_error(msgFailedToParseManifest, msg::path = manifest_path) | ||
.append_raw("\n") | ||
.append(format_filesystem_call_error(ec, "read_contents", {manifest_path})); | ||
} | ||
|
||
if (fs.exists(control_path, IgnoreErrors{})) | ||
auto control_contents = fs.read_contents(control_path, ec); | ||
if (ec) | ||
{ | ||
return get_paragraphs(fs, control_path).then([&](std::vector<Paragraph>&& vector_pghs) { | ||
return SourceControlFile::parse_control_file(control_path, std::move(vector_pghs)) | ||
.map_error(ToLocalizedString); | ||
}); | ||
} | ||
if (ec != std::errc::no_such_file_or_directory) | ||
{ | ||
return LocalizedString::from_raw(port_directory) | ||
.append_raw(": ") | ||
.append(format_filesystem_call_error(ec, "read_contents", {control_path})); | ||
} | ||
|
||
if (fs.exists(port_directory, IgnoreErrors{})) | ||
{ | ||
return msg::format_error( | ||
msgPortMissingManifest, msg::package_name = port_name, msg::path = port_directory); | ||
if (fs.exists(port_directory, IgnoreErrors{})) | ||
{ | ||
return LocalizedString::from_raw(port_directory) | ||
.append_raw(": ") | ||
.append(msgErrorMessage) | ||
.append(msgPortMissingManifest2, msg::package_name = port_name); | ||
} | ||
|
||
return LocalizedString::from_raw(port_directory) | ||
.append_raw(": ") | ||
.append(msgErrorMessage) | ||
.append(msgPortDoesNotExist, msg::package_name = port_name); | ||
} | ||
|
||
return std::unique_ptr<SourceControlFile>(); | ||
return try_load_control_file_text(control_contents, control_path); | ||
} | ||
|
||
if (fs.exists(control_path, IgnoreErrors{})) | ||
{ | ||
return msg::format_error(msgManifestConflict, msg::path = port_directory); | ||
} | ||
|
||
return try_load_manifest_text(manifest_contents, manifest_path, stdout_sink); | ||
return try_load_port_manifest_text(manifest_contents, manifest_path, stdout_sink); | ||
} | ||
|
||
ExpectedL<std::unique_ptr<SourceControlFile>> try_load_port_required(const ReadOnlyFilesystem& fs, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this pattern will be common enough to warrant a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be worth it but I'd rather do it as its own PR rather than expanding the scope of this one