Skip to content

Commit

Permalink
fuck toml++
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Nov 3, 2023
1 parent 0ede80c commit 78330be
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ cmake-generator is a command line utility used to expedite building modern c/c++
- [x] Simple dependency management
- [ ] Multi platform builds
- [ ] Existing cmake project migration
- [ ] Extensible toml configuration
- [ ] Extensible json configuration
10 changes: 5 additions & 5 deletions src/Command/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Command {
}
// opening the file
if (file) {
std::cout << "file config.toml exists" << std::endl;
std::cout << "file config.json exists" << std::endl;
std::cout << "do you want to overwrite it?[y/n]:";
std::string input;
std::getline(std::cin, input);
Expand Down Expand Up @@ -180,12 +180,12 @@ namespace Command {


/*
* Loads the config.toml file into the project context
* Loads the config.json file into the project context
* @param a blank project context
* @return bool -> finished successfully
*/
[[deprecated("Old function, Command::Interface ")]]
bool loadPackageToml(std::shared_ptr<Context> ctx);
bool loadPackageJson(std::shared_ptr<Context> ctx);
/*
* Initializes the project
* Prompts the user for information about the project
Expand Down Expand Up @@ -223,7 +223,7 @@ namespace Command {
[[deprecated("Old function, Command::Interface ")]]
bool help();
/*
* adds a dependency to the config.toml
* adds a dependency to the config.json
* @returns bool -> finished successfully
*/
[[deprecated("Old function, Command::Interface ")]]
Expand All @@ -244,7 +244,7 @@ namespace Command {

std::string downloadIndex();
/*
* adds dependency to toml and regenerates the CMakeLists.txt
* adds dependency to json and regenerates the CMakeLists.txt
* @returns bool -> finished successfully
*/
[[deprecated("Old function, Command::Interface ")]]
Expand Down
4 changes: 2 additions & 2 deletions src/Command/CommandAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace Command {
return false;
}

std::cout << "Adding dependency to config.toml" << std::endl;
std::cout << "Adding dependency to config.json" << std::endl;
ctx->dependencies.push_back({

.name = searchResults[index].name,
Expand All @@ -128,7 +128,7 @@ namespace Command {
: searchResults[index].target_link

});
std::cout << "Writing config.toml" << std::endl;
std::cout << "Writing config.json" << std::endl;
Generators::ConfigJson::writeConfig(ctx);
Generators::CMakeList::create(ctx);

Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Command{
bool Interface::ftp() {
bool test = this->LoadPackageJson();
if(!test) {
std::cout << "Error: Could not load config.toml" << std::endl;
std::cout << "Error: Could not load config.json" << std::endl;
return false;
}
#ifdef DEBUG
Expand Down
65 changes: 37 additions & 28 deletions src/Command/CommandInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Command {


bool createToml(std::shared_ptr<Context> ctx) {
bool createJson(std::shared_ptr<Context> ctx) {
//Lucas did it again
std::shared_ptr<Generators::ConfigJson::Config>config_json = std::make_shared<Generators::ConfigJson::Config>();
Generators::ConfigJson::readUserInput(ctx, config_json);
Expand Down Expand Up @@ -55,7 +55,7 @@ bool createHelloWorldC(std::shared_ptr<Context> ctx) {
}

bool createProject(Interface *inter){
createToml(inter->ctx);
createJson(inter->ctx);
inter->LoadPackageJson();
Generators::CMakeList::create(inter->ctx);
if(inter->ctx->lang == "cpp"){
Expand All @@ -67,36 +67,45 @@ bool createProject(Interface *inter){
}


bool defaultTomlCpp(std::shared_ptr<Context> ctx) {
bool defaultJsonCpp(std::shared_ptr<Context> ctx) {
using nlohmann::json;
std::string language = ctx->args->operator[]("language").as<std::string>();
std::string name = ctx->args->operator[]("name").as<std::string>();
toml::array authors = toml::array{};
toml::table table = toml::table{
{"project",
toml::table{
{"cmake_version", ctx->cmake_version},
{"include_dir", ctx->include_dir},
{"project_version", ctx->project_version},
{"compiler", ctx->compiler},
{"project_name", ctx->project_name},
{"authors", authors},
{"src_dir", ctx->src_dir},
{"build_dir", ctx->build_dir},
{"lang", ctx->lang},
{"lang_version", ctx->lang_version},
}},
};


std::cout << "📄 New Toml File: \n";
std::cout << table << '\n';
json j;
j["cmake_version"] = ctx->cmake_version;
j["project_version"] = ctx->project_version;
j["lang"] = ctx->lang;
j["lang_version"] = ctx->lang_version;
j["compiler"] = ctx->compiler;
j["src_dir"] = ctx->src_dir;
j["build_dir"] = ctx->build_dir;
j["include_dir"] = ctx->include_dir;
j["project_name"] = ctx->project_name;
j["authors"] = ctx->authors;
j["project_path"] = ctx->project_path;
j["project_type"] = ctx->project_type;
j["project_description"] = ctx->project_description;
j["flags"] = ctx->flags;
std::vector<json> deps;
for (auto &dep : ctx->dependencies) {
json dep_json;
dep_json["name"] = dep.name;
dep_json["url"] = dep.url;
dep_json["version"] = dep.version;
dep_json["target_link"] = dep.target_link;
deps.push_back(dep_json);
}
j["dependencies"] = deps;

std::cout << "📄 New json File: \n";
std::cout << j << '\n';

std::ofstream file;
std::string file_name = "config.toml";
std::string file_name = "config.json";


file.open(ctx->project_path / file_name);
file << table;
file << j;
file << '\n';
file.close();
return false;
Expand Down Expand Up @@ -124,17 +133,17 @@ bool Interface::init() {
}

std::cout << "project path" << ctx->project_path << ENDL;
std::cout << "config.toml path" << ctx->project_path / file_name << ENDL;
std::cout << "config.json path" << ctx->project_path / file_name << ENDL;
std::string current_path = ctx->project_path.string();


if (args->operator[]("skip-init").as<bool>()) {
std::string language = args->operator[]("language").as<std::string>();
if (language == "cpp" || language == "c++"){
defaultTomlCpp(ctx);
defaultJsonCpp(ctx);
}
else if (language == "c") {
//defaultTomlC(ctx, args);
//defaultJsonC(ctx, args);
std::cout << "C is not supported yet" << ENDL;
exit(-1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ namespace Generators::ConfigJson{
return true;
}

} // namespace Generators::ConfigToml
} // namespace Generators::ConfigJson
18 changes: 9 additions & 9 deletions src/Generators/Generators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace Generators{
* Validate cmakeVersion
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the cmake version is valid
*/
bool validateCmakeVersion(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -70,7 +70,7 @@ namespace Generators{
* Validates the project name
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the project name is valid
*/
bool validateProjectName(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -79,7 +79,7 @@ namespace Generators{
* Validates the project version
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the project version is valid
*/
bool validateProjectVersion(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -88,7 +88,7 @@ namespace Generators{
* Validates the language version
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the language version is valid
*/
bool validateLanguageVersion(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -97,7 +97,7 @@ namespace Generators{
* Validates the compiler
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the language is valid
*/
bool validateCompiler(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -106,7 +106,7 @@ namespace Generators{
* Validates the source directory
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the source directory is valid
*/
bool validateSourceDir(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -115,15 +115,15 @@ namespace Generators{
* Validates the build directory
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the build directory is valid
*/
bool validateBuildDir(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);

/*
* Reads the data from the user
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the data is valid
*/
bool validateIncludeDir(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand All @@ -133,7 +133,7 @@ namespace Generators{
* Validates the language
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the language is valid
*/
bool validateLang(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json);
Expand Down
18 changes: 9 additions & 9 deletions src/Generators/Validators/BuildDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ namespace Generators::ConfigJson{
* 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
* @param config_json: the config json context
* @return: true if the build directory is valid
*/
bool validateBuildDir(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_toml) {
bool validateBuildDir(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
std::cout << prefix;
#ifndef TEST
std::getline(std::cin, config_toml->build_dir);
std::getline(std::cin, config_json->build_dir);
#endif
//If the build directory is empty we're gonna set it
if(config_toml->build_dir == "") {
if(config_json->build_dir == "") {
goto end;
}
if (config_toml->build_dir.size() > 255) {
if (config_json->build_dir.size() > 255) {
return false;
}
if (config_toml->build_dir == "build") {
if (config_json->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_-]+$"))) {
if (std::regex_match(config_json->build_dir, std::regex("^[a-zA-Z0-9_-]+$"))) {
goto end;
}
if (sizeof(config_toml->build_dir) > 255) {
if (sizeof(config_json->build_dir) > 255) {
goto end;
}
return false;
end:
ctx->build_dir = config_toml->build_dir == "" ? ctx->build_dir : config_toml->build_dir;
ctx->build_dir = config_json->build_dir == "" ? ctx->build_dir : config_json->build_dir;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Validators/CMakeVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Generators::ConfigJson{
* Validates the cmake version
* @param prefix: the prefix of the message @param ctx: the context of the
* command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the version is valid
*/
bool validateCmakeVersion(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Validators/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Generators::ConfigJson{
* Validates the compiler
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the language is valid
*/
bool validateCompiler(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Validators/IncludeDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Generators::ConfigJson{
/*
* Reads the data from the user
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the data is valid
*/
bool validateIncludeDir(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Validators/Language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Generators::ConfigJson{
* Validates the language
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the language is valid
*/
bool validateLang(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Validators/LanguageVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Generators::ConfigJson{
* Validates the language version
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the language version is valid
*/
bool validateLanguageVersion(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/Validators/ProjectName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Generators::ConfigJson{
* Validates the project name
* @param prefix: the prefix of the message
* @param ctx: the context of the command
* @param config_json: the config toml context
* @param config_json: the config json context
* @return: true if the project name is valid
*/
bool validateProjectName(std::string prefix, std::shared_ptr<Command::Context> ctx, std::shared_ptr<Config> config_json) {
Expand Down
Loading

0 comments on commit 78330be

Please sign in to comment.