Skip to content

Commit

Permalink
completely overhauled the template management system
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Jan 16, 2024
1 parent 8fb474d commit c021a0d
Show file tree
Hide file tree
Showing 40 changed files with 1,386 additions and 469 deletions.
20 changes: 19 additions & 1 deletion include/Frate/Frate.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
#pragma once
#include "Frate/Utils/CLI.hpp"
#include <Frate/Utils/General.hpp>

namespace Frate {} // namespace Frate
namespace Frate {
class FrateException : public std::exception {
protected:
std::string message;

public:
FrateException(const std::string message)
: message(Utils::CLI::Ansi::RED + message + Utils::CLI::Ansi::RESET) {


}

[[nodiscard]] const char *what() const noexcept override {
return (message.c_str());
};
};

} // namespace Fra
11 changes: 11 additions & 0 deletions include/Frate/Lua/Exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "Frate/Frate.hpp"

namespace Frate::Lua {

class LuaException : public FrateException {
public:
LuaException(const std::string &message) : FrateException(message) {}
};

} // namespace Frate::Lua
15 changes: 9 additions & 6 deletions include/Frate/LuaAPI.hpp → include/Frate/Lua/LuaAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "inja.hpp"
#include <filesystem>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <sol/forward.hpp>
#include <sol/sol.hpp>

namespace Frate::LuaAPI {
namespace Frate::Lua {

using nlohmann::json;
using Project::Config;
using std::filesystem::path;
Expand All @@ -28,32 +28,35 @@ namespace Frate::LuaAPI {
static sol::table fetch_json(const std::string &url, sol::this_state lua);
};

sol::table to_table(nlohmann::json json, sol::state_view &lua);
nlohmann::json from_table(sol::table in_table);

/*
* Registers the project scripts with the project that is specifed
*/
[[deprecated("Use TemplateEnvironment")]]
bool registerProjectScripts(inja::Environment &env, sol::state &lua,
path script_path,
std::shared_ptr<Project::Config> project);

/*
* Registers the project with the user types for the project struct
*/
[[deprecated("Use TemplateEnvironment")]]
bool registerProject(sol::state &lua, std::shared_ptr<Project::Config> pro);
/*
* Registers api functions for the lua state
* and initializes the lua state
*/
[[deprecated("Use TemplateEnvironment")]]
void registerAPI(sol::state &lua);

/*
* Runs __init__ scripts
*/
[[deprecated("Use TemplateEnvironment")]]
bool initScripts(sol::state &lua, std::shared_ptr<Project::Config> project);

/*
* Runs __post__ scripts
*/
[[deprecated("Use TemplateEnvironment")]]
bool postScripts(sol::state &lua, std::shared_ptr<Project::Config> project);
} // namespace Frate::LuaAPI
} // namespace Frate::Lua
201 changes: 201 additions & 0 deletions include/Frate/Lua/TemplateEnvironment.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#pragma once
#include "Frate/Project/Config.hpp"
#include "Frate/Utils/Logging.hpp"
#include "inja.hpp"
#include <Frate/Lua/Exceptions.hpp>
#include <sol/state.hpp>
#include <string>
#include <unordered_map>

namespace Frate::Lua {

class TemplateEnvironmentException : public FrateException {
public:
explicit TemplateEnvironmentException(const std::string &message)
: FrateException(message) {}
};
/*
* This class is responsible for handling the lua template environent,
* managing macros, pre and post scripts, and templating files. It is designed
* in such a way that allows complete control of over the template render
* pipeline.
* To use this, start by registering all of your scripts using your method of
* choice, then use templateFile to render your file to the output path
*/
class TemplateEnvironment {
private:
std::shared_ptr<sol::state> lua;
inja::Environment env;
std::unordered_map<std::string, std::string> macro_scripts;
std::unordered_map<std::string, std::string> init_scripts;
std::unordered_map<std::string, std::string> post_scripts;
std::shared_ptr<Project::Config> pro;

void register_user_types();
void register_frate_api();
void register_inja_function(std::string name, std::string lua_script_text);

public:
/*
* A project config is required to register the project with the lua
* environment
* @param pro: The project config
* @throws LuaException: If there is an error while registering the project
*/
TemplateEnvironment(std::shared_ptr<Project::Config> pro)
: pro(pro) {

lua = std::make_shared<sol::state>();

macro_scripts = std::unordered_map<std::string, std::string>();
init_scripts = std::unordered_map<std::string, std::string>();
post_scripts = std::unordered_map<std::string, std::string>();

if(pro == nullptr){
throw TemplateEnvironmentException("Project is null on construction");
}

try {
Utils::verbose << "Registering frate api" << std::endl;
register_frate_api();

} catch (std::exception &e) {
Utils::error << e.what() << std::endl;
throw TemplateEnvironmentException("Error registering frate api");
}

try {
Utils::verbose << "Registering user types" << std::endl;
register_user_types();

} catch (std::exception &e) {
Utils::error << e.what() << std::endl;
throw TemplateEnvironmentException("Error registering user types");
}
};

~TemplateEnvironment() = default;
/*
* Registers a new macro script the name of the script is the identifier
* @note Using relativePathToNamespace to get a namespaced script name is
* advised
* @param name: The name of the script
* @param script_text: The text of the script
* @throws LuaException: If there is an error while registering the script
*/
void registerMacroScript(std::string name, std::string script_text);
/*
* Registers a new init script the name of the script is the identifier
* currently the name is not used for anything
* @param name: The name of the script
* @param script_text: The text of the script
* @throws LuaException: If there is an error while registering the script
*/
void registerInitScript(std::string name, std::string script_text);
/*
* Registers a new post script the name of the script is the identifier
* currently the name is not used for anything
* @param name: The name of the script
* @param script_text: The text of the script
* @throws LuaException: If there is an error while registering the script
*/
void registerPostScript(std::string name, std::string script_text);

/*
* @param input_file: The path to the input .inja file
* @param output_file: The path to the output file
* @throws LuaException: If there is an error while templating the file
* @throws std::exception: If there is an error while writing the file
*/
void templateFile(std::filesystem::path input_file,
std::filesystem::path output_file);
/*
* Runs __init__ scripts assuming they have been registered
* @throws LuaException: If there is an error while running the script
*/
void runInitScripts();
/*
* Runs __post__ scripts assuming they have been registered
* @throws LuaException: If there is an error while running the script
*/
void runPostScripts();

/*
* Providing this a root path and a file path it will return the dot
* notation version of the path while also removing the extension
* @param root_path: The root path which you would like to base the relative
* path off of
* @param file_path: The file path which you would like to convert to dot
* notation
* @return: The dot notation version of the file path
* @example: relativePathToNamespace("/home/user/project",
* "/home/user/project/src/main.cpp") -> "src.main"
*/
static std::string relativePathToNamespace(std::filesystem::path root_path,
std::filesystem::path file_path);
/*
* Providing this a file path it will return the dot notation version of the
* path while also removing the extension
* @param file_path: The file path which you would like to convert to dot
* notation
* @return: The dot notation version of the file path
* @example: relativePathToNamespace("src/main.cpp") -> "src.main"
*/
static std::string
relativePathToNamespace(std::filesystem::path relative_path);


// Getters

[[nodiscard]] const std::shared_ptr<Project::Config> &getProjectConfig() const {
return pro;
}

[[nodiscard]] const std::unordered_map<std::string, std::string> &getMacroScripts() const {
return macro_scripts;
}

[[nodiscard]] const std::unordered_map<std::string, std::string> &getInitScripts() const {
return init_scripts;
}

[[nodiscard]] const std::unordered_map<std::string, std::string> &getPostScripts() const {
return post_scripts;
}

[[nodiscard]] const std::shared_ptr<sol::state> &getLuaState() const {
return lua;
}

[[nodiscard]] const inja::Environment &getInjaEnv() const {
return env;
}

//Setters

void setProjectConfig(const std::shared_ptr<Project::Config> &pro) {
TemplateEnvironment::pro = pro;
}

// Display

friend std::ostream &operator<<(std::ostream &os,
const TemplateEnvironment &environment) {

for(auto [name, script] : environment.macro_scripts){
os << "Macro Script: " << name << std::endl;
os << script << std::endl;
}
for(auto [name, script] : environment.init_scripts){
os << "Init Script: " << name << std::endl;
os << script << std::endl;
}
for(auto [name, script] : environment.post_scripts){
os << "Post Script: " << name << std::endl;
os << script << std::endl;
}
return os;
}

};
} // namespace Frate::Lua
11 changes: 11 additions & 0 deletions include/Frate/Lua/Utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <sol/sol.hpp>
#include <nlohmann/json.hpp>
namespace Frate::Lua {

sol::table to_table(nlohmann::json in_json, sol::state_view &lua);
nlohmann::json from_table(sol::table in_table);
bool is_table_array(const sol::table &table);


}
4 changes: 3 additions & 1 deletion include/Frate/Project/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Frate::Project {
bool loaded_json{false};
std::filesystem::path template_path;
TemplateMeta current_template;
std::vector<std::filesystem::path> template_files;
std::vector<std::filesystem::path> script_files;
std::string name;
std::string description;
std::string type{""};
Expand Down Expand Up @@ -74,7 +76,7 @@ namespace Frate::Project {
std::unordered_map<std::string, json> global{};
friend void from_json(const json &json_obj, Config &pro);
friend void to_json(json &json_obj, const Config &pro);
void fromTemplate(Template &_template);
void fromTemplate(TemplateConfig &_template);
};

} // namespace Frate::Project
35 changes: 35 additions & 0 deletions include/Frate/Project/Exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "Frate/Frate.hpp"
#include <string>

namespace Frate::Project {

class TemplateException : public FrateException {
public:
TemplateException(const std::string &message) : FrateException(message) {}
};

class TemplateNotInstalled : public TemplateException {
public:
TemplateNotInstalled(const std::string &message)
: TemplateException(message) {}
};

class TemplateIndexNotLoaded : public TemplateException {
public:
TemplateIndexNotLoaded(const std::string &message)
: TemplateException(message) {}
};

class TemplateConfigNotFound : public TemplateException {
public:
TemplateConfigNotFound(const std::string &message)
: TemplateException(message) {}
};

class TemplateFileMapEmpty : public TemplateException {
public:
TemplateFileMapEmpty(const std::string &message)
: TemplateException(message) {}
};

} // namespace Frate::Project
Loading

0 comments on commit c021a0d

Please sign in to comment.