From 8487470c1923fd9e5be204227c149b08b33ffa0f Mon Sep 17 00:00:00 2001 From: Jon-Michael Hartway Date: Mon, 11 Dec 2023 20:06:47 -0500 Subject: [PATCH] added support for directory recursing in the lua api --- include/Frate/Command.hpp | 3 + src/Lua/API.cpp | 157 +++++++++++++++++++++++--------------- 2 files changed, 99 insertions(+), 61 deletions(-) diff --git a/include/Frate/Command.hpp b/include/Frate/Command.hpp index 2bed523..49697f7 100644 --- a/include/Frate/Command.hpp +++ b/include/Frate/Command.hpp @@ -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 prompts{}; } Project; diff --git a/src/Lua/API.cpp b/src/Lua/API.cpp index 6d3184e..31b2736 100644 --- a/src/Lua/API.cpp +++ b/src/Lua/API.cpp @@ -8,62 +8,87 @@ #include namespace Frate::LuaAPI { -using std::filesystem::path; - -std::string format(const std::string &str, sol::variadic_args var_args) { - std::vector args; - std::string result; - - for (const auto &arg : var_args) { - if (arg.is()) { - args.push_back(arg.as()); - } else if (arg.is()) { - args.push_back(std::to_string(arg.as())); - } else if (arg.is()) { - args.push_back(arg.as() ? "true" : "false"); - } else if (arg.is()) { - args.push_back(std::to_string(arg.as())); - } else { - args.push_back("nil"); + using std::filesystem::path; + std::vector 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 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 args; + std::string result; + + for (const auto &arg : var_args) { + if (arg.is()) { + args.push_back(arg.as()); + } else if (arg.is()) { + args.push_back(std::to_string(arg.as())); + } else if (arg.is()) { + args.push_back(arg.as() ? "true" : "false"); + } else if (arg.is()) { + args.push_back(std::to_string(arg.as())); } 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; } @@ -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 @@ -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()); - }else if(arg->is_number()){ - args_table.add(arg->get()); - }else if(arg->is_boolean()){ - args_table.add(arg->get()); - }else{ - error << "Error while executing lua script" << std::endl; - exit(1); - } + if(arg->is_string()){ + args_table.add(arg->get()); + }else if(arg->is_number()){ + args_table.add(arg->get()); + }else if(arg->is_boolean()){ + args_table.add(arg->get()); + }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()){ @@ -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("Mode", "new", sol::no_constructor, "name", &Command::Mode::name, "flags", &Command::Mode::flags, "dependencies", &Command::Mode::dependencies - ); + ); + lua.new_usertype("Project" "new", sol::no_constructor, @@ -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("ProjectPrompt", "value", &Command::ProjectPrompt::value, @@ -199,14 +225,23 @@ std::string format(const std::string &str, sol::variadic_args var_args) { "getint", &Command::ProjectPrompt::get, "getbool", &Command::ProjectPrompt::get, "getfloat", &Command::ProjectPrompt::get - ); + ); + - 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); } }