From 0c40906eca0a23bcc8e3ee9a35b27d922bdb6806 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 30 Aug 2023 17:54:59 -0300 Subject: [PATCH] some first slow thoughts --- CMakeLists.txt | 7 +++++- src/CMakeLists.txt | 2 ++ src/main.cpp | 24 +++++++++++++++++- src/project.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ src/project.h | 33 ++++++++++++++++++++++++ test/CMakeLists.txt | 1 + test/mason.json | 4 +++ 7 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/project.cpp create mode 100644 src/project.h create mode 100644 test/CMakeLists.txt create mode 100644 test/mason.json diff --git a/CMakeLists.txt b/CMakeLists.txt index e1c88d9..cebe54d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 620b3b9..e1a2e8d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,10 @@ set(SOURCES main.cpp + project.cpp ) set(HEADERS + project.h ) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/src/main.cpp b/src/main.cpp index f8d5a4f..a3aedf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,28 @@ #include +#include + +#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; } diff --git a/src/project.cpp b/src/project.cpp new file mode 100644 index 0000000..d2a2e2b --- /dev/null +++ b/src/project.cpp @@ -0,0 +1,61 @@ +#include "project.h" + +#include +#include +#include + +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; +} diff --git a/src/project.h b/src/project.h new file mode 100644 index 0000000..b7e02cb --- /dev/null +++ b/src/project.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include +#include + +class Project { +public: + typedef std::string LogMessage; + typedef std::list 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; + +}; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..a31a46c --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1 @@ +configure_file(mason.json mason.json) diff --git a/test/mason.json b/test/mason.json new file mode 100644 index 0000000..e8de763 --- /dev/null +++ b/test/mason.json @@ -0,0 +1,4 @@ +{ + "name" : "test", + "version" : "0.0.1" +}