Add build log to templater

This commit is contained in:
Jan 2022-09-06 00:36:28 +02:00
parent 6a45031c96
commit 379aabebd6
6 changed files with 58 additions and 12 deletions

View File

@ -13,9 +13,12 @@ class RawTemplater::Impl
{
RawTemplaterArguments m_args;
_NODISCARD bool GenerateCode(const std::string& filename) const
bool m_write_build_log;
std::ofstream m_build_log_file;
_NODISCARD bool GenerateCode(const std::string& filename)
{
std::ifstream file(filename);
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (!file.is_open())
{
std::cerr << "Failed to open file \"" << filename << "\"\n";
@ -23,6 +26,8 @@ class RawTemplater::Impl
}
templating::Templater templater(file, filename);
if (m_write_build_log)
templater.SetBuildLogFile(&m_build_log_file);
if (!m_args.m_output_directory.empty())
return templater.TemplateToDirectory(m_args.m_output_directory);
@ -34,13 +39,26 @@ class RawTemplater::Impl
public:
Impl()
= default;
: m_write_build_log(false)
{
}
int Run(const int argc, const char** argv)
{
if (!m_args.Parse(argc, argv))
return 1;
if (!m_args.m_build_log_file.empty())
{
m_build_log_file = std::ofstream(m_args.m_build_log_file, std::ios::out | std::ios::binary);
if (!m_build_log_file.is_open())
{
std::cerr << "Failed to open build log file \"" << m_args.m_build_log_file << "\"\n";
return false;
}
m_write_build_log = true;
}
for (const auto& inputFile : m_args.m_input_files)
{
if (!GenerateCode(inputFile))

View File

@ -22,6 +22,12 @@ const CommandLineOption* const OPTION_OUTPUT_FOLDER = CommandLineOption::Builder
.WithParameter("outputPath")
.Build();
const CommandLineOption* const OPTION_BUILD_LOG = CommandLineOption::Builder::Create()
.WithLongName("build-log")
.WithDescription("Specify a file to write a build log to.")
.WithParameter("logFilePath")
.Build();
const CommandLineOption* const OPTION_DEFINE = CommandLineOption::Builder::Create()
.WithShortName("d")
.WithLongName("define")
@ -35,6 +41,7 @@ const CommandLineOption* const COMMAND_LINE_OPTIONS[]
OPTION_HELP,
OPTION_VERBOSE,
OPTION_OUTPUT_FOLDER,
OPTION_BUILD_LOG,
OPTION_DEFINE
};
@ -87,6 +94,10 @@ bool RawTemplaterArguments::Parse(const int argc, const char** argv)
else
m_output_directory = ".";
// --build-log
if (m_argument_parser.IsOptionSpecified(OPTION_BUILD_LOG))
m_build_log_file = m_argument_parser.GetValueForOption(OPTION_BUILD_LOG);
// -d; --define
if (m_argument_parser.IsOptionSpecified(OPTION_DEFINE))
{

View File

@ -21,6 +21,8 @@ public:
std::vector<std::string> m_input_files;
std::string m_output_directory;
std::string m_build_log_file;
std::vector<std::pair<std::string, std::string>> m_defines;
RawTemplaterArguments();

View File

@ -172,7 +172,7 @@ namespace templating
m_default_output_file = (m_output_directory / filenamePath.replace_extension()).string();
}
bool RunNextPass()
bool RunNextPass(std::ostream* buildLogFile)
{
m_stream.clear();
m_stream.seekg(0, std::ios::beg);
@ -209,7 +209,7 @@ namespace templating
if (!m_write_output_to_file)
{
m_output_stream = std::ofstream(m_output_file);
m_output_stream = std::ofstream(m_output_file, std::ios::out | std::ios::binary);
if (!m_output_stream.is_open())
{
std::cerr << "Failed to open output file \"" << m_output_file << "\"\n";
@ -223,6 +223,9 @@ namespace templating
std::cout << "Templated file \"" << m_output_file << "\"\n";
if(buildLogFile)
*buildLogFile << "Templated file \"" << m_output_file << "\"\n";
m_first_line = true;
m_write_output_to_file = false;
m_output_cache.clear();
@ -289,7 +292,7 @@ namespace templating
return false;
m_output_file = (m_output_directory / fileName).string();
m_output_stream = std::ofstream(m_output_file);
m_output_stream = std::ofstream(m_output_file, std::ios::out | std::ios::binary);
if (!m_output_stream.is_open())
{
std::cerr << "Failed to open output file \"" << m_output_file << "\"\n";
@ -326,23 +329,29 @@ namespace templating
Templater::Templater(std::istream& stream, std::string fileName)
: m_stream(stream),
m_build_log(nullptr),
m_file_name(std::move(fileName))
{
}
void Templater::SetBuildLogFile(std::ostream* buildLogFile)
{
m_build_log = buildLogFile;
}
bool Templater::TemplateToDirectory(const std::string& outputDirectory) const
{
TemplaterControlImpl control(m_stream, m_file_name, outputDirectory);
try
{
if (!control.RunNextPass())
if (!control.RunNextPass(m_build_log))
return false;
control.AdvanceActiveVariations();
while (control.HasActiveVariations())
{
if (!control.RunNextPass())
if (!control.RunNextPass(m_build_log))
return false;
control.AdvanceActiveVariations();

View File

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include "Utils/ClassUtils.h"
#include "Parsing/IParserLineStream.h"
namespace templating
@ -10,10 +11,12 @@ namespace templating
public:
Templater(std::istream& stream, std::string fileName);
bool TemplateToDirectory(const std::string& outputDirectory) const;
void SetBuildLogFile(std::ostream* buildLogFile);
_NODISCARD bool TemplateToDirectory(const std::string& outputDirectory) const;
private:
std::istream& m_stream;
std::ostream* m_build_log;
std::string m_file_name;
};
}

View File

@ -39,6 +39,8 @@ function Raw:project()
}
}
RawTemplater:use()
filter "files:not **/*.template"
buildmessage 'Copying rawfile %{file.relpath}'
buildcommands {
@ -59,10 +61,11 @@ function Raw:project()
buildcommands {
'"' .. TargetDirectoryBin .. '/' .. ExecutableByOs('RawTemplater') .. '"'
.. " -o %{cfg.targetdir}/build/raw/%{file.reldirectory}"
.. " --build-log \"%{prj.location}/build/%{file.relpath}.log\""
.. " %{file.relpath}"
}
buildoutputs {
"%{cfg.targetdir}/build/raw/%{file.relpath}"
"%{prj.location}/build/%{file.relpath}.log"
}
filter {}
end