some first slow thoughts

This commit is contained in:
Blue 2023-08-30 17:54:59 -03:00
parent 17881c4066
commit 0c40906eca
Signed by: blue
GPG Key ID: 9B203B252A63EE38
7 changed files with 130 additions and 2 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
project(mason
VERSION 0.0.1
@ -22,6 +22,8 @@ elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND COMPILE_OPTIONS -Wextra)
endif()
find_package(nlohmann_json REQUIRED)
add_executable(${PROJECT_NAME})
target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS})
@ -37,6 +39,9 @@ set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_subdirectory(src)
add_subdirectory(test)
target_link_libraries(${PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets

View File

@ -1,8 +1,10 @@
set(SOURCES
main.cpp
project.cpp
)
set(HEADERS
project.h
)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View File

@ -1,6 +1,28 @@
#include <iostream>
#include <string>
#include "project.h"
int main(int argc, char *argv[]) {
std::cout << "Ahh, shit! Here we go again!" << std::endl;
std::string firstArg;
if (argc > 1)
firstArg = argv[1];
else
firstArg = "./";
Project root(firstArg);
bool success = root.read();
if (!success) {
Project::Log log(root.getLog());
for (const Project::LogMessage& msg : log)
std::cout << msg << std::endl;
return -1;
} else {
std::cout << "successfully parsed project " << root.getName() << std::endl;
}
return 0;
}

61
src/project.cpp Normal file
View File

@ -0,0 +1,61 @@
#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;
}

33
src/project.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <filesystem>
#include <string>
#include <fstream>
#include <list>
class Project {
public:
typedef std::string LogMessage;
typedef std::list<LogMessage> Log;
enum class Status {
unknown,
read,
error
};
Project(const std::filesystem::path& location);
bool read();
Status getStatus() const;
Log getLog() const;
std::string getName() const;
private:
void log(const std::string& message) const;
private:
std::filesystem::path location;
Status status;
std::string name;
mutable Log logStorage;
};

1
test/CMakeLists.txt Normal file
View File

@ -0,0 +1 @@
configure_file(mason.json mason.json)

4
test/mason.json Normal file
View File

@ -0,0 +1,4 @@
{
"name" : "test",
"version" : "0.0.1"
}