mason/src/component.h

82 lines
1.9 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>
#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-27 23:30:57 +00:00
const std::weak_ptr<Collection>& collection,
2023-09-20 19:45:22 +00:00
const std::shared_ptr<Logger>& logger
);
Type getType() const;
State getState() const;
2023-09-29 21:09:09 +00:00
std::filesystem::path getLocation() const;
std::string getLibrariesPath() const;
bool successfullyRead() const;
2023-09-20 19:45:22 +00:00
void read();
void build(const std::filesystem::path& destination, const std::string& target);
2023-09-26 17:36:40 +00:00
private:
void tryReadingBuildScenarios();
bool readAsMason();
bool errorScenario(const std::string& message);
2023-09-29 21:09:09 +00:00
void buildAsFile(const std::filesystem::path& destination);
void buildAsDiractory(const std::filesystem::path& destination);
void buildAsMason(const std::filesystem::path& destination, const std::string& target);
bool readMasonDependencies(const nlohmann::json& deps, const std::string& manifestPath);
2023-09-20 19:45:22 +00:00
private:
State state;
Type type;
2023-09-27 23:30:57 +00:00
std::weak_ptr<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;
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);
};