just some refactoring thoughts

This commit is contained in:
Blue 2023-09-17 16:34:00 -03:00
parent 198677fc18
commit b92b66c101
Signed by: blue
GPG Key ID: 9B203B252A63EE38
10 changed files with 159 additions and 5 deletions

View File

@ -41,6 +41,7 @@ set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(src2)
add_subdirectory(test) add_subdirectory(test)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json) target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)

View File

@ -5,6 +5,7 @@
#include "project.h" #include "project.h"
constexpr std::string_view downloads("downloads"); constexpr std::string_view downloads("downloads");
constexpr std::string_view sources("sources");
constexpr std::string_view build("build"); constexpr std::string_view build("build");
constexpr std::string_view acceptJson("accept: application/json"); constexpr std::string_view acceptJson("accept: application/json");
@ -20,7 +21,8 @@ Dependency::Dependency(
path(path), path(path),
type(type), type(type),
name(name), name(name),
version(version) version(version),
location()
{} {}
Dependency::Type Dependency::getType() const { Dependency::Type Dependency::getType() const {
@ -35,6 +37,13 @@ std::optional<std::string> Dependency::getVersion() const {
return version; return version;
} }
std::optional<std::filesystem::path> 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) { bool Dependency::prepare(const std::filesystem::path& source, const std::filesystem::path& destination) {
std::smatch results; std::smatch results;
if (std::regex_search(path, results, repo)) { 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: case Type::mason:
if (std::filesystem::exists(temp/"mason.json")) if (std::filesystem::exists(temp/"mason.json"))
break; break;
else else {
Project::warn("Project " + path + " is supposed to me a mason project, but no mason.json was found in the project directory"); Project::warn("Project " + path + " is supposed to me a mason project, but no mason.json was found in the project directory");
success = false;
}
break; break;
case Type::simple: case Type::simple:
@ -108,7 +119,8 @@ bool Dependency::downloadRepo(
if (res) { if (res) {
Project::info("Successfully downloaded " + archivePath.string()); Project::info("Successfully downloaded " + archivePath.string());
res = extract(archivePath, destination/build); location = destination/sources;
res = extract(archivePath, location);
if (res) { if (res) {
out = destination/build/repo; out = destination/build/repo;
if (!std::filesystem::is_directory(out)) { if (!std::filesystem::is_directory(out)) {
@ -119,6 +131,9 @@ bool Dependency::downloadRepo(
Project::error("Couldn't extract archive " + fileName); Project::error("Couldn't extract archive " + fileName);
} }
if (!res)
location.clear();
return res; return res;
} }
} else { } else {

View File

@ -29,6 +29,7 @@ public:
Type getType() const; Type getType() const;
std::optional<std::string> getName() const; std::optional<std::string> getName() const;
std::optional<std::string> getVersion() const; std::optional<std::string> getVersion() const;
std::optional<std::filesystem::path> getLocation() const;
bool prepare(const std::filesystem::path& source, const std::filesystem::path& destination); bool prepare(const std::filesystem::path& source, const std::filesystem::path& destination);
@ -64,4 +65,5 @@ private:
Type type; Type type;
std::optional<std::string> name; std::optional<std::string> name;
std::optional<std::string> version; std::optional<std::string> version;
std::filesystem::path location;
}; };

View File

@ -18,6 +18,7 @@ std::map<std::string, Dependency::Type> types({
Logger* Project::logger = nullptr; Logger* Project::logger = nullptr;
Project::Project(const std::filesystem::path& location, const std::filesystem::path& destination): Project::Project(const std::filesystem::path& location, const std::filesystem::path& destination):
parent(nullptr),
location(location), location(location),
destination(destination), destination(destination),
state(State::unknown), 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() { Project::~Project() {
if (root) { if (root) {
curl_global_cleanup(); curl_global_cleanup();
@ -156,10 +166,21 @@ void Project::createDepencencyFromObject(const nlohmann::json& entry) {
void Project::discover() { void Project::discover() {
int fine = 0; int fine = 0;
for (std::pair<const std::string, Dependency>& pair : dependencies) { for (std::pair<const std::string, Dependency>& pair : dependencies) {
bool success = pair.second.prepare(location, destination); Dependency& dep = pair.second;
if (success) bool success = dep.prepare(location, destination);
if (success) {
switch (dep.getType()) {
case Dependency::Type::mason:
break;
case Dependency::Type::simple:
break;
default:
break;
}
fine++; fine++;
} }
}
} }

View File

@ -44,11 +44,14 @@ public:
static void addProject(const std::filesystem::path& location, const std::filesystem::path& destination); static void addProject(const std::filesystem::path& location, const std::filesystem::path& destination);
private: private:
Project(const std::filesystem::path& location, const std::filesystem::path& destination, Project* parent);
void parse(const nlohmann::json& json); void parse(const nlohmann::json& json);
void createDepencencyFromString(const std::string& entry); void createDepencencyFromString(const std::string& entry);
void createDepencencyFromObject(const nlohmann::json& entry); void createDepencencyFromObject(const nlohmann::json& entry);
private: private:
Project* parent;
std::filesystem::path location; std::filesystem::path location;
std::filesystem::path destination; std::filesystem::path destination;
State state; State state;

11
src2/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
set(SOURCES
component.cpp
collection.cpp
)
set(HEADERS
component.h
collection.h
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

1
src2/collection.cpp Normal file
View File

@ -0,0 +1 @@
#include "collection.h"

59
src2/collection.h Normal file
View File

@ -0,0 +1,59 @@
#pragma once
#include <string>
#include <filesystem>
#include <map>
#include <set>
#include <exception>
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<std::string> errorSources;
std::set<std::string> pendingSources;
std::map<std::string, std::filesystem::path> 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;
};

1
src2/component.cpp Normal file
View File

@ -0,0 +1 @@
#include "component.h"

40
src2/component.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include <string>
#include <filesystem>
#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;
};