mason/src/component.h

99 lines
2.8 KiB
C++

#pragma once
#include <string>
#include <filesystem>
#include <fstream>
#include <memory>
#include <exception>
#include <optional>
#include <functional>
#include <array>
#include <string_view>
#include <set>
#include <nlohmann/json.hpp>
#include "loggable.h"
#include "loggger.h"
class Collection;
class Component : protected Loggable {
public:
class WrongState;
class WrongType;
enum State {
initial,
reading,
ready,
building,
error,
done
};
enum Type {
file,
directory,
mason,
unknown
};
Component(
const std::filesystem::path& path,
Collection* collection,
const std::shared_ptr<Logger>& logger,
const std::optional<std::string>& name = std::nullopt
);
Type getType() const;
State getState() const;
std::filesystem::path getLocation() const;
std::string getLibrariesPath() const;
std::string getName() const;
bool successfullyRead() const;
bool successfullyBuilt() const;
void read();
void build(const std::filesystem::path& destination, const std::string& target, bool useName = true);
private:
bool tryReadingBuildScenarios();
bool readAsMason();
bool errorScenario(const std::string& message);
void buildAsFile(const std::filesystem::path& destination);
void buildAsDirectory(const std::filesystem::path& destination);
void buildAsMason(const std::filesystem::path& destination, std::string target);
bool readMasonDependencies(const nlohmann::json& deps, const std::string& manifestPath);
void copyFile(const std::filesystem::path& from, const std::filesystem::path& to) const;
std::string validateMasonTarget(std::string target) const;
void masonFilesArray(const nlohmann::json& files, const std::filesystem::path& destination);
bool masonFileObject(const nlohmann::json& object, const std::filesystem::path& destination);
bool allowedForMasonTarget(const nlohmann::json& object, const std::string& target) const;
void getMasonTargets(const nlohmann::json& object, std::set<std::string>& out) const;
std::optional<std::string> getFileSource(const nlohmann::json& object) const;
std::optional<std::string> getFileTarget(const nlohmann::json& object) const;
static std::optional<std::string> tryStringValue (const nlohmann::json& object, const std::string& key);
private:
State state;
Type type;
Collection* collection;
std::filesystem::path location;
std::optional<nlohmann::json> manifest;
std::optional<std::string> name;
};
class Component::WrongState : public std::runtime_error {
public:
explicit WrongState(State state, const std::string& action);
};
class Component::WrongType : public std::runtime_error {
public:
explicit WrongType(Type type, const std::string& action);
};