mason/src/project.cpp

62 lines
1.3 KiB
C++
Raw Normal View History

2023-08-30 20:54:59 +00:00
#include "project.h"
#include <nlohmann/json.hpp>
#include <string_view>
#include <exception>
constexpr std::string_view entry("mason.json");
Project::Project(const std::filesystem::path& location):
location(location),
status(Status::unknown),
name()
{}
bool Project::read() {
if (status == Status::read)
return true;
std::filesystem::path entryPointPath;
try {
entryPointPath = std::filesystem::canonical(location) / entry;
} catch (const std::exception& e) {
log(e.what());
status = Status::error;
return false;
}
std::ifstream file(entryPointPath);
if (!file.is_open()) {
log("couldn't open " + std::string(entryPointPath));
status = Status::error;
return false;
}
try {
nlohmann::json data = nlohmann::json::parse(file);
name = data.at("name");
} catch (const std::exception& e) {
log(e.what());
status = Status::error;
return false;
}
return true;
}
void Project::log(const std::string& message) const {
logStorage.emplace_back(message);
}
Project::Log Project::getLog() const {
return logStorage;
}
Project::Status Project::getStatus() const {
return status;
}
std::string Project::getName() const {
return name;
}