Skip to content

Commit

Permalink
refactored validators
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Nov 2, 2023
1 parent 3babbdd commit 0cca548
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 377 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Command {

bool createToml(std::shared_ptr<Context> ctx) {
//Lucas did it again
std::shared_ptr<Generators::ConfigToml::ConfigToml>config_toml = std::make_shared<Generators::ConfigToml::ConfigToml>();
std::shared_ptr<Generators::ConfigToml::Config>config_toml = std::make_shared<Generators::ConfigToml::Config>();
Generators::ConfigToml::readUserInput(ctx, config_toml);
Generators::ConfigToml::writeConfig(ctx);
return false;
Expand Down
327 changes: 2 additions & 325 deletions src/Generators/ConfigTomlGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,336 +6,13 @@
#include <termcolor/termcolor.hpp>

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<Command::Context> ctx,
std::shared_ptr<ConfigToml> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> config_toml) {
std::cout << prefix;
#ifndef TEST
std::getline(std::cin, config_toml->lang_version);
#endif
std::vector<string> valid_c_versions = {"89", "90", "94", "99", "11", "18"};
std::vector<string> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> config_toml) {
std::vector<string> supportedCppCompilers = {"g++", "clang++"};
std::vector<string> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> config_toml) {
std::vector<string> 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<Command::Context> ctx, std::shared_ptr<ConfigToml> config_toml) {
bool readUserInput(std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_toml) {
validateLang:
if(!validateLang("📚Language->" + ctx->lang + " : ", ctx, config_toml)) {
std::cout << termcolor::red << "Invalid language - retry" << termcolor::reset << std::endl;
Expand Down Expand Up @@ -426,7 +103,7 @@ bool validateCmakeVersion(string prefix, std::shared_ptr<Command::Context> 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
Expand Down
Loading

0 comments on commit 0cca548

Please sign in to comment.