-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completely overhauled the template management system
- Loading branch information
Showing
40 changed files
with
1,386 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.