From d30e6ed759b1be4bfe06df280ad2e6d01fd64f79 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 26 Sep 2023 14:36:40 -0300 Subject: [PATCH] some more refactor --- src2/collection.cpp | 6 +++ src2/collection.h | 23 ++-------- src2/component.cpp | 100 +++++++++++++++++++++++++++++++++++++++----- src2/component.h | 11 +++++ src2/download.cpp | 2 - src2/download.h | 1 + 6 files changed, 111 insertions(+), 32 deletions(-) diff --git a/src2/collection.cpp b/src2/collection.cpp index 83038ae..1e24878 100644 --- a/src2/collection.cpp +++ b/src2/collection.cpp @@ -2,6 +2,8 @@ constexpr std::string_view downloads("downloads"); +static const std::regex remote("(^https?):\\/\\/"); + Collection::Collection( const std::filesystem::path& destination, const std::string& target, @@ -71,6 +73,10 @@ void Collection::sourceBuilt(const std::string& source, TaskManager::Error err) successSources.emplace(source); } +bool Collection::isRemote(const std::string& source) { + return std::regex_search(source, remote); +} + void Collection::queueDownload(const std::string& source) { std::pair res = downloadingSources.emplace(std::piecewise_construct, std::forward_as_tuple(source), diff --git a/src2/collection.h b/src2/collection.h index b0b099c..b8c2f84 100644 --- a/src2/collection.h +++ b/src2/collection.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "loggable.h" #include "component.h" @@ -16,11 +17,8 @@ class Collection : protected Loggable { using Sources = std::set; using Downloads = std::map>; using Components = std::map>; -public: - class UnknownSource; - class DuplicateSource; - class DuplicatePath; +public: Collection( const std::filesystem::path& destination, const std::string& target, @@ -41,7 +39,7 @@ private: void sourceRead(const std::string& source, TaskManager::Error err); void sourceBuilt(const std::string& source, TaskManager::Error err); - bool isRemote(const std::string& source); + static bool isRemote(const std::string& source); void queueDownload(const std::string& source); void queueRead(const std::string& source, const std::filesystem::path& location); @@ -56,18 +54,3 @@ private: Components readingSources; Components buildingSources; }; - -class Collection::UnknownSource : public std::runtime_error { -public: - explicit UnknownSource(const std::string& source); -}; - -class Collection::DuplicateSource : public std::runtime_error { -public: - explicit DuplicateSource(const std::string& source); -}; - -class Collection::DuplicatePath : public std::runtime_error { -public: - explicit DuplicatePath(const std::string& source, const std::filesystem::path& path); -}; diff --git a/src2/component.cpp b/src2/component.cpp index a4c7c6b..f89f203 100644 --- a/src2/component.cpp +++ b/src2/component.cpp @@ -1,10 +1,9 @@ #include "component.h" -#include -#include - #include "collection.h" +constexpr std::string_view masonJSON("mason.json"); +constexpr std::string_view dependencies("dependencies"); constexpr std::array stringStates { "initial", "reading", @@ -20,21 +19,102 @@ Component::Component( const std::shared_ptr& logger ): Loggable(logger), + state(initial), + type(unknown), collection(collection), - location(path) + location(path), + scenario(std::nullopt) {} void Component::read() { if (state != initial) throw WrongState(state, "read"); - // try { - // collection->addSource(location); - // } catch (const Collection::DuplicateSource e) { - // state = error; - // throw e; - // } state = reading; + + std::filesystem::file_status status = std::filesystem::status(location); + switch (status.type()) { + case std::filesystem::file_type::directory: + type = directory; + state = ready; + tryReadingBuildScenarios(); + break; + case std::filesystem::file_type::regular: + type = file; + state = ready; + break; + default: + warn(location.string() + " doesn't appear to be a file nor a directory"); + state = error; + break; + } +} + +void Component::tryReadingBuildScenarios() { + if (readAsMason()) + return; +} + +bool Component::readAsMason() { + std::filesystem::path masonScenarion = location/masonJSON; + try { + std::ifstream file(masonScenarion); + if (file.is_open()) + scenario = nlohmann::json::parse(file); + else + minor("Couldn't open " + masonScenarion.string()); + } catch (const nlohmann::json::exception& e) { + major("Couldn't parse " + masonScenarion.string()); + } + + if (scenario.has_value()) { + const nlohmann::json& sc = scenario.value(); + if (!sc.is_object()) + return errorScenario(masonScenarion.string() + " is unexpected root type"); + + nlohmann::json::const_iterator itr = sc.find(dependencies); + if (itr == sc.end()) { + info(masonScenarion.string() + " doesn't have dependencies"); + } else { + const nlohmann::json& deps = *itr; + if (!deps.is_array()) + return errorScenario(masonScenarion.string() + " dependencies are not organized as an array"); + + for (const nlohmann::json& dep : deps) { + switch (dep.type()) { + case nlohmann::json::value_t::string: + collection->addSource(dep); + break; + case nlohmann::json::value_t::object: { + nlohmann::json::const_iterator ditr = dep.find("path"); + if (ditr == dep.end()) + return errorScenario(masonScenarion.string() + " object of unexpected format in dependecies"); + + nlohmann::json path = *ditr; + if (!path.is_string()) + return errorScenario(masonScenarion.string() + " object of unexpected format in dependecies"); + + collection->addSource(path); + } + break; + default: + return errorScenario(masonScenarion.string() + " has unexpected entries in dependecies"); + } + } + } + + type = mason; + return true; + } + + return false; +} + +bool Component::errorScenario(const std::string& message) { + major(message); + scenario = std::nullopt; + return false; + return false; } void Component::build(const std::filesystem::path& destination, const std::string& target) { diff --git a/src2/component.h b/src2/component.h index 0b7daf6..d24b034 100644 --- a/src2/component.h +++ b/src2/component.h @@ -2,9 +2,14 @@ #include #include +#include #include #include +#include #include +#include +#include +#include #include "loggable.h" #include "loggger.h" @@ -42,11 +47,17 @@ public: void read(); void build(const std::filesystem::path& destination, const std::string& target); +private: + void tryReadingBuildScenarios(); + bool readAsMason(); + bool errorScenario(const std::string& message); + private: State state; Type type; std::shared_ptr collection; std::filesystem::path location; + std::optional scenario; }; class Component::WrongState : public std::runtime_error { diff --git a/src2/download.cpp b/src2/download.cpp index 0d21c17..a96dd00 100644 --- a/src2/download.cpp +++ b/src2/download.cpp @@ -1,7 +1,5 @@ #include "download.h" -#include - constexpr std::string_view archived("archived"); constexpr std::string_view headerAcceptJson("accept: application/json"); diff --git a/src2/download.h b/src2/download.h index 6c74d9a..d5deb06 100644 --- a/src2/download.h +++ b/src2/download.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include