From b92b66c101f31c0ade2ad90fdeba6d860896fe23 Mon Sep 17 00:00:00 2001 From: blue Date: Sun, 17 Sep 2023 16:34:00 -0300 Subject: [PATCH] just some refactoring thoughts --- CMakeLists.txt | 1 + src/dependency.cpp | 21 +++++++++++++--- src/dependency.h | 2 ++ src/project.cpp | 25 +++++++++++++++++-- src/project.h | 3 +++ src2/CMakeLists.txt | 11 +++++++++ src2/collection.cpp | 1 + src2/collection.h | 59 +++++++++++++++++++++++++++++++++++++++++++++ src2/component.cpp | 1 + src2/component.h | 40 ++++++++++++++++++++++++++++++ 10 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 src2/CMakeLists.txt create mode 100644 src2/collection.cpp create mode 100644 src2/collection.h create mode 100644 src2/component.cpp create mode 100644 src2/component.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 24e3875..1c70c23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) add_subdirectory(src) +add_subdirectory(src2) add_subdirectory(test) target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json) diff --git a/src/dependency.cpp b/src/dependency.cpp index 3fe4033..b282dc2 100644 --- a/src/dependency.cpp +++ b/src/dependency.cpp @@ -5,6 +5,7 @@ #include "project.h" constexpr std::string_view downloads("downloads"); +constexpr std::string_view sources("sources"); constexpr std::string_view build("build"); constexpr std::string_view acceptJson("accept: application/json"); @@ -20,7 +21,8 @@ Dependency::Dependency( path(path), type(type), name(name), - version(version) + version(version), + location() {} Dependency::Type Dependency::getType() const { @@ -35,6 +37,13 @@ std::optional Dependency::getVersion() const { return version; } +std::optional Dependency::getLocation() const { + if (location.empty()) + return std::nullopt; + else + return location; +} + bool Dependency::prepare(const std::filesystem::path& source, const std::filesystem::path& destination) { std::smatch results; if (std::regex_search(path, results, repo)) { @@ -60,8 +69,10 @@ bool Dependency::prepare(const std::filesystem::path& source, const std::filesys case Type::mason: if (std::filesystem::exists(temp/"mason.json")) break; - else + else { Project::warn("Project " + path + " is supposed to me a mason project, but no mason.json was found in the project directory"); + success = false; + } break; case Type::simple: @@ -108,7 +119,8 @@ bool Dependency::downloadRepo( if (res) { Project::info("Successfully downloaded " + archivePath.string()); - res = extract(archivePath, destination/build); + location = destination/sources; + res = extract(archivePath, location); if (res) { out = destination/build/repo; if (!std::filesystem::is_directory(out)) { @@ -119,6 +131,9 @@ bool Dependency::downloadRepo( Project::error("Couldn't extract archive " + fileName); } + if (!res) + location.clear(); + return res; } } else { diff --git a/src/dependency.h b/src/dependency.h index bd19565..5e9e95d 100644 --- a/src/dependency.h +++ b/src/dependency.h @@ -29,6 +29,7 @@ public: Type getType() const; std::optional getName() const; std::optional getVersion() const; + std::optional getLocation() const; bool prepare(const std::filesystem::path& source, const std::filesystem::path& destination); @@ -64,4 +65,5 @@ private: Type type; std::optional name; std::optional version; + std::filesystem::path location; }; diff --git a/src/project.cpp b/src/project.cpp index 44cca5d..c1f0de8 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -18,6 +18,7 @@ std::map types({ Logger* Project::logger = nullptr; Project::Project(const std::filesystem::path& location, const std::filesystem::path& destination): + parent(nullptr), location(location), destination(destination), state(State::unknown), @@ -31,6 +32,15 @@ Project::Project(const std::filesystem::path& location, const std::filesystem::p } } +Project::Project(const std::filesystem::path& location, const std::filesystem::path& destination, Project* parent): + parent(parent), + location(location), + destination(destination), + state(State::unknown), + name(), + dependencies(), + root(false) {} + Project::~Project() { if (root) { curl_global_cleanup(); @@ -156,9 +166,20 @@ void Project::createDepencencyFromObject(const nlohmann::json& entry) { void Project::discover() { int fine = 0; for (std::pair& pair : dependencies) { - bool success = pair.second.prepare(location, destination); - if (success) + Dependency& dep = pair.second; + bool success = dep.prepare(location, destination); + if (success) { + switch (dep.getType()) { + case Dependency::Type::mason: + break; + case Dependency::Type::simple: + break; + default: + break; + } + fine++; + } } diff --git a/src/project.h b/src/project.h index a5e1fe3..97da3c2 100644 --- a/src/project.h +++ b/src/project.h @@ -44,11 +44,14 @@ public: static void addProject(const std::filesystem::path& location, const std::filesystem::path& destination); private: + Project(const std::filesystem::path& location, const std::filesystem::path& destination, Project* parent); + void parse(const nlohmann::json& json); void createDepencencyFromString(const std::string& entry); void createDepencencyFromObject(const nlohmann::json& entry); private: + Project* parent; std::filesystem::path location; std::filesystem::path destination; State state; diff --git a/src2/CMakeLists.txt b/src2/CMakeLists.txt new file mode 100644 index 0000000..1517ac0 --- /dev/null +++ b/src2/CMakeLists.txt @@ -0,0 +1,11 @@ +set(SOURCES + component.cpp + collection.cpp +) + +set(HEADERS + component.h + collection.h +) + +target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/src2/collection.cpp b/src2/collection.cpp new file mode 100644 index 0000000..3f2a2db --- /dev/null +++ b/src2/collection.cpp @@ -0,0 +1 @@ +#include "collection.h" diff --git a/src2/collection.h b/src2/collection.h new file mode 100644 index 0000000..b6f8c1b --- /dev/null +++ b/src2/collection.h @@ -0,0 +1,59 @@ +#pragma once + +#include +#include +#include +#include +#include + +class Collection { +public: + class UnknownSource; + class DuplicateSource; + class DuplicatePath; + + Collection(); + + unsigned int sourcesTotal() const; + unsigned int sourcesPending() const; + unsigned int sourcesError() const; + unsigned int sourcesSuccess() const; + + bool hasSource(const std::string& source) const; + void addSource(const std::string& source); + void sourceError(const std::string& source); + void sourceSuccess(const std::string& source, const std::filesystem::path& path); + +private: + std::set errorSources; + std::set pendingSources; + std::map successSources; //would be nice to have a bimap here +}; + +class Collection::UnknownSource : public std::exception { +public: + UnknownSource(const std::string& source); + + const char* what() const noexcept( true ) override; +private: + std::string source; +}; + +class Collection::DuplicateSource : public std::exception { +public: + DuplicateSource(const std::string& source); + + const char* what() const noexcept( true ) override; +private: + std::string source; +}; + +class Collection::DuplicatePath : public std::exception { +public: + DuplicatePath(const std::string& source, const std::filesystem::path& path); + + const char* what() const noexcept( true ) override; +private: + std::string source; + std::filesystem::path path; +}; diff --git a/src2/component.cpp b/src2/component.cpp new file mode 100644 index 0000000..fb8a3a6 --- /dev/null +++ b/src2/component.cpp @@ -0,0 +1 @@ +#include "component.h" diff --git a/src2/component.h b/src2/component.h new file mode 100644 index 0000000..9ab4c5b --- /dev/null +++ b/src2/component.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +#include "loggger.h" +#include "collection.h" + +class Component { +public: + enum State { + initial, + reading, + building, + ready + }; + + enum Type { + unknown, + file, + directory, + mason + }; + + Component(Logger* logger, Collection* collection); + virtual ~Component(); + + Type getType() const; + Type getState() const; + + virtual void read(const std::filesystem::path& path) = 0; + virtual void build(const std::filesystem::path& destination, const std::string& target) = 0; + +protected: + State state; + Type type; + Logger* logger; + Collection* collection; + std::filesystem::path location; +};