Skip to content

Commit

Permalink
Fixed inja files being left in src
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Feb 11, 2024
1 parent feb1d92 commit f0e4b4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
61 changes: 36 additions & 25 deletions src/Template/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@
#include <Frate/Project/Config.hpp>
#include <Frate/Template/Exceptions.hpp>
#include <Frate/Template/Renderer.hpp>
#include <Frate/Utils/CLIPrompt.hpp>
#include <Frate/Utils/FileFilter.hpp>
#include <Frate/Utils/Logging.hpp>
#include <Frate/Utils/Macros.hpp>
#include <Frate/Utils/CLIPrompt.hpp>
#include <filesystem>
#include <fstream>
#include <sol/stack.hpp>

namespace Frate::Project {
using std::filesystem::directory_entry;

TemplateRenderer::TemplateRenderer(const nlohmann::json &json_obj): install_path(Constants::INSTALLED_TEMPLATE_PATH / this->name / this->hash) {
TemplateRenderer::TemplateRenderer(const nlohmann::json &json_obj)
: install_path(Constants::INSTALLED_TEMPLATE_PATH / this->name /
this->hash) {
Utils::verbose << "Creating template meta from json with contents: "
<< json_obj << std::endl;
from_json(json_obj, *this);
}

TemplateRenderer::TemplateRenderer(): install_path(Constants::INSTALLED_TEMPLATE_PATH / this->name / this->hash) {
}
TemplateRenderer::TemplateRenderer()
: install_path(Constants::INSTALLED_TEMPLATE_PATH / this->name /
this->hash) {}

TemplateRenderer::TemplateRenderer(TemplateIndexEntry &entry)
: name(entry.getName()), description(entry.getDescription()),
hash(entry.getBranchHash(Constants::TEMPLATE_BRANCH)), git(entry.getGit()),
install_path(Constants::INSTALLED_TEMPLATE_PATH / this->name /
this->hash) {}
hash(entry.getBranchHash(Constants::TEMPLATE_BRANCH)),
git(entry.getGit()), install_path(Constants::INSTALLED_TEMPLATE_PATH /
this->name / this->hash) {}

void from_json(const nlohmann::json &json_obj, TemplateRenderer &temp) {
FROM_JSON_FIELD(temp, name);
FROM_JSON_FIELD(temp, description);
FROM_JSON_FIELD(temp, hash);
FROM_JSON_FIELD(temp, git);

temp.install_path = Constants::INSTALLED_TEMPLATE_PATH / temp.name / temp.hash;
temp.install_path =
Constants::INSTALLED_TEMPLATE_PATH / temp.name / temp.hash;
}

void to_json(nlohmann::json &json_obj, const TemplateRenderer &temp) {
Expand All @@ -46,15 +50,17 @@ namespace Frate::Project {
TO_JSON_FIELD(temp, git);
}

std::ostream &operator<<(std::ostream &os_stream, const TemplateRenderer &temp) {
std::ostream &operator<<(std::ostream &os_stream,
const TemplateRenderer &temp) {
os_stream << "Name: " << temp.name << std::endl;
os_stream << "Description: " << temp.description << std::endl;
os_stream << "Hash: " << temp.hash << std::endl;
os_stream << "Git: " << temp.git << std::endl;
return os_stream;
}

void TemplateRenderer::build(std::shared_ptr<Config> config, std::shared_ptr<Local> local) {
void TemplateRenderer::build(std::shared_ptr<Config> config,
std::shared_ptr<Local> local) {

Utils::verbose << "Building template from template at: " << install_path
<< std::endl;
Expand Down Expand Up @@ -105,17 +111,16 @@ namespace Frate::Project {
Utils::info << "Template built" << std::endl;

install_cpm(config);
render(config,local);
render(config, local);
}

void TemplateRenderer::refresh(std::shared_ptr<Config> config, std::shared_ptr<Local> local) {
void TemplateRenderer::refresh(std::shared_ptr<Config> config,
std::shared_ptr<Local> local) {

std::filesystem::path override_path = config->path / "override";


Utils::FileFilter template_filter(install_path);


template_filter.addDirs(
{"scripts", "__init__", "__post__", "cmake_includes"});
template_filter.addFiles({"CMakeLists.txt.inja"});
Expand All @@ -141,7 +146,7 @@ namespace Frate::Project {
file_map[relative_path.string()] = file;
}

render(config,local);
render(config, local);
}

void TemplateRenderer::load_scripts() {
Expand Down Expand Up @@ -206,7 +211,7 @@ namespace Frate::Project {
scripts_loaded = true;
}

void TemplateRenderer::run_prompts(std::shared_ptr<Config> config){
void TemplateRenderer::run_prompts(std::shared_ptr<Config> config) {
for (auto [key, tmpl_prompt] : config->prompts) {
Utils::CLI::Prompt prompt(tmpl_prompt.text, tmpl_prompt.default_value);
if (tmpl_prompt.type == "bool") {
Expand All @@ -221,15 +226,15 @@ namespace Frate::Project {

prompt.run();
auto value = prompt.get<std::string>();
try{
try {
value = prompt.get<std::string>();
}catch(std::exception &e){
} catch (std::exception &e) {
throw TemplatePromptException("Error while getting prompt value");
}
config->prompts[key].value = value;
}

}

void TemplateRenderer::install_cpm(std::shared_ptr<Config> config) {
std::string cpm;

Expand All @@ -250,10 +255,11 @@ namespace Frate::Project {
cpm_file << cpm;
}

void TemplateRenderer::render(std::shared_ptr<Config> config, std::shared_ptr<Local> local) {
void TemplateRenderer::render(std::shared_ptr<Config> config,
std::shared_ptr<Local> local) {
Utils::info << "Rendering template" << std::endl;
Utils::verbose << *this << std::endl;
this->env = std::make_shared<Lua::TemplateEnvironment>(config,local);
this->env = std::make_shared<Lua::TemplateEnvironment>(config, local);

if (!scripts_loaded) {

Expand All @@ -275,18 +281,22 @@ namespace Frate::Project {

non_template_filter.addExtensions({".lua", ".inja"});
non_template_filter.addDirs(
{"scripts", "__init__", "__post__", "cmake_includes"});
{"scripts", "__init__", "__post__", "cmake_includes", "src"});
non_template_filter.addFiles({"template.json"});

all_other_files = non_template_filter.filterOut();

for (auto &file : all_other_files) {
// Finds the relative path i nthe original file map and copies it to the
// output directory
Utils::verbose << "Copying other file: " << file << " to "
<< config->path /
std::filesystem::relative(file, install_path)
<< std::endl;
for (auto [relative_path, file_path] : file_map) {
if (file_path == file) {
std::filesystem::path output_file = config->path / relative_path;
Utils::verbose << "Copying file: " << file_path << " to "
Utils::verbose << "Copying other file: " << file_path << " to "
<< output_file << std::endl;
if (!std::filesystem::exists(output_file.parent_path())) {
std::filesystem::create_directories(output_file.parent_path());
Expand All @@ -296,10 +306,9 @@ namespace Frate::Project {
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::overwrite_existing);
}

}
}

// Generate a list of all files that are templates
for (auto [relative_path, file_path] : file_map) {
if (file_path.extension() == ".inja") {
Expand All @@ -308,6 +317,8 @@ namespace Frate::Project {

output_file = output_file.substr(0, output_file.find(".inja"));

Utils::verbose << "Output file: " << output_file << std::endl;

if (env->getProjectConfig() == nullptr) {
throw Lua::LuaException(
"Project config is null before templating file");
Expand Down
3 changes: 3 additions & 0 deletions src/Utils/FileFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace Frate::Utils {
}

bool FileFilter::is_valid(const std::filesystem::path &check_path) {
if(!check_path.has_filename()){
return false;
}
if (!std::filesystem::exists(check_path)) {
throw FileFilterException("Check path does not exist: " + check_path.string());
}
Expand Down

0 comments on commit f0e4b4e

Please sign in to comment.