Crafter.Build/Crafter.Build-Configuration.cpp

125 lines
6 KiB
C++
Raw Normal View History

2024-12-29 01:04:33 +01:00
/*
Crafter.Build
Copyright (C) 2024 Jorijn van der Graaf
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
2024-12-28 21:00:12 +01:00
module;
#include <vector>
#include <string>
#include <fstream>
#include "json.hpp"
2024-12-29 17:53:46 +01:00
#include <filesystem>
2024-12-28 21:00:12 +01:00
module Crafter.Build:ConfigurationImpl;
import :Configuration;
2024-12-29 17:53:46 +01:00
import :Dependency;
2024-12-28 21:00:12 +01:00
using namespace Crafter::Build;
2024-12-29 17:53:46 +01:00
namespace fs = std::filesystem;
2024-12-28 21:00:12 +01:00
2024-12-29 17:53:46 +01:00
Configuration::Configuration(std::string name, std::string standard, std::vector<fs::path> sourceFiles, std::vector<fs::path> moduleFiles, std::string optimizationLevel, std::string buildDir, std::string outputDir, std::string type, std::string target, std::vector<Dependency> dependencies): name(name), standard(standard), sourceFiles(sourceFiles), moduleFiles(moduleFiles), optimizationLevel(optimizationLevel), buildDir(buildDir), outputDir(outputDir), type(type), target(target), dependencies(dependencies) {
2024-12-28 21:00:12 +01:00
}
2024-12-29 17:53:46 +01:00
Configuration::Configuration(nlohmann::json& configs, nlohmann::json& config, fs::path workingDir) {
name = config["name"].get<std::string>();
if(config.contains("standard")) {
standard = config["standard"].get<std::string>();
}
if(config.contains("target")) {
target = config["target"].get<std::string>();
}
if(config.contains("type")) {
type = config["type"].get<std::string>();
}
if(config.contains("source_files")) {
const std::vector<std::string> tempSourceFiles = config["source_files"].get<std::vector<std::string>>();
sourceFiles = std::vector<fs::path>(tempSourceFiles.size());
for(std::int_fast32_t i = 0; i < sourceFiles.size(); i++){
std::filesystem::path filePath (tempSourceFiles[i]);
std::filesystem::path fullFilePath = workingDir / filePath;
sourceFiles[i] = fullFilePath.generic_string();
}
}
if(config.contains("module_files")) {
const std::vector<std::string> tempModuleFiles = config["module_files"].get<std::vector<std::string>>();
moduleFiles = std::vector<fs::path>(tempModuleFiles.size());
for(std::int_fast32_t i = 0; i < moduleFiles.size(); i++){
std::filesystem::path filePath (tempModuleFiles[i]);
std::filesystem::path fullFilePath = workingDir / filePath;
moduleFiles[i] = fullFilePath.generic_string();
}
}
if(config.contains("optimization_level")) {
optimizationLevel = config["optimization_level"].get<std::string>();
}
if(config.contains("build_dir")) {
const std::string tempBuildDir = config["build_dir"].get<std::string>();
std::filesystem::path buildPath (tempBuildDir);
std::filesystem::path fullBuildPath = workingDir / buildPath;
buildDir = fullBuildPath.generic_string();
}
if(config.contains("output_dir")) {
const std::string tempOutputDir = config["output_dir"].get<std::string>();
std::filesystem::path outputPath (tempOutputDir);
std::filesystem::path fullOutputPath = workingDir / outputPath;
outputDir = fullOutputPath.generic_string();
}
if(config.contains("dependencies")) {
nlohmann::json dependenciesJson = config["dependencies"];
for (nlohmann::json::iterator it = dependenciesJson.begin(); it != dependenciesJson.end(); ++it) {
2024-12-29 20:14:49 +01:00
dependencies.emplace_back((*it)["path"].get<std::string>(), (*it)["configuration"].get<std::string>());
2024-12-29 17:53:46 +01:00
}
}
if(config.contains("extends")){
const std::vector<std::string> extends = config["extends"].get<std::vector<std::string>>();
for(const std::string& extendName : extends) {
for (nlohmann::json::iterator it = configs.begin(); it != configs.end(); ++it) {
if((*it)["name"].get<std::string>() == extendName){
Configuration extendData = Configuration(configs, (*it), workingDir);
if(!extendData.standard.empty() && standard.empty()){
standard = extendData.standard;
}
if(!extendData.sourceFiles.empty()){
sourceFiles.insert(sourceFiles.end(), extendData.sourceFiles.begin(), extendData.sourceFiles.end());
}
if(!extendData.moduleFiles.empty()){
moduleFiles.insert(moduleFiles.end(), extendData.moduleFiles.begin(), extendData.moduleFiles.end());
}
if(!extendData.optimizationLevel.empty() && optimizationLevel.empty()){
optimizationLevel = extendData.optimizationLevel;
}
if(!extendData.dependencies.empty()){
dependencies.insert(dependencies.end(), extendData.dependencies.begin(), extendData.dependencies.end());
}
if(!extendData.buildDir.empty() && buildDir.empty()) {
buildDir = extendData.buildDir;
}
if(!extendData.outputDir.empty() && outputDir.empty()) {
outputDir = extendData.outputDir;
}
if(!extendData.target.empty() && target.empty()) {
target = extendData.target;
}
if(!extendData.type.empty() && type.empty()) {
type = extendData.type;
}
break;
}
}
}
}
}