Skip to content

Commit

Permalink
Fixed custom prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Dec 11, 2023
1 parent bcaba2e commit 3d6a1c2
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 66 deletions.
2 changes: 1 addition & 1 deletion build
Submodule build updated from 79df52 to 5d6a21
1 change: 1 addition & 0 deletions include/Frate/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace Frate::Command {

typedef struct ProjectPrompt_s {
std::string value{""};
std::string text{""};
std::string type{"string"};
// std::string key{""};
std::string default_value{""};
Expand Down
8 changes: 8 additions & 0 deletions src/Command/Actions/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Frate::Command::Add {
Packages::options(inter);
return Packages::add(inter);
},
.requires_project = true,
.unlimited_args = true,
},
Handler{
Expand All @@ -58,6 +59,7 @@ namespace Frate::Command::Add {
Flags::options(inter);
return Flags::add(inter);
},
.requires_project = true,
},
Handler{
.aliases = {"lib","l"},
Expand All @@ -70,6 +72,7 @@ namespace Frate::Command::Add {
return Library::add(inter);
},
.implemented = false,
.requires_project = true,
},
Handler{
.aliases = {"mode","m"},
Expand All @@ -79,6 +82,7 @@ namespace Frate::Command::Add {
Modes::options(inter);
return Modes::add(inter);
},
.requires_project = true,
},
Handler{
.aliases = {"server","s"},
Expand All @@ -87,6 +91,7 @@ namespace Frate::Command::Add {
.callback = [inter]() {
return RemoteServers::add(inter);
},
.requires_project = true,
},
Handler{
.aliases = {"toolchain","t"},
Expand All @@ -104,6 +109,7 @@ namespace Frate::Command::Add {
return Toolchains::add(toolchain_name, inter);
}
},
.requires_project = true,
},
Handler{
.aliases = {"author","a"},
Expand All @@ -112,6 +118,7 @@ namespace Frate::Command::Add {
.callback = [inter]() {
return Author::add(inter);
},
.requires_project = true,
.unlimited_args = true,
},
Handler{
Expand All @@ -122,6 +129,7 @@ namespace Frate::Command::Add {
Keywords::add(inter);
return false;
},
.requires_project = true,
.unlimited_args = true,
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/Command/Actions/New.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ bool options(Interface* inter) {
}else{
Frate::info << "Creating project" << ENDL;
Frate::info << "Creating frate-project.json" << ENDL;

inter->pro->project_type = "";
Wizard::Project(inter->pro);
if(!createProjectWizard(inter)){
return false;
Expand Down
6 changes: 5 additions & 1 deletion src/Command/Actions/Set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Frate::Command::Set {
.callback = [inter]() {
return License::set(inter);
},
.requires_project = true,
},
Handler{
.aliases = {"name","n"},
Expand All @@ -33,6 +34,7 @@ namespace Frate::Command::Set {
Name::set(inter);
return true;//Name::set(inter);
},
.requires_project = true,
},
Handler{
.aliases = {"version","ver","v"},
Expand All @@ -41,7 +43,8 @@ namespace Frate::Command::Set {

return true;//Version::set(inter);
},
.implemented = false
.implemented = false,
.requires_project = true,
},
Handler{
.aliases = {"server","s"},
Expand All @@ -51,6 +54,7 @@ namespace Frate::Command::Set {
RemoteServers::set(inter);
return true; //Server::set(inter);
},
.requires_project = true,
}
};
}
Expand Down
28 changes: 13 additions & 15 deletions src/Command/CommandGeneral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ namespace Frate::Command {
std::fstream file;
std::string file_name = "frate-project.json";

if(project_present) return true;
Frate::info << "Loading: " << (pro->project_path / file_name) << std::endl;

if(!std::filesystem::exists(pro->project_path / file_name)){
return false;
}


Frate::info << "Loading: " << (pro->project_path / file_name) << std::endl;
try{
file.open((pro->project_path / file_name).string());
}catch(std::exception &e){
Frate::error << e.what() << std::endl;
Frate::error << "Error: Could not open: " << (pro->project_path / file_name) << std::endl;
Utils::debug(e.what());
return false;
}
json data;
try {
json data = json::parse(file);
pro->checkKeys(data);
pro->fromJson(data);
//Simplfied this fucking code
} catch (json::exception &e) {
Frate::error << e.what() << std::endl;
Frate::error << "Error: Could not load: " << (pro->project_path / file_name) << std::endl;

try{
json j = json::parse(file);
pro->fromJson(j);
}catch(std::exception &e){
Utils::debug(e.what());
return false;
}
file.close();
project_present = true;

return true;
};
} // namespace Command
9 changes: 6 additions & 3 deletions src/Command/Helpers/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ namespace Frate::Command {
#else
pro->project_path = std::filesystem::current_path();
#endif
if(LoadProjectJson()){
this->project_present = true;
}
}
bool Interface::execute(){

Expand Down Expand Up @@ -235,8 +238,8 @@ namespace Frate::Command {
for(Handler& handler : commands){
for(std::string& alias : handler.aliases){
if(alias == command){
if(handler.requires_project && !LoadProjectJson()){
Frate::error << "Error: Project not found and is required for this command" << ENDL;
if(!project_present && handler.requires_project){
std::cout << "Error: Project not found and command: " << command << " requires a project" << ENDL;
return false;
}
found_alias = true;
Expand Down Expand Up @@ -289,7 +292,7 @@ namespace Frate::Command {
Frate::error << "Command not implemented: " << command;
return false;
}
if(handler.requires_project && !LoadProjectJson()){
if(handler.requires_project && !project_present){
Frate::error << "Error: Project not found and command: " << command << " requires a project" << ENDL;
return false;
}
Expand Down
90 changes: 68 additions & 22 deletions src/Command/Helpers/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ namespace Frate::Command {
/*
* Welp reflection is a bitch aint it
*/
void Project::fromJson(json j){
void Project::fromJson(json j){
checkKeys(j);
try{
project_name = j["project_name"];
cmake_version = j["cmake_version"];
project_version = j["project_version"];
Expand All @@ -43,40 +45,74 @@ namespace Frate::Command {
keywords = j["keywords"];
for (auto &dep : j["dependencies"]) {
Package d;
d.name = dep["name"];
d.git = dep["git"];
d.selected_version = dep["version"];
d.target_link = dep["target_link"];
d.name = dep["name"].is_null() ? "" : dep["name"].is_null() ? "" : dep["name"];
d.git = dep["git"].is_null() ? "" : dep["git"].is_null() ? "" : dep["git"];
d.selected_version = dep["version"].is_null() ? "" : dep["version"];
d.target_link = dep["target_link"].is_null() ? "" : dep["target_link"];
if(d.target_link.empty()){
warning <<
"Warning: target_link is empty for dependency: " << d.name << std::endl;
}
dependencies.push_back(d);
}
std::vector<Mode> temp_modes;
for (auto &mode: j["modes"]){
Mode m;
m.name = mode["name"];
m.name = mode["name"].is_null() ? "" : mode["name"];
m.flags = mode["flags"];
for (auto &dep : mode["dependencies"]) {
Package d;
d.name = dep["name"];
d.git = dep["git"];
d.selected_version = dep["version"];
d.target_link = dep["target_link"];
m.dependencies.push_back(d);
Package new_dep;
new_dep.name = dep["name"].is_null() ? "" : dep["name"];
new_dep.git = dep["git"].is_null() ? "" : dep["git"];
new_dep.selected_version = dep["version"].is_null() ? "" : dep["version"];
new_dep.target_link = dep["target_link"].is_null() ? "" : dep["target_link"];
if(new_dep.target_link == ""){
warning <<
"Warning: target_link is empty for dependency: " << new_dep.name <<
" in mode: " << m.name << std::endl;
}

m.dependencies.push_back(new_dep);
}
temp_modes.push_back(m);
}
modes = temp_modes;
flags = j["flags"];
for(auto [key, value] : j["prompts"].items()){
ProjectPrompt prompt;
prompt.value = value["value"];
prompt.type = value["type"];
for(auto &option : value["options"]){
prompt.options.push_back(option);
toolchains = j["toolchains"];
if(!j.contains("prompts")){
prompts = {};
}else{
for(auto [key, value] : j["prompts"].items()){
ProjectPrompt prompt;

prompt.type = value["type"].is_null() ? "string" : value["type"];

prompt.text = value["text"].is_null() ? "missing prompt text" : value["text"];


if(value["options"].is_null()){
prompt.options = {};
}else{
for(auto &option : value["options"]){
prompt.options.push_back(option);
}
}


if(value["default_value"].is_null()){
prompt.default_value = "";
}else{
prompt.default_value = value["default_value"];
}

prompts[key] = prompt;
}
prompt.default_value = value["default_value"];
prompts[key] = prompt;
}
} catch (std::exception &e){
Utils::debug(e.what());
error << "Error while parsing json for project" << std::endl;
}
}
nlohmann::json Project::toJson(){
using nlohmann::json;
std::vector<json> deps;
Expand Down Expand Up @@ -126,6 +162,15 @@ namespace Frate::Command {
j["project_type"] = project_type;
j["project_description"] = project_description;
j["toolchains"] = toolchains;
j["prompts"] = json::object();
for(auto [key, value] : prompts){
json prompt;
prompt["type"] = value.type;
prompt["text"] = value.text;
prompt["options"] = value.options;
prompt["default_value"] = value.default_value;
j["prompts"][key] = prompt;
}
return j;

};
Expand All @@ -149,10 +194,11 @@ namespace Frate::Command {
{"authors", false},
{"project_type", false},
{"project_description", false},
{"toolchains", false}
{"toolchains", false},
{"prompts", false}
};
for (std::pair<std::string, bool> &key: required_keys){
if (j.find(key.first) == j.end()){
if (!j.contains(key.first)){
Frate::error << "Missing required key: " << key.first << std::endl;
} else {
key.second = true;
Expand Down
Loading

0 comments on commit 3d6a1c2

Please sign in to comment.