Skip to content

Commit

Permalink
added support for directory recursing in the lua api
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Dec 12, 2023
1 parent cadd9a3 commit 8487470
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 61 deletions.
3 changes: 3 additions & 0 deletions include/Frate/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ namespace Frate::Command {
nlohmann::json toJson();
bool save();
void checkKeys(json j);
std::string getPath(){
return project_path.string();
};
std::unordered_map<std::string,ProjectPrompt> prompts{};
} Project;

Expand Down
157 changes: 96 additions & 61 deletions src/Lua/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,87 @@
#include <sol/variadic_args.hpp>

namespace Frate::LuaAPI {
using std::filesystem::path;

std::string format(const std::string &str, sol::variadic_args var_args) {
std::vector<std::string> args;
std::string result;

for (const auto &arg : var_args) {
if (arg.is<std::string>()) {
args.push_back(arg.as<std::string>());
} else if (arg.is<int>()) {
args.push_back(std::to_string(arg.as<int>()));
} else if (arg.is<bool>()) {
args.push_back(arg.as<bool>() ? "true" : "false");
} else if (arg.is<float>()) {
args.push_back(std::to_string(arg.as<float>()));
} else {
args.push_back("nil");
using std::filesystem::path;
std::vector<std::string> get_paths_recurse(std::string input_path) {

std::filesystem::path deepest_path = std::filesystem::current_path();



info << "Getting paths from " << input_path << std::endl;
//check if path is absolute
if (input_path[0] != '/') {
error << "Frate Lua Api Error: Path in get_paths_recurse must be absolute" << std::endl;
exit(1);
}
}

for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '{') {
if (str[i + 1] == '{') {
result += '{';
i++;
if(input_path.find(deepest_path.string()) == std::string::npos){
error << "Frate Lua Api Error: Path in get_paths_recurse must be in project directory" << std::endl;
exit(1);
}


std::vector<std::string> paths;
for (const auto &p : std::filesystem::recursive_directory_iterator(input_path)) {
paths.push_back(p.path().string());
}

return paths;
}
std::string format(const std::string &str, sol::variadic_args var_args) {
std::vector<std::string> args;
std::string result;

for (const auto &arg : var_args) {
if (arg.is<std::string>()) {
args.push_back(arg.as<std::string>());
} else if (arg.is<int>()) {
args.push_back(std::to_string(arg.as<int>()));
} else if (arg.is<bool>()) {
args.push_back(arg.as<bool>() ? "true" : "false");
} else if (arg.is<float>()) {
args.push_back(std::to_string(arg.as<float>()));
} else {
size_t j = i + 1;
while (j < str.size() && str[j] != '}') {
j++;
}
if (j == str.size()) {
throw std::runtime_error("Invalid format string");
}
std::string index = str.substr(i + 1, j - i - 1);
if (index == "0") {
result += args[0];
args.push_back("nil");
}
}

for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '{') {
if (str[i + 1] == '{') {
result += '{';
i++;
} else {
int idx = std::stoi(index);
if (idx < 1 || idx > args.size()) {
size_t j = i + 1;
while (j < str.size() && str[j] != '}') {
j++;
}
if (j == str.size()) {
throw std::runtime_error("Invalid format string");
}
result += args[idx - 1];
std::string index = str.substr(i + 1, j - i - 1);
if (index == "0") {
result += args[0];
} else {
int idx = std::stoi(index);
if (idx < 1 || idx > args.size()) {
throw std::runtime_error("Invalid format string");
}
result += args[idx - 1];
}
i = j;
}
i = j;
} else {
result += str[i];
}
} else {
result += str[i];
}
}

return result;
return result;
}

bool initLua(sol::state &lua){
lua.open_libraries(sol::lib::base, sol::lib::package);

lua.open_libraries(sol::lib::base, sol::lib::package,sol::lib::string);

return true;
}
Expand All @@ -82,7 +107,7 @@ std::string format(const std::string &str, sol::variadic_args var_args) {
std::string full_script_path = p.string();
//Yoinkin off the lua extension
file_name = file_name.substr(0, file_name.find(".lua"));

std::string prefix;

//Remove the script path
Expand All @@ -108,21 +133,21 @@ std::string format(const std::string &str, sol::variadic_args var_args) {
env.add_callback( key, -1, [&lua, script_path](inja::Arguments input_args){
sol::table args_table = lua.create_table();
for(const nlohmann::json* arg: input_args){
if(arg->is_string()){
args_table.add(arg->get<std::string>());
}else if(arg->is_number()){
args_table.add(arg->get<int>());
}else if(arg->is_boolean()){
args_table.add(arg->get<bool>());
}else{
error << "Error while executing lua script" << std::endl;
exit(1);
}
if(arg->is_string()){
args_table.add(arg->get<std::string>());
}else if(arg->is_number()){
args_table.add(arg->get<int>());
}else if(arg->is_boolean()){
args_table.add(arg->get<bool>());
}else{
error << "Error while executing lua script" << std::endl;
exit(1);
}
}
lua.set("args", args_table);
if(!std::filesystem::exists(script_path)){
error << "Lua script not found at path: " << script_path << std::endl;
exit(1);
error << "Lua script not found at path: " << script_path << std::endl;
exit(1);
}
auto result = lua.script_file(script_path);
if(result.valid()){
Expand Down Expand Up @@ -159,14 +184,15 @@ std::string format(const std::string &str, sol::variadic_args var_args) {
"forks", &Command::Package::forks,
"open_issues", &Command::Package::open_issues,
"watchers", &Command::Package::watchers
);
);

lua.new_usertype<Command::Mode>("Mode",
"new", sol::no_constructor,
"name", &Command::Mode::name,
"flags", &Command::Mode::flags,
"dependencies", &Command::Mode::dependencies
);
);


lua.new_usertype<Command::Project>("Project"
"new", sol::no_constructor,
Expand All @@ -190,7 +216,7 @@ std::string format(const std::string &str, sol::variadic_args var_args) {
"project_type", &Command::Project::project_type,
"keywords", &Command::Project::keywords,
"prompts", &Command::Project::prompts
);
);

lua.new_usertype<Command::ProjectPrompt>("ProjectPrompt",
"value", &Command::ProjectPrompt::value,
Expand All @@ -199,14 +225,23 @@ std::string format(const std::string &str, sol::variadic_args var_args) {
"getint", &Command::ProjectPrompt::get<int>,
"getbool", &Command::ProjectPrompt::get<bool>,
"getfloat", &Command::ProjectPrompt::get<float>
);
);



return true;
}

void registerAPI(sol::state &lua) {
lua.set_function("format", &format);
lua.set_function("get_paths_recurse", &get_paths_recurse);
//lua.set_function("get_project_path", &Command::Project::getPath);
lua.set_function("get_path", []() {
return std::filesystem::current_path().string()
#ifdef DEBUG
+ "/build"
#endif
;
});
initLua(lua);
}
}

0 comments on commit 8487470

Please sign in to comment.