mason/src/component.h

99 lines
2.8 KiB
C
Raw Normal View History

2023-09-20 19:45:22 +00:00
#pragma once
#include <string>
#include <filesystem>
2023-09-26 17:36:40 +00:00
#include <fstream>
2023-09-20 19:45:22 +00:00
#include <memory>
#include <exception>
2023-09-26 17:36:40 +00:00
#include <optional>
2023-09-20 19:45:22 +00:00
#include <functional>
2023-09-26 17:36:40 +00:00
#include <array>
#include <string_view>
2023-10-01 15:47:17 +00:00
#include <set>
2023-09-26 17:36:40 +00:00
#include <nlohmann/json.hpp>
2023-09-20 19:45:22 +00:00
#include "loggable.h"
#include "loggger.h"
class Collection;
2023-09-20 19:45:22 +00:00
class Component : protected Loggable {
public:
class WrongState;
2023-09-29 21:09:09 +00:00
class WrongType;
2023-09-20 19:45:22 +00:00
enum State {
initial,
reading,
ready,
building,
error,
done
};
enum Type {
file,
directory,
2023-09-29 21:09:09 +00:00
mason,
unknown
2023-09-20 19:45:22 +00:00
};
Component(
const std::filesystem::path& path,
2023-09-30 20:13:44 +00:00
Collection* collection,
2023-10-01 15:47:17 +00:00
const std::shared_ptr<Logger>& logger,
const std::optional<std::string>& name = std::nullopt
2023-09-20 19:45:22 +00:00
);
Type getType() const;
State getState() const;
2023-09-29 21:09:09 +00:00
std::filesystem::path getLocation() const;
std::string getLibrariesPath() const;
2023-10-01 15:47:17 +00:00
std::string getName() const;
2023-09-29 21:09:09 +00:00
bool successfullyRead() const;
2023-09-30 20:13:44 +00:00
bool successfullyBuilt() const;
2023-09-20 19:45:22 +00:00
void read();
2023-10-01 15:47:17 +00:00
void build(const std::filesystem::path& destination, const std::string& target, bool useName = true);
2023-09-20 19:45:22 +00:00
2023-09-26 17:36:40 +00:00
private:
2023-10-01 15:47:17 +00:00
bool tryReadingBuildScenarios();
2023-09-26 17:36:40 +00:00
bool readAsMason();
bool errorScenario(const std::string& message);
2023-09-29 21:09:09 +00:00
void buildAsFile(const std::filesystem::path& destination);
2023-09-30 20:13:44 +00:00
void buildAsDirectory(const std::filesystem::path& destination);
2023-10-01 15:47:17 +00:00
void buildAsMason(const std::filesystem::path& destination, std::string target);
2023-09-29 21:09:09 +00:00
bool readMasonDependencies(const nlohmann::json& deps, const std::string& manifestPath);
2023-09-30 20:13:44 +00:00
void copyFile(const std::filesystem::path& from, const std::filesystem::path& to) const;
2023-10-01 15:47:17 +00:00
std::string validateMasonTarget(std::string target) const;
2023-09-30 20:13:44 +00:00
void masonFilesArray(const nlohmann::json& files, const std::filesystem::path& destination);
2023-10-01 15:47:17 +00:00
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;
2023-09-30 20:13:44 +00:00
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);
2023-09-29 21:09:09 +00:00
2023-09-20 19:45:22 +00:00
private:
State state;
Type type;
2023-09-30 20:13:44 +00:00
Collection* collection;
2023-09-20 19:45:22 +00:00
std::filesystem::path location;
2023-09-29 21:09:09 +00:00
std::optional<nlohmann::json> manifest;
std::optional<std::string> name;
2023-09-20 19:45:22 +00:00
};
class Component::WrongState : public std::runtime_error {
public:
explicit WrongState(State state, const std::string& action);
};
2023-09-29 21:09:09 +00:00
class Component::WrongType : public std::runtime_error {
public:
explicit WrongType(Type type, const std::string& action);
};