Skip to content

Commit

Permalink
remove toml shit
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Nov 3, 2023
1 parent 51ebd0f commit 73bf330
Show file tree
Hide file tree
Showing 20 changed files with 339 additions and 245 deletions.
50 changes: 48 additions & 2 deletions src/Command/Command.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "nlohmann/json_fwd.hpp"
#include <exception>
#include <memory>
#include <toml++/toml.hpp>
Expand Down Expand Up @@ -77,8 +76,21 @@ namespace Command {
} dependency;


typedef struct BuildServer {
std::string name;
std::string ip;
std::string username;
std::string authMethod;
std::optional<std::string> password;
std::optional<std::string> key;
std::optional<std::string> path;
int port;
} BuildServer;

typedef struct Context {
std::string project_name;
std::string project_type;
std::string project_description;
std::filesystem::path project_path;
std::string git{"null"};
std::string lang{"cpp"};
Expand All @@ -89,11 +101,42 @@ namespace Command {
std::string src_dir{"src"};
std::string include_dir{"include"};
std::vector<dependency> dependencies;
std::vector<BuildServer> build_servers;
std::string build_dir{"build"};
dependency testing_lib;
std::string project_version{"0.0.1"};
std::vector<std::string> flags;
std::shared_ptr<cxxopts::ParseResult> args;
nlohmann::json toJson(){
using nlohmann::json;
std::vector<json> deps;
for (auto &dep : 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);
}
json j;
j["project_name"] = project_name;
j["cmake_version"] = cmake_version;
j["project_version"] = project_version;
j["lang"] = lang;
j["lang_version"] = lang_version;
j["compiler"] = compiler;
j["src_dir"] = src_dir;
j["build_dir"] = build_dir;
j["include_dir"] = include_dir;
j["dependencies"] = deps;
j["flags"] = flags;
j["authors"] = authors;
j["project_path"] = project_path;
j["project_type"] = project_type;
j["project_description"] = project_description;
return j;

};
} Context;
class Interface{
private:
Expand All @@ -106,6 +149,8 @@ namespace Command {
bool run();
bool help();
bool addFlag();
bool server();

bool addAuthors();
bool addDependency();
bool ftp();
Expand All @@ -122,12 +167,13 @@ namespace Command {
~Interface();
bool InitHeader();
bool CreateCMakelists();
bool LoadPackageToml();
bool LoadPackageJson();
};
namespace OptionsInit{
bool Init(Interface*);
bool Add(Interface*);
bool Remove(Interface*);
bool Server(Interface*);
bool Update(Interface*);
bool Main(Interface*);
bool Watch(Interface*);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace Command {

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

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Command{
bool Interface::ftp() {
bool test = this->LoadPackageToml();
bool test = this->LoadPackageJson();
if(!test) {
std::cout << "Error: Could not load config.toml" << std::endl;
return false;
Expand Down
63 changes: 28 additions & 35 deletions src/Command/CommandGeneral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,39 @@
#include <iostream>

namespace Command {
bool Interface::LoadPackageToml() {
bool Interface::LoadPackageJson() {
using nlohmann::json;
try {
std::string file_name = "config.toml";



auto data = toml::parse_file((ctx->project_path / file_name).string());
ctx->project_name = data["project"]["project_name"].value_or("");
for (auto &author : *data["project"]["authors"].as_array()) {
ctx->authors.push_back(author.value_or(""));
std::string file_name = "config.json";
std::fstream file;
file.open((ctx->project_path / file_name).string());
json data = json::parse(file);
ctx->project_name = data["project_name"].template get<std::string>();
ctx->authors = data["authors"].template get<std::vector<std::string>>();
ctx->src_dir = data["src_dir"].template get<std::string>();
ctx->build_dir = data["build_dir"].template get<std::string>();
ctx->compiler = data["compiler"].template get<std::string>();
ctx->cmake_version = data["cmake_version"].template get<std::string>();
ctx->git = data["git"].template get<std::string>();
ctx->lang = data["lang"].template get<std::string>();
ctx->include_dir = data["include_dir"].template get<std::string>();
ctx->lang_version = data["lang_version"].template get<std::string>();
ctx->project_version = data["project_version"].template get<std::string>();
for (json &dep: data["dependencies"]) {
Command::dependency _dep;
_dep.name = dep["name"].template get<std::string>();
_dep.url = dep["url"].template get<std::string>();
_dep.version = dep["version"].template get<std::string>();
_dep.target_link = dep["target_link"].template get<std::string>();
ctx->dependencies.push_back(_dep);
}
ctx->src_dir = data["project"]["src_dir"].value_or("");
ctx->build_dir = data["project"]["build_dir"].value_or("");
ctx->compiler = data["project"]["compiler"].value_or("");
ctx->cmake_version = data["project"]["cmake_version"].value_or("");
ctx->git = data["project"]["git"].value_or("");
ctx->lang = data["project"]["lang"].value_or("");
ctx->include_dir =
data["project"]["include_dir"].value_or("");
ctx->lang_version =
data["project"]["lang_version"].value_or("");
ctx->project_version = data["project"]["project_version"].value_or("");
if (data.at_path("dependencies").is_table()) {
for (auto &dep : *data["dependencies"].as_table()) {


Command::dependency _dep;
_dep.name = dep.first;
_dep.url =
data["dependencies"][dep.first].as_array()->at(0).value_or("");
_dep.version =
data["dependencies"][dep.first].as_array()->at(1).value_or("");
_dep.target_link = data["dependencies"][dep.first].as_array()->at(2).value_or("");

ctx->dependencies.push_back(_dep);

}
}


} catch (const toml::parse_error &err) {
std::cout << "Error: Could not load config.toml" << std::endl;
} catch (json::exception &e) {
std::cout << "Error: Could not load config.json" << std::endl;
return false;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Command/CommandInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace Command {

bool createToml(std::shared_ptr<Context> ctx) {
//Lucas did it again
std::shared_ptr<Generators::ConfigToml::Config>config_toml = std::make_shared<Generators::ConfigToml::Config>();
Generators::ConfigToml::readUserInput(ctx, config_toml);
Generators::ConfigToml::writeConfig(ctx);
std::shared_ptr<Generators::ConfigJson::Config>config_json = std::make_shared<Generators::ConfigJson::Config>();
Generators::ConfigJson::readUserInput(ctx, config_json);
Generators::ConfigJson::writeConfig(ctx);
return false;
}

Expand Down Expand Up @@ -57,7 +57,7 @@ bool createHelloWorldC(std::shared_ptr<Context> ctx) {

bool createProject(Interface *inter){
createToml(inter->ctx);
inter->LoadPackageToml();
inter->LoadPackageJson();
Generators::CMakeList::create(inter->ctx);
if(inter->ctx->lang == "cpp"){
createHelloWorldCpp(inter->ctx);
Expand Down Expand Up @@ -105,7 +105,7 @@ bool defaultTomlCpp(std::shared_ptr<Context> ctx) {


bool Interface::init() {
std::string file_name = "config.toml";
std::string file_name = "config.json";
std::string new_project_name = "";
#ifdef DEBUG
new_project_name = "DEBUG";
Expand Down Expand Up @@ -140,7 +140,7 @@ bool Interface::init() {
exit(-1);
}

this->LoadPackageToml();
this->LoadPackageJson();
Generators::CMakeList::create(ctx);
} else {
createProject(this);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CommandRemove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Usage remove dep:
return false;
});

Generators::ConfigToml::writeConfig(ctx);
Generators::ConfigJson::writeConfig(ctx);
Generators::CMakeList::create(ctx);
return true;
}
Expand Down
97 changes: 97 additions & 0 deletions src/Command/CommandServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "Command.hpp"
#include "../Utils/General.hpp"
namespace Command{
bool getServerName(std::string& name){
std::cout << "Enter the name of the server: ";
std::getline(std::cin, name);
return true;
}
bool getServerAddress(std::string& address){
std::cout << "Enter the address of the server: ";
std::getline(std::cin, address);
return true;
}
bool getServerPort(std::string& port){
std::cout << "Enter the port of the server: ";
std::getline(std::cin, port);
return true;
}
bool getServerUsername(std::string& username){
std::cout << "Enter the username of the server: ";
std::getline(std::cin, username);
return true;
}
bool getServerAuthMethod(std::string& authMethod){
std::cout << "Enter the authentication method of the server: ";
std::getline(std::cin, authMethod);
return true;
}
bool getServerPassword(std::string& password){
std::cout << "Enter the password of the server: ";
std::getline(std::cin, password);
return true;
}
bool getServerKey(std::string& key){
std::cout << "Enter path the ssh key for the server: ";
std::getline(std::cin, key);
return true;
}

bool serverHelp(){

std::cout << R"EOF(
Usage server:
add: adds a build server
remove: removes a build server
list: lists all build servers
set: sets the current build server
get: gets the current build server
)EOF" << std::endl;
return true;
}
bool serverAdd(Interface* inter){
std::string name, address, port, username, authMethod, password, key;
getServerName(name);
getServerAddress(address);
getServerPort(port);
getServerUsername(username);
getServerAuthMethod(authMethod);
if (authMethod == "password") {
getServerPassword(password);
}
else if (authMethod == "key") {
getServerKey(key);
}
else{
std::cout << "Invalid authentication method" << std::endl;
return false;
}
inter->ctx->build_servers.push_back(BuildServer(name, address, port, username, authMethod, password, key));
return true;
}
bool Interface::server(){

if (this->args->count("subcommand") == 0) {
serverHelp();

return false;
}
if (this->args->operator[]("subcommand").as<std::string>() == "add") {
serverAdd(this);
}
else if (this->args->operator[]("subcommand").as<std::string>() == "remove") {
}
else if (this->args->operator[]("subcommand").as<std::string>() == "list") {
}
else if (this->args->operator[]("subcommand").as<std::string>() == "set") {
}
else if (this->args->operator[]("subcommand").as<std::string>() == "get") {
}
else{
serverHelp();

return false;
}
return true;
}
}
2 changes: 1 addition & 1 deletion src/Command/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Command {
std::cout << "DEBUG MODE ENABLED\n";
#endif
if(command != "init"){
this->LoadPackageToml();
this->LoadPackageJson();
}


Expand Down
Loading

0 comments on commit 73bf330

Please sign in to comment.