Skip to content

feat: new codegen flag for printing output paths #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ecsact/cli/commands/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ constexpr auto USAGE = R"(Ecsact Codegen Command

Usage:
ecsact codegen <files>... --plugin=<plugin> [--stdout]
ecsact codegen <files>... --plugin=<plugin>... [--outdir=<directory>] [--format=<type>] [--report_filter=<filter>]
ecsact codegen <files>... --plugin=<plugin>... [--outdir=<directory>] [--format=<type>] [--report_filter=<filter>] [--print-output-files]

Options:
-p, --plugin=<plugin> Name of bundled plugin or path to plugin.
--stdout Print to stdout instead of writing to a file. May only be used if a single ecsact file and single ecsact codegen plugin are used.
-o, --outdir=<directory> Specify directory generated files should be written to. By default generated files are written next to source files.
-f --format=<type> The format used to report progress of the build [default: text]
--report_filter=<filter> Filtering out report logs [default: none]
--print-output-files Simply print output file paths to stdout. No codegen will occur.
)";

static auto stdout_write_fn(
Expand All @@ -54,6 +55,8 @@ int ecsact::cli::detail::codegen_command(int argc, const char* argv[]) {
return exit_code;
}

auto only_print_output_files = args.at("--print-output-files").asBool();

auto files_error = false;
auto files_str = args.at("<files>").asStringList();
auto files = std::vector<fs::path>{};
Expand Down Expand Up @@ -158,6 +161,7 @@ int ecsact::cli::detail::codegen_command(int argc, const char* argv[]) {
auto codegen_options = ecsact::cli::codegen_options{
.plugin_paths = plugin_paths,
.outdir = outdir,
.only_print_output_files = only_print_output_files,
};

if(args.at("--stdout").asBool()) {
Expand Down
12 changes: 10 additions & 2 deletions ecsact/cli/commands/codegen/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ auto ecsact::cli::codegen(codegen_options options) -> int {
// We're filling this in the for loop. We shouldn't have any in here.
assert(file_write_streams.empty());

if(options.only_print_output_files) {
for(auto& output_file_path : plugin_output_paths) {
report(output_path_message{
fs::weakly_canonical(output_file_path).generic_string()
});
}
plugin.unload();
continue;
}

if(options.write_fn) {
if(plugin_output_paths.size() > 1) {
// TODO: this error can be misleading if a non-stdout custom
Expand All @@ -266,8 +276,6 @@ auto ecsact::cli::codegen(codegen_options options) -> int {

plugin_fn(package_id, *options.write_fn, &codegen_report_fn);
} else {
auto write_fn = options.write_fn.value_or(&file_write_fn);

for(auto filename_index = 0;
plugin_output_paths.size() > filename_index;
++filename_index) {
Expand Down
1 change: 1 addition & 0 deletions ecsact/cli/commands/codegen/codegen.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct codegen_options {
std::vector<std::filesystem::path> plugin_paths;
std::optional<std::filesystem::path> outdir;
std::optional<ecsact_codegen_write_fn_t> write_fn;
bool only_print_output_files;
};

auto codegen(codegen_options options) -> int;
Expand Down
1 change: 1 addition & 0 deletions ecsact/cli/detail/json_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(subcommand_stdout_message, id, line)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(subcommand_stderr_message, id, line)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(subcommand_progress_message, id, description)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(subcommand_end_message, id, exit_code)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(output_path_message, output_path)
// clang-format on
} // namespace ecsact::cli

Expand Down
5 changes: 5 additions & 0 deletions ecsact/cli/detail/text_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using ecsact::cli::ecsact_error_message;
using ecsact::cli::error_message;
using ecsact::cli::info_message;
using ecsact::cli::module_methods_message;
using ecsact::cli::output_path_message;
using ecsact::cli::subcommand_end_message;
using ecsact::cli::subcommand_progress_message;
using ecsact::cli::subcommand_start_message;
Expand Down Expand Up @@ -151,6 +152,10 @@ auto print_text_report(auto&& output, const subcommand_end_message& msg)
msg.exit_code
);
}

auto print_text_report(auto&& output, const output_path_message& msg) -> void {
get_outputstream(output, std::cout) << msg.output_path << "\n";
}
} // namespace

auto ecsact::cli::detail::text_report::operator()( //
Expand Down
8 changes: 7 additions & 1 deletion ecsact/cli/report_message.hh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ struct subcommand_end_message {
int exit_code;
};

struct output_path_message {
static constexpr auto type = std::string_view{"output_path"};
std::string output_path;
};

using message_variant_t = std::variant<
alert_message,
info_message,
Expand All @@ -113,5 +118,6 @@ using message_variant_t = std::variant<
subcommand_stdout_message,
subcommand_stderr_message,
subcommand_progress_message,
subcommand_end_message>;
subcommand_end_message,
output_path_message>;
} // namespace ecsact::cli
23 changes: 23 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ cc_test(
],
)

cc_test(
name = "test_codegen_print_outputs",
srcs = ["test_codegen_print_outputs.cc"],
copts = copts,
data = [
"test.ecsact",
":test_codegen_plugin",
":test_codegen_plugin_multi_output",
"@ecsact_cli",
],
env = {
"TEST_ECSACT_CLI": "$(rootpath @ecsact_cli)",
"TEST_ECSACT_FILE_PATH": "$(rootpath test.ecsact)",
"TEST_CODEGEN_PLUGIN_PATH": "$(rootpath :test_codegen_plugin_multi_output)",
},
deps = [
"@bazel_tools//tools/cpp/runfiles",
"@boost.process",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)

cc_test(
name = "test_codegen_stdout",
srcs = ["test_codegen_stdout.cc"],
Expand Down
73 changes: 73 additions & 0 deletions test/test_codegen_print_outputs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <format>
#include <ranges>
#include <filesystem>
#include <fstream>
#include <boost/process.hpp>
#include "tools/cpp/runfiles/runfiles.h"

using bazel::tools::cpp::runfiles::Runfiles;
using namespace std::string_literals;
namespace fs = std::filesystem;
namespace bp = boost::process;

TEST(Codegen, Stdout) {
auto runfiles_err = std::string{};
auto runfiles = Runfiles::CreateForTest(&runfiles_err);
auto test_ecsact_cli = std::getenv("TEST_ECSACT_CLI");
auto test_codegen_plugin_path = std::getenv("TEST_CODEGEN_PLUGIN_PATH");
auto test_ecsact_file_path = std::getenv("TEST_ECSACT_FILE_PATH");

ASSERT_NE(test_ecsact_cli, nullptr);
ASSERT_NE(test_codegen_plugin_path, nullptr);
ASSERT_NE(test_ecsact_file_path, nullptr);
ASSERT_NE(runfiles, nullptr) << runfiles_err;
ASSERT_TRUE(fs::exists(test_codegen_plugin_path));
ASSERT_TRUE(fs::exists(test_ecsact_file_path));

// this file should NOT be generated
auto bad_generated_file_path = fs::path{"test.txt"s};
if(fs::exists(bad_generated_file_path)) {
fs::remove(bad_generated_file_path);
}

auto proc_stdout = bp::ipstream{};
auto proc = bp::child{
bp::exe(test_ecsact_cli),
bp::args({
"codegen"s,
std::string{test_ecsact_file_path},
std::format("--plugin={}", test_codegen_plugin_path),
"--print-output-files"s,
}),
bp::std_out > proc_stdout
};

proc.wait();

auto exit_code = proc.exit_code();
ASSERT_EQ(exit_code, 0);

auto output_files = std::vector<std::string>{};
auto line = std::string{};
while(std::getline(proc_stdout, line)) {
if(line.ends_with("\r")) {
line.pop_back();
}
output_files.emplace_back(line);
}

EXPECT_EQ(output_files.size(), 2);

EXPECT_FALSE(
std::ranges::find(output_files, "test.txt") == output_files.end()
);
EXPECT_FALSE(
std::ranges::find(output_files, "test.zomsky") == output_files.end()
);
EXPECT_FALSE(fs::exists(bad_generated_file_path))
<< bad_generated_file_path.string()
<< " was generated even with --print-output-files option!";
}