Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:frate-dev/frate into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Dec 12, 2023
2 parents 0ecd0ad + b8b645b commit 6284dde
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 169 deletions.
10 changes: 9 additions & 1 deletion include/Frate/LuaAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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
*/
Expand Down
170 changes: 2 additions & 168 deletions src/Lua/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::string> 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<std::string> 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<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");
}
}

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) {

Expand Down Expand Up @@ -177,71 +80,9 @@ namespace Frate::LuaAPI {
return true;
}

bool registerProject(sol::state &lua, std::shared_ptr<Command::Project> pro) {

lua.set("project", pro);
lua.new_usertype<Command::Package>("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<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,
"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<Command::ProjectPrompt>("ProjectPrompt",
"value", &Command::ProjectPrompt::value,
"default", &Command::ProjectPrompt::default_value,
"getstr", &Command::ProjectPrompt::get<std::string>,
"getint", &Command::ProjectPrompt::get<int>,
"getbool", &Command::ProjectPrompt::get<bool>,
"getfloat", &Command::ProjectPrompt::get<float>
);
void registerAPI(sol::state &lua) {
lua.open_libraries(sol::lib::base, sol::lib::package,sol::lib::string);

lua.new_usertype<FrateApi>("frate",
"new", sol::no_constructor,
Expand All @@ -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);
}
}
55 changes: 55 additions & 0 deletions src/Lua/Format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <Frate/LuaAPI.hpp>


namespace Frate::LuaAPI {
std::string FrateApi::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");
}
}

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;
}
}
8 changes: 8 additions & 0 deletions src/Lua/GetOS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <Frate/LuaAPI.hpp>
#include <Frate/Constants.hpp>
namespace Frate::LuaAPI {
using std::filesystem::path;
std::string FrateApi::get_os() {
return Frate::Constants::BUILD_OS;
}
}
11 changes: 11 additions & 0 deletions src/Lua/GetPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <Frate/LuaAPI.hpp>
#include <filesystem>
namespace Frate::LuaAPI {
std::string FrateApi::get_path() {
return std::filesystem::current_path().string()
#ifdef DEBUG
+ "/build"
#endif
;
}
}
27 changes: 27 additions & 0 deletions src/Lua/GetPathsRecurse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <Frate/LuaAPI.hpp>
#include <filesystem>

namespace Frate::LuaAPI {
std::vector<std::string> 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<std::string> paths;
for (const auto &p : std::filesystem::recursive_directory_iterator(input_path)) {
paths.push_back(p.path().string());
}

return paths;
}
}
74 changes: 74 additions & 0 deletions src/Lua/RegisterProject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <Frate/LuaAPI.hpp>

namespace Frate::LuaAPI {
bool registerProject(sol::state &lua, std::shared_ptr<Command::Project> pro) {

lua.set("project", pro);
lua.new_usertype<Command::Package>("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<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,
"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<Command::ProjectPrompt>("ProjectPrompt",
"value", &Command::ProjectPrompt::value,
"default", &Command::ProjectPrompt::default_value,
"getstr", &Command::ProjectPrompt::get<std::string>,
"getint", &Command::ProjectPrompt::get<int>,
"getbool", &Command::ProjectPrompt::get<bool>,
"getfloat", &Command::ProjectPrompt::get<float>
);



return true;
}
}

0 comments on commit 6284dde

Please sign in to comment.