some more refactor

This commit is contained in:
Blue 2023-09-26 14:36:40 -03:00
parent cdaf94e3cd
commit d30e6ed759
Signed by: blue
GPG Key ID: 9B203B252A63EE38
6 changed files with 111 additions and 32 deletions

View File

@ -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<Downloads::iterator, bool> res = downloadingSources.emplace(std::piecewise_construct,
std::forward_as_tuple(source),

View File

@ -6,6 +6,7 @@
#include <set>
#include <exception>
#include <string_view>
#include <regex>
#include "loggable.h"
#include "component.h"
@ -16,11 +17,8 @@ class Collection : protected Loggable {
using Sources = std::set<std::string>;
using Downloads = std::map<std::string, std::unique_ptr<Download>>;
using Components = std::map<std::string, std::unique_ptr<Component>>;
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);
};

View File

@ -1,10 +1,9 @@
#include "component.h"
#include <array>
#include <string_view>
#include "collection.h"
constexpr std::string_view masonJSON("mason.json");
constexpr std::string_view dependencies("dependencies");
constexpr std::array<std::string_view, Component::done + 1> stringStates {
"initial",
"reading",
@ -20,21 +19,102 @@ Component::Component(
const std::shared_ptr<Logger>& 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) {

View File

@ -2,9 +2,14 @@
#include <string>
#include <filesystem>
#include <fstream>
#include <memory>
#include <exception>
#include <optional>
#include <functional>
#include <array>
#include <string_view>
#include <nlohmann/json.hpp>
#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> collection;
std::filesystem::path location;
std::optional<nlohmann::json> scenario;
};
class Component::WrongState : public std::runtime_error {

View File

@ -1,7 +1,5 @@
#include "download.h"
#include <regex>
constexpr std::string_view archived("archived");
constexpr std::string_view headerAcceptJson("accept: application/json");

View File

@ -8,6 +8,7 @@
#include <filesystem>
#include <optional>
#include <exception>
#include <regex>
#include <curl/curl.h>
#include <nlohmann/json.hpp>