From 0cca54863db05099a7c7aca7ceb81a73df626f89 Mon Sep 17 00:00:00 2001 From: Jon-Michael Hartway Date: Thu, 2 Nov 2023 00:55:32 -0400 Subject: [PATCH] refactored validators --- CMakeLists.txt | 2 + src/Command/CommandInit.cpp | 2 +- src/Generators/ConfigTomlGenerator.cpp | 327 +----------------- src/Generators/Generators.hpp | 31 +- src/Generators/Validators/BuildDir.cpp | 45 +++ src/Generators/Validators/CMakeVersion.cpp | 41 +++ src/Generators/Validators/Compiler.cpp | 74 ++++ src/Generators/Validators/IncludeDir.cpp | 41 +++ src/Generators/Validators/Language.cpp | 42 +++ src/Generators/Validators/LanguageVersion.cpp | 47 +++ src/Generators/Validators/ProjectName.cpp | 40 +++ src/Generators/Validators/ProjectVersion.cpp | 42 +++ src/Generators/Validators/SourceDir.cpp | 46 +++ src/Test/TestGenerators.cpp | 80 ++--- 14 files changed, 483 insertions(+), 377 deletions(-) create mode 100644 src/Generators/Validators/BuildDir.cpp create mode 100644 src/Generators/Validators/CMakeVersion.cpp create mode 100644 src/Generators/Validators/Compiler.cpp create mode 100644 src/Generators/Validators/IncludeDir.cpp create mode 100644 src/Generators/Validators/Language.cpp create mode 100644 src/Generators/Validators/LanguageVersion.cpp create mode 100644 src/Generators/Validators/ProjectName.cpp create mode 100644 src/Generators/Validators/ProjectVersion.cpp create mode 100644 src/Generators/Validators/SourceDir.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b8d9944..395b11d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,8 @@ FetchContent_MakeAvailable(termcolor) file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "src/**.cpp" "src/**.c" + "src/**/**.cpp" + "src/**/**.c" ) include_directories(${CMAKE_SOURCE_DIR}/include) diff --git a/src/Command/CommandInit.cpp b/src/Command/CommandInit.cpp index b983fb4..18026d0 100644 --- a/src/Command/CommandInit.cpp +++ b/src/Command/CommandInit.cpp @@ -15,7 +15,7 @@ namespace Command { bool createToml(std::shared_ptr ctx) { //Lucas did it again - std::shared_ptrconfig_toml = std::make_shared(); + std::shared_ptrconfig_toml = std::make_shared(); Generators::ConfigToml::readUserInput(ctx, config_toml); Generators::ConfigToml::writeConfig(ctx); return false; diff --git a/src/Generators/ConfigTomlGenerator.cpp b/src/Generators/ConfigTomlGenerator.cpp index 041a41e..f04e5e9 100644 --- a/src/Generators/ConfigTomlGenerator.cpp +++ b/src/Generators/ConfigTomlGenerator.cpp @@ -6,336 +6,13 @@ #include namespace Generators::ConfigToml { -using std::string; -/* - * Validates the cmake version - * @param prefix: the prefix of the message @param ctx: the context of the - * command - * @param config_toml: the config toml context - * @return: true if the version is valid - */ -bool validateCmakeVersion(string prefix, std::shared_ptr ctx, - std::shared_ptr config_toml) { - std::cout << prefix; -#ifndef TEST - std::getline(std::cin, config_toml->cmake_version); -#endif - //If the version is empty we're gonna set it - if(config_toml->cmake_version == "") { - goto end; - } - //Checking if the version is x.x.x - if(std::regex_match(config_toml->cmake_version, std::regex("^[0-4]+\\.[0-9]+\\.[0-9]+$"))) { - goto end; - } - //Checking if the version is x.x - if(std::regex_match(config_toml->cmake_version, std::regex("^[0-4]+\\.[0-9]+$"))) { - goto end; - } - return false; - end: - ctx->cmake_version = config_toml->cmake_version == "" ? ctx->cmake_version : config_toml->cmake_version; - - return true; - } - - /* - * Validates the project name - * @param prefix: the prefix of the message - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the project name is valid - */ - bool validateProjectName(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::cout << prefix; - #ifndef TEST - std::getline(std::cin, config_toml->project_name); - #endif - //If the name is empty we're gonna set it - if(config_toml->project_name == "") { - goto end; - } - //Checking if the name is valid - if ((config_toml->project_name.size() > 255)) { - return false; - } - if(std::regex_match(config_toml->project_name, std::regex("^[a-zA-Z0-9_-]+$"))) { - goto end; - } - return false; - end: - ctx->project_name = config_toml->project_name == "" ? ctx->project_name : config_toml->project_name; - return true; - } - - /* - * Validates the project version - * @param prefix: the prefix of the message - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the project version is valid - */ - bool validateProjectVersion(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::cout << prefix; -#ifndef TEST - std::getline(std::cin, config_toml->project_version); -#endif - if(config_toml->project_version == "") { - goto end; - } - //Checking if the version is x.x.x - if(std::regex_match(config_toml->project_version, std::regex("^[0-9]+\\.[0-9]+\\.[0-9]+$"))) { - goto end; - } - //Checking if the version is x.x - if(std::regex_match(config_toml->project_version, std::regex("^[0-9]+\\.[0-9]+$"))) { - goto end; - } - return false; - //If the version is empty we're gonna set it - end: - ctx->project_version = config_toml->project_version == "" ? ctx->project_version : config_toml->project_version; - - return true; - } - - /* - * Validates the language version - * @param prefix: the prefix of the message - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the language version is valid - */ - bool validateLanguageVersion(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::cout << prefix; -#ifndef TEST - std::getline(std::cin, config_toml->lang_version); -#endif - std::vector valid_c_versions = {"89", "90", "94", "99", "11", "18"}; - std::vector valid_cpp_versions = {"98", "03", "11", "14", "17", "20","23"}; - - //If the version is empty we're gonna set it - if(config_toml->lang_version == "") { - goto end; - } - for(string version : valid_c_versions) { - if(config_toml->lang_version == version) { - goto end; - break; - } - } - for(string version : valid_cpp_versions) { - if(config_toml->lang_version == version) { - goto end; - break; - } - } - return false; - end: - ctx->lang_version = config_toml->lang_version == "" ? ctx->lang_version : config_toml->lang_version; - - return true; - } - - /* - * Validates the compiler - * @param prefix: the prefix of the message - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the language is valid - */ - bool validateCompiler(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::vector supportedCppCompilers = {"g++", "clang++"}; - std::vector supportedCCompilers = {"gcc", "clang", "msvc", "icc", "tcc", "emcc"}; - std::cout << prefix << ENDL; - if(ctx->lang == "cpp" || ctx->lang == "c++"){ - ctx->compiler = supportedCppCompilers[0]; - std::cout << " Supported Compilers: ( "; - for(string comp : supportedCppCompilers){ - if(comp == supportedCppCompilers[0]){ - std::cout << " [ " << comp << " ] "; - continue; - }else{ - std::cout << comp << " "; - } - } - } - if(config_toml->lang == "c"){ - ctx->compiler = supportedCCompilers[0]; - std::cout << " Supported Compilers: "; - for(string comp : supportedCCompilers){ - if(comp == supportedCCompilers[0]){ - std::cout << " [ " << comp << " ] "; - continue; - }else{ - std::cout << comp << " "; - } - } - } - std::cout << "): "; -#ifndef TEST - std::getline(std::cin, config_toml->compiler); -#endif - //If the compiler is empty we're gonna set it - if(config_toml->compiler == "") { - goto end; - } - if(config_toml->lang == "cpp" || config_toml->lang == "c++"){ - for(string compiler : supportedCppCompilers){ - if(config_toml->compiler == compiler){ - goto end; - } - } - }else if(config_toml->lang == "c"){ - for(string compiler : supportedCCompilers){ - if(config_toml->compiler == compiler){ - goto end; - } - } - } - - return false; - end: - ctx->compiler = config_toml->compiler == "" ? ctx->compiler : config_toml->compiler; - - return true; - } - - /* - * Validates the source directory - * @param prefix: the prefix of the message - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the source directory is valid - */ - bool validateSourceDir(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::cout << prefix; -#ifndef TEST - std::getline(std::cin, config_toml->src_dir); -#endif - //If the source directory is empty we're gonna set it - if(config_toml->src_dir == "") { - goto end; - } - if (config_toml->src_dir.size() > 255) { - return false; - } - //check if the source directory is valid - if (std::regex_match(config_toml->src_dir, std::regex("^[a-zA-Z0-9_-]+$"))) { - goto end; - } - if (config_toml->src_dir == "src") { - goto end; - } - if (sizeof(config_toml->src_dir) > 255) { - goto end; - } - return false; - end: - ctx->src_dir = config_toml->src_dir == "" ? ctx->src_dir : config_toml->src_dir; - - return true; - } - - /* - * Validates the build directory - * @param prefix: the prefix of the message - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the build directory is valid - */ - bool validateBuildDir(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::cout << prefix; -#ifndef TEST - std::getline(std::cin, config_toml->build_dir); -#endif - //If the build directory is empty we're gonna set it - if(config_toml->build_dir == "") { - goto end; - } - if (config_toml->build_dir.size() > 255) { - return false; - } - if (config_toml->build_dir == "build") { - goto end; - } - //check if the build directory is valid - if (std::regex_match(config_toml->build_dir, std::regex("^[a-zA-Z0-9_-]+$"))) { - goto end; - } - if (sizeof(config_toml->build_dir) > 255) { - goto end; - } - return false; - end: - ctx->build_dir = config_toml->build_dir == "" ? ctx->build_dir : config_toml->build_dir; - - return true; - } - - /* - * Reads the data from the user - * @param ctx: the context of the command - * @param config_toml: the config toml context - * @return: true if the data is valid - */ - bool validateIncludeDir(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::cout << prefix; -#ifndef TEST - std::getline(std::cin, config_toml->include_dir); -#endif - //If the include directory is empty we're gonna set it - if(config_toml->include_dir == "") { - goto end; - } - if (config_toml->include_dir.size() > 255) { - return false; - } - - //check if the include directory is valid - if (std::regex_match(config_toml->include_dir, std::regex("^[a-zA-Z0-9_-]+$"))) { - goto end; - } - - return false; - end: - ctx->include_dir = config_toml->include_dir == "" ? ctx->include_dir : config_toml->include_dir; - - return true; - } - - bool validateLang(string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { - std::vector supportedLangs = {"cpp", "c"}; - std::cout << prefix << ENDL; - std::cout << " Supported languages: ( "; - for(string lang : supportedLangs){ - std::cout << lang << " "; - } - std::cout << "): "; -#ifndef TEST - std::getline(std::cin, config_toml->lang); -#endif - - if(config_toml->lang == "") { - goto end; - } - for(string lang : supportedLangs){ - if(config_toml->lang == lang){ - goto end; - } - } - return false; - end: - ctx->lang = config_toml->lang == "" ? ctx->lang : config_toml->lang; - return true; - } - bool readUserInput(std::shared_ptr ctx, std::shared_ptr config_toml) { + bool readUserInput(std::shared_ptr ctx, std::shared_ptr config_toml) { validateLang: if(!validateLang("đź“šLanguage->" + ctx->lang + " : ", ctx, config_toml)) { std::cout << termcolor::red << "Invalid language - retry" << termcolor::reset << std::endl; @@ -426,7 +103,7 @@ bool validateCmakeVersion(string prefix, std::shared_ptr ctx, std::ofstream file; - string file_name = "config.toml"; + std::string file_name = "config.toml"; #ifdef DEBUG file_name = "build/config.toml"; std::cout << "Writing config.toml to " << ctx->project_path / file_name diff --git a/src/Generators/Generators.hpp b/src/Generators/Generators.hpp index e094fe2..12dd702 100644 --- a/src/Generators/Generators.hpp +++ b/src/Generators/Generators.hpp @@ -40,7 +40,7 @@ namespace Generators{ namespace ConfigToml{ - typedef struct ConfigToml{ + typedef struct Config_s{ std::string cmake_version; std::string project_name; std::string project_version; @@ -52,9 +52,9 @@ namespace Generators{ std::string lang; std::string lang_version; std::string authors_str; - } ConfigToml; + } Config; - bool readUserInput(std::shared_ptr ctx, std::shared_ptr config_toml); + bool readUserInput(std::shared_ptr ctx, std::shared_ptr config_toml); bool writeConfig(std::shared_ptr& ctx); /* @@ -64,7 +64,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the cmake version is valid */ - bool validateCmakeVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateCmakeVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Validates the project name @@ -73,7 +73,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the project name is valid */ - bool validateProjectName(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateProjectName(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Validates the project version @@ -82,7 +82,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the project version is valid */ - bool validateProjectVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateProjectVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Validates the language version @@ -91,7 +91,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the language version is valid */ - bool validateLanguageVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateLanguageVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Validates the compiler @@ -100,7 +100,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the language is valid */ - bool validateCompiler(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateCompiler(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Validates the source directory @@ -109,7 +109,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the source directory is valid */ - bool validateSourceDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateSourceDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Validates the build directory @@ -118,7 +118,7 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the build directory is valid */ - bool validateBuildDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateBuildDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); /* * Reads the data from the user @@ -126,8 +126,17 @@ namespace Generators{ * @param config_toml: the config toml context * @return: true if the data is valid */ - bool validateIncludeDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + bool validateIncludeDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); + + /* + * Validates the language + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the language is valid + */ + bool validateLang(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml); } } diff --git a/src/Generators/Validators/BuildDir.cpp b/src/Generators/Validators/BuildDir.cpp new file mode 100644 index 0000000..26d2263 --- /dev/null +++ b/src/Generators/Validators/BuildDir.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml{ + /* + * Validates the build directory + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the build directory is valid + */ + bool validateBuildDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; +#ifndef TEST + std::getline(std::cin, config_toml->build_dir); +#endif + //If the build directory is empty we're gonna set it + if(config_toml->build_dir == "") { + goto end; + } + if (config_toml->build_dir.size() > 255) { + return false; + } + if (config_toml->build_dir == "build") { + goto end; + } + //check if the build directory is valid + if (std::regex_match(config_toml->build_dir, std::regex("^[a-zA-Z0-9_-]+$"))) { + goto end; + } + if (sizeof(config_toml->build_dir) > 255) { + goto end; + } + return false; + end: + ctx->build_dir = config_toml->build_dir == "" ? ctx->build_dir : config_toml->build_dir; + + return true; + } +} diff --git a/src/Generators/Validators/CMakeVersion.cpp b/src/Generators/Validators/CMakeVersion.cpp new file mode 100644 index 0000000..ede8b3f --- /dev/null +++ b/src/Generators/Validators/CMakeVersion.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml{ + /* + * Validates the cmake version + * @param prefix: the prefix of the message @param ctx: the context of the + * command + * @param config_toml: the config toml context + * @return: true if the version is valid + */ + bool validateCmakeVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; +#ifndef TEST + std::getline(std::cin, config_toml->cmake_version); +#endif + //If the version is empty we're gonna set it + if(config_toml->cmake_version == "") { + goto end; + } + //Checking if the version is x.x.x + if(std::regex_match(config_toml->cmake_version, std::regex("^[0-4]+\\.[0-9]+\\.[0-9]+$"))) { + goto end; + } + //Checking if the version is x.x + if(std::regex_match(config_toml->cmake_version, std::regex("^[0-4]+\\.[0-9]+$"))) { + goto end; + } + return false; + end: + ctx->cmake_version = config_toml->cmake_version == "" ? ctx->cmake_version : config_toml->cmake_version; + + return true; + } + +} diff --git a/src/Generators/Validators/Compiler.cpp b/src/Generators/Validators/Compiler.cpp new file mode 100644 index 0000000..a2c3912 --- /dev/null +++ b/src/Generators/Validators/Compiler.cpp @@ -0,0 +1,74 @@ + +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml{ + /* + * Validates the compiler + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the language is valid + */ + bool validateCompiler(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::vector supportedCppCompilers = {"g++", "clang++"}; + std::vector supportedCCompilers = {"gcc", "clang", "msvc", "icc", "tcc", "emcc"}; + std::cout << prefix << ENDL; + if(ctx->lang == "cpp" || ctx->lang == "c++"){ + ctx->compiler = supportedCppCompilers[0]; + std::cout << " Supported Compilers: ( "; + for(std::string comp : supportedCppCompilers){ + if(comp == supportedCppCompilers[0]){ + std::cout << " [ " << comp << " ] "; + continue; + }else{ + std::cout << comp << " "; + } + } + } + if(config_toml->lang == "c"){ + ctx->compiler = supportedCCompilers[0]; + std::cout << " Supported Compilers: "; + for(std::string comp : supportedCCompilers){ + if(comp == supportedCCompilers[0]){ + std::cout << " [ " << comp << " ] "; + continue; + }else{ + std::cout << comp << " "; + } + } + } + std::cout << "): "; +#ifndef TEST + std::getline(std::cin, config_toml->compiler); +#endif + //If the compiler is empty we're gonna set it + if(config_toml->compiler == "") { + goto end; + } + if(config_toml->lang == "cpp" || config_toml->lang == "c++"){ + for(std::string compiler : supportedCppCompilers){ + if(config_toml->compiler == compiler){ + goto end; + } + } + }else if(config_toml->lang == "c"){ + for(std::string compiler : supportedCCompilers){ + if(config_toml->compiler == compiler){ + goto end; + } + } + } + + return false; + end: + ctx->compiler = config_toml->compiler == "" ? ctx->compiler : config_toml->compiler; + + return true; + } +} diff --git a/src/Generators/Validators/IncludeDir.cpp b/src/Generators/Validators/IncludeDir.cpp new file mode 100644 index 0000000..09e39ee --- /dev/null +++ b/src/Generators/Validators/IncludeDir.cpp @@ -0,0 +1,41 @@ + +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml{ + /* + * Reads the data from the user + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the data is valid + */ + bool validateIncludeDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; +#ifndef TEST + std::getline(std::cin, config_toml->include_dir); +#endif + //If the include directory is empty we're gonna set it + if(config_toml->include_dir == "") { + goto end; + } + if (config_toml->include_dir.size() > 255) { + return false; + } + + //check if the include directory is valid + if (std::regex_match(config_toml->include_dir, std::regex("^[a-zA-Z0-9_-]+$"))) { + goto end; + } + + return false; + end: + ctx->include_dir = config_toml->include_dir == "" ? ctx->include_dir : config_toml->include_dir; + + return true; + } +} diff --git a/src/Generators/Validators/Language.cpp b/src/Generators/Validators/Language.cpp new file mode 100644 index 0000000..59f3924 --- /dev/null +++ b/src/Generators/Validators/Language.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml { + /* + * Validates the language + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the language is valid + */ + bool validateLang(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::vector supportedLangs = {"cpp", "c"}; + std::cout << prefix << ENDL; + std::cout << " Supported languages: ( "; + for(std::string lang : supportedLangs){ + std::cout << lang << " "; + } + std::cout << "): "; +#ifndef TEST + std::getline(std::cin, config_toml->lang); +#endif + + if(config_toml->lang == "") { + goto end; + } + for(std::string lang : supportedLangs){ + if(config_toml->lang == lang){ + goto end; + } + } + return false; + end: + ctx->lang = config_toml->lang == "" ? ctx->lang : config_toml->lang; + return true; + } +} diff --git a/src/Generators/Validators/LanguageVersion.cpp b/src/Generators/Validators/LanguageVersion.cpp new file mode 100644 index 0000000..faaca2b --- /dev/null +++ b/src/Generators/Validators/LanguageVersion.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml{ + /* + * Validates the language version + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the language version is valid + */ + bool validateLanguageVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; +#ifndef TEST + std::getline(std::cin, config_toml->lang_version); +#endif + std::vector valid_c_versions = {"89", "90", "94", "99", "11", "18"}; + std::vector valid_cpp_versions = {"98", "03", "11", "14", "17", "20","23"}; + + //If the version is empty we're gonna set it + if(config_toml->lang_version == "") { + goto end; + } + for(std::string version : valid_c_versions) { + if(config_toml->lang_version == version) { + goto end; + break; + } + } + for(std::string version : valid_cpp_versions) { + if(config_toml->lang_version == version) { + goto end; + break; + } + } + return false; + end: + ctx->lang_version = config_toml->lang_version == "" ? ctx->lang_version : config_toml->lang_version; + + return true; + } +} diff --git a/src/Generators/Validators/ProjectName.cpp b/src/Generators/Validators/ProjectName.cpp new file mode 100644 index 0000000..4c9bd43 --- /dev/null +++ b/src/Generators/Validators/ProjectName.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml { + /* + * Validates the project name + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the project name is valid + */ + bool validateProjectName(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; + #ifndef TEST + std::getline(std::cin, config_toml->project_name); + #endif + //If the name is empty we're gonna set it + if(config_toml->project_name == "") { + goto end; + } + //Checking if the name is valid + if ((config_toml->project_name.size() > 255)) { + return false; + } + if(std::regex_match(config_toml->project_name, std::regex("^[a-zA-Z0-9_-]+$"))) { + goto end; + } + + return false; + end: + ctx->project_name = config_toml->project_name == "" ? ctx->project_name : config_toml->project_name; + + return true; + } +} diff --git a/src/Generators/Validators/ProjectVersion.cpp b/src/Generators/Validators/ProjectVersion.cpp new file mode 100644 index 0000000..b8ba034 --- /dev/null +++ b/src/Generators/Validators/ProjectVersion.cpp @@ -0,0 +1,42 @@ + +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml { + /* + * Validates the project version + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the project version is valid + */ + bool validateProjectVersion(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; +#ifndef TEST + std::getline(std::cin, config_toml->project_version); +#endif + if(config_toml->project_version == "") { + goto end; + } + //Checking if the version is x.x.x + if(std::regex_match(config_toml->project_version, std::regex("^[0-9]+\\.[0-9]+\\.[0-9]+$"))) { + goto end; + } + //Checking if the version is x.x + if(std::regex_match(config_toml->project_version, std::regex("^[0-9]+\\.[0-9]+$"))) { + goto end; + } + return false; + //If the version is empty we're gonna set it + end: + ctx->project_version = config_toml->project_version == "" ? ctx->project_version : config_toml->project_version; + + return true; + + } +} diff --git a/src/Generators/Validators/SourceDir.cpp b/src/Generators/Validators/SourceDir.cpp new file mode 100644 index 0000000..ff96468 --- /dev/null +++ b/src/Generators/Validators/SourceDir.cpp @@ -0,0 +1,46 @@ + +#include +#include +#include +#include +#include "../../Command/Command.hpp" +#include "../Generators.hpp" + + +namespace Generators::ConfigToml{ + /* + * Validates the source directory + * @param prefix: the prefix of the message + * @param ctx: the context of the command + * @param config_toml: the config toml context + * @return: true if the source directory is valid + */ + bool validateSourceDir(std::string prefix, std::shared_ptr ctx, std::shared_ptr config_toml) { + std::cout << prefix; +#ifndef TEST + std::getline(std::cin, config_toml->src_dir); +#endif + //If the source directory is empty we're gonna set it + if(config_toml->src_dir == "") { + goto end; + } + if (config_toml->src_dir.size() > 255) { + return false; + } + //check if the source directory is valid + if (std::regex_match(config_toml->src_dir, std::regex("^[a-zA-Z0-9_-]+$"))) { + goto end; + } + if (config_toml->src_dir == "src") { + goto end; + } + if (sizeof(config_toml->src_dir) > 255) { + goto end; + } + return false; + end: + ctx->src_dir = config_toml->src_dir == "" ? ctx->src_dir : config_toml->src_dir; + + return true; + } +} diff --git a/src/Test/TestGenerators.cpp b/src/Test/TestGenerators.cpp index 6ef1f51..b365a59 100644 --- a/src/Test/TestGenerators.cpp +++ b/src/Test/TestGenerators.cpp @@ -19,8 +19,8 @@ namespace Test::Generators { for(std::string version : versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->cmake_version = version; if(!::Generators::ConfigToml::validateCmakeVersion("Testing version " + version + "\n",ctx, cmake_context)){ return false; @@ -29,8 +29,8 @@ namespace Test::Generators { for(std::string version : failing_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->cmake_version = version; if(::Generators::ConfigToml::validateCmakeVersion("Testing failing version " + version + "\n",ctx, cmake_context)){ return false; @@ -64,8 +64,8 @@ namespace Test::Generators { for(std::string name : mock_names){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->project_name = name; if(!::Generators::ConfigToml::validateProjectName("Test " + name + " \n",ctx, cmake_context)){ return false; @@ -74,8 +74,8 @@ namespace Test::Generators { for(std::string name : failing_names){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->project_name = name; if(::Generators::ConfigToml::validateProjectName("Test failing name " + name + " \n",ctx, cmake_context)){ return false; @@ -89,8 +89,8 @@ namespace Test::Generators { for(std::string version : passing_project_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->project_version = version; if(!::Generators::ConfigToml::validateProjectVersion("Testing version " + version + "\n",ctx, cmake_context)){ return false; @@ -98,8 +98,8 @@ namespace Test::Generators { } for(std::string version : failing_project_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->project_version = version; if(::Generators::ConfigToml::validateProjectVersion("Testing failing version " + version + "\n",ctx, cmake_context)){ return false; @@ -117,8 +117,8 @@ namespace Test::Generators { for(std::string version : passing_cpp_language_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->lang_version = version; cmake_context->lang = "cpp"; if(!::Generators::ConfigToml::validateLanguageVersion("Testing cpp language version " + version + "\n",ctx, cmake_context)){ @@ -127,8 +127,8 @@ namespace Test::Generators { } for(std::string version : failing_cpp_language_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->lang_version = version; cmake_context->lang = "cpp"; if(::Generators::ConfigToml::validateLanguageVersion("Testing failing cpp language version " + version + "\n",ctx, cmake_context)){ @@ -137,8 +137,8 @@ namespace Test::Generators { } for(std::string version : passing_c_language_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->lang_version = version; cmake_context->lang = "c"; if(!::Generators::ConfigToml::validateLanguageVersion("Testing c language version " + version + "\n",ctx, cmake_context)){ @@ -147,8 +147,8 @@ namespace Test::Generators { } for(std::string version : failing_c_language_versions){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->lang_version = version; cmake_context->lang = "c"; if(::Generators::ConfigToml::validateLanguageVersion("Testing failing c language version " + version + "\n",ctx, cmake_context)){ @@ -167,8 +167,8 @@ namespace Test::Generators { for(std::string compiler : passing_c_compilers){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->compiler = compiler; cmake_context->lang = "c"; if(!::Generators::ConfigToml::validateCompiler("Testing c compiler " + compiler + "\n",ctx, cmake_context)){ @@ -177,8 +177,8 @@ namespace Test::Generators { } for(std::string compiler : failing_c_compilers){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->compiler = compiler; cmake_context->lang = "c"; if(::Generators::ConfigToml::validateCompiler("Testing failing c compiler " + compiler + "\n",ctx, cmake_context)){ @@ -187,8 +187,8 @@ namespace Test::Generators { } for(std::string compiler : passing_cpp_compilers){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->compiler = compiler; cmake_context->lang = "cpp"; if(!::Generators::ConfigToml::validateCompiler("Testing cpp compiler " + compiler + "\n",ctx, cmake_context)){ @@ -197,8 +197,8 @@ namespace Test::Generators { } for(std::string compiler : failing_cpp_compilers){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->compiler = compiler; cmake_context->lang = "cpp"; if(::Generators::ConfigToml::validateCompiler("Testing failing cpp compiler " + compiler + "\n",ctx, cmake_context)){ @@ -215,8 +215,8 @@ namespace Test::Generators { for(std::string source_dir : passing_source_dirs){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::Config::ConfigToml> cmake_context = + std::make_shared<::Generators::Config::ConfigToml>(); cmake_context->src_dir = source_dir; if(!::Generators::ConfigToml::validateSourceDir("Testing source dir " + source_dir + "\n",ctx, cmake_context)){ @@ -225,8 +225,8 @@ namespace Test::Generators { } for(std::string source_dir : failing_source_dirs){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::ConfigToml::Config> cmake_context = + std::make_shared<::Generators::ConfigToml::Config>(); cmake_context->src_dir = source_dir; if(::Generators::ConfigToml::validateSourceDir("Testing failing source dir " + source_dir + "\n",ctx, cmake_context)){ @@ -241,8 +241,8 @@ namespace Test::Generators { for(std::string build_dir : passing_build_dirs){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::ConfigToml::Config> cmake_context = + std::make_shared<::Generators::ConfigToml::Config>(); cmake_context->build_dir = build_dir; if(!::Generators::ConfigToml::validateBuildDir("Testing build dir " + build_dir + "\n",ctx, cmake_context)){ @@ -251,8 +251,8 @@ namespace Test::Generators { } for(std::string build_dir : failing_build_dirs){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::ConfigToml::Config> cmake_context = + std::make_shared<::Generators::ConfigToml::Config>(); cmake_context->build_dir = build_dir; if(::Generators::ConfigToml::validateBuildDir("Testing failing build dir " + build_dir + "\n",ctx, cmake_context)){ @@ -267,8 +267,8 @@ namespace Test::Generators { for(std::string include_dir : passing_include_dirs){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::ConfigToml::Config> cmake_context = + std::make_shared<::Generators::ConfigToml::Config>(); cmake_context->include_dir = include_dir; if(!::Generators::ConfigToml::validateIncludeDir("Testing include dir " + include_dir + "\n",ctx, cmake_context)){ @@ -277,8 +277,8 @@ namespace Test::Generators { } for(std::string include_dir : failing_include_dirs){ std::shared_ptr ctx = std::make_shared(); - std::shared_ptr<::Generators::ConfigToml::ConfigToml> cmake_context = - std::make_shared<::Generators::ConfigToml::ConfigToml>(); + std::shared_ptr<::Generators::ConfigToml::Config> cmake_context = + std::make_shared<::Generators::ConfigToml::Config>(); cmake_context->include_dir = include_dir; if(::Generators::ConfigToml::validateIncludeDir("Testing failing include dir " + include_dir + "\n",ctx, cmake_context)){