diff --git a/include/Frate/LuaAPI.hpp b/include/Frate/LuaAPI.hpp index 190fd7b..1c198a3 100644 --- a/include/Frate/LuaAPI.hpp +++ b/include/Frate/LuaAPI.hpp @@ -11,11 +11,19 @@ namespace Frate::LuaAPI { using std::filesystem::path; + class FrateApi { + public: + FrateApi() = default; + ~FrateApi() = default; + static std::string get_os(); + static std::vector get_paths_recurse(std::string input_path); + static std::string get_path(); + static std::string format(const std::string &str, sol::variadic_args var_args); + }; sol::table to_table(const nlohmann::json& json); nlohmann::json from_table(sol::table in_table); - std::string format(const std::string &str, sol::variadic_args var_args); /* * Registers the project scripts with the project that is specifed */ diff --git a/src/Lua/API.cpp b/src/Lua/API.cpp index f9d33c6..c3572a6 100644 --- a/src/Lua/API.cpp +++ b/src/Lua/API.cpp @@ -10,104 +10,7 @@ namespace Frate::LuaAPI { using std::filesystem::path; - class FrateApi { - public: - FrateApi() = default; - ~FrateApi() = default; - static std::string get_os(); - static std::vector get_paths_recurse(std::string input_path); - static std::string get_path(); - static std::string format(const std::string &str, sol::variadic_args var_args); - }; - std::string FrateApi::get_os() { - return Frate::Constants::BUILD_OS; - } - std::string FrateApi::get_path() { - return std::filesystem::current_path().string() - #ifdef DEBUG - + "/build" - #endif - ; - } - std::vector FrateApi::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); - } - - 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 FrateApi::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"); - } - } - - for (size_t i = 0; i < str.size(); i++) { - if (str[i] == '{') { - if (str[i + 1] == '{') { - result += '{'; - i++; - } 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]; - } 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; - } - } else { - result += str[i]; - } - } - - return result; - } - - bool initLua(sol::state &lua){ - lua.open_libraries(sol::lib::base, sol::lib::package,sol::lib::string); - - return true; - } bool registerProjectScripts(inja::Environment &env, sol::state &lua, path script_path) { @@ -177,71 +80,9 @@ namespace Frate::LuaAPI { return true; } - bool registerProject(sol::state &lua, std::shared_ptr pro) { - - lua.set("project", pro); - lua.new_usertype("Package", - "new", sol::no_constructor, - "name", &Command::Package::name, - "git", &Command::Package::git, - "git_short", &Command::Package::git_short, - "git_prefixed", &Command::Package::git_prefixed, - "selected_version", &Command::Package::selected_version, - "versions", &Command::Package::versions, - "target_link", &Command::Package::target_link, - "description", &Command::Package::description, - "selected_version", &Command::Package::selected_version, - "git_description", &Command::Package::git_description, - "language", &Command::Package::language, - "license", &Command::Package::license, - "owner", &Command::Package::owner, - "owner_type", &Command::Package::owner_type, - "stars", &Command::Package::stars, - "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, - "name", &Command::Project::project_name, - "version", &Command::Project::project_version, - "description", &Command::Project::project_description, - "authors", &Command::Project::authors, - "dependencies", &Command::Project::dependencies, - "toolchains", &Command::Project::toolchains, - "flags", &Command::Project::flags, - "modes", &Command::Project::modes, - "libs", &Command::Project::libs, - "license", &Command::Project::license, - "git", &Command::Project::git, - "cmake_version", &Command::Project::cmake_version, - "build_dir", &Command::Project::build_dir, - "src_dir", &Command::Project::src_dir, - "include_dir", &Command::Project::include_dir, - "lang_version", &Command::Project::lang_version, - "lang", &Command::Project::lang, - "project_type", &Command::Project::project_type, - "keywords", &Command::Project::keywords, - "prompts", &Command::Project::prompts - ); - - lua.new_usertype("ProjectPrompt", - "value", &Command::ProjectPrompt::value, - "default", &Command::ProjectPrompt::default_value, - "getstr", &Command::ProjectPrompt::get, - "getint", &Command::ProjectPrompt::get, - "getbool", &Command::ProjectPrompt::get, - "getfloat", &Command::ProjectPrompt::get - ); + void registerAPI(sol::state &lua) { + lua.open_libraries(sol::lib::base, sol::lib::package,sol::lib::string); lua.new_usertype("frate", "new", sol::no_constructor, @@ -250,12 +91,5 @@ namespace Frate::LuaAPI { "get_paths_recurse", &FrateApi::get_paths_recurse, "format", &FrateApi::format ); - - - return true; - } - - void registerAPI(sol::state &lua) { - initLua(lua); } } diff --git a/src/Lua/Format.cpp b/src/Lua/Format.cpp new file mode 100644 index 0000000..c92fae4 --- /dev/null +++ b/src/Lua/Format.cpp @@ -0,0 +1,55 @@ +#include + + +namespace Frate::LuaAPI { + std::string FrateApi::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"); + } + } + + for (size_t i = 0; i < str.size(); i++) { + if (str[i] == '{') { + if (str[i + 1] == '{') { + result += '{'; + i++; + } 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]; + } 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; + } + } else { + result += str[i]; + } + } + + return result; + } +} diff --git a/src/Lua/GetOS.cpp b/src/Lua/GetOS.cpp new file mode 100644 index 0000000..785cf29 --- /dev/null +++ b/src/Lua/GetOS.cpp @@ -0,0 +1,8 @@ +#include +#include +namespace Frate::LuaAPI { + using std::filesystem::path; + std::string FrateApi::get_os() { + return Frate::Constants::BUILD_OS; + } +} diff --git a/src/Lua/GetPath.cpp b/src/Lua/GetPath.cpp new file mode 100644 index 0000000..c988de8 --- /dev/null +++ b/src/Lua/GetPath.cpp @@ -0,0 +1,11 @@ +#include +#include +namespace Frate::LuaAPI { + std::string FrateApi::get_path() { + return std::filesystem::current_path().string() + #ifdef DEBUG + + "/build" + #endif + ; + } +} diff --git a/src/Lua/GetPathsRecurse.cpp b/src/Lua/GetPathsRecurse.cpp new file mode 100644 index 0000000..24621a3 --- /dev/null +++ b/src/Lua/GetPathsRecurse.cpp @@ -0,0 +1,27 @@ +#include +#include + +namespace Frate::LuaAPI { + std::vector FrateApi::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); + } + + 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; + } +} diff --git a/src/Lua/RegisterProject.cpp b/src/Lua/RegisterProject.cpp new file mode 100644 index 0000000..4f768bc --- /dev/null +++ b/src/Lua/RegisterProject.cpp @@ -0,0 +1,74 @@ +#include + +namespace Frate::LuaAPI { + bool registerProject(sol::state &lua, std::shared_ptr pro) { + + lua.set("project", pro); + lua.new_usertype("Package", + "new", sol::no_constructor, + "name", &Command::Package::name, + "git", &Command::Package::git, + "git_short", &Command::Package::git_short, + "git_prefixed", &Command::Package::git_prefixed, + "selected_version", &Command::Package::selected_version, + "versions", &Command::Package::versions, + "target_link", &Command::Package::target_link, + "description", &Command::Package::description, + "selected_version", &Command::Package::selected_version, + "git_description", &Command::Package::git_description, + "language", &Command::Package::language, + "license", &Command::Package::license, + "owner", &Command::Package::owner, + "owner_type", &Command::Package::owner_type, + "stars", &Command::Package::stars, + "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, + "name", &Command::Project::project_name, + "version", &Command::Project::project_version, + "description", &Command::Project::project_description, + "authors", &Command::Project::authors, + "dependencies", &Command::Project::dependencies, + "toolchains", &Command::Project::toolchains, + "flags", &Command::Project::flags, + "modes", &Command::Project::modes, + "libs", &Command::Project::libs, + "license", &Command::Project::license, + "git", &Command::Project::git, + "cmake_version", &Command::Project::cmake_version, + "build_dir", &Command::Project::build_dir, + "src_dir", &Command::Project::src_dir, + "include_dir", &Command::Project::include_dir, + "lang_version", &Command::Project::lang_version, + "lang", &Command::Project::lang, + "project_type", &Command::Project::project_type, + "keywords", &Command::Project::keywords, + "prompts", &Command::Project::prompts + ); + + lua.new_usertype("ProjectPrompt", + "value", &Command::ProjectPrompt::value, + "default", &Command::ProjectPrompt::default_value, + "getstr", &Command::ProjectPrompt::get, + "getint", &Command::ProjectPrompt::get, + "getbool", &Command::ProjectPrompt::get, + "getfloat", &Command::ProjectPrompt::get + ); + + + + return true; + } +}