Skip to content

Commit

Permalink
fix: output file issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Aug 9, 2024
1 parent 80e5599 commit ee72f38
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 56 deletions.
36 changes: 18 additions & 18 deletions ecsact/cli/commands/codegen/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ auto ecsact::cli::codegen(codegen_options options) -> int {
);
}

for(auto& output_file_path : plugin_output_paths) {
if(options.outdir) {
output_file_path = *options.outdir / output_file_path;
} else {
output_file_path = output_file_path;
}
}

auto has_plugin_output_conflict_error = false;
for(auto filename_index = 0; plugin_output_paths.size() > filename_index;
++filename_index) {
Expand Down Expand Up @@ -254,26 +262,18 @@ auto ecsact::cli::codegen(codegen_options options) -> int {
fs::permissions(output_file_path, fs::perms::all);
}

if(options.outdir) {
output_file_path = *options.outdir / output_file_path;
report_info("{}", fs::absolute(output_file_path).string());
} else {
output_file_path = output_file_path;
}
auto& file_write_stream = file_write_streams.emplace_back();
file_write_stream.open(output_file_path);
}

auto& file_write_stream =
file_write_streams.emplace_back(output_file_path);
plugin_fn(package_id, &file_write_fn, &codegen_report_fn);
if(received_fatal_codegen_report) {
received_fatal_codegen_report = false;
has_plugin_error = true;
report_error("Codegen plugin '{}' reported fatal error", plugin_name);
}

file_write_stream.open(output_file_path);
plugin_fn(package_id, &file_write_fn, &codegen_report_fn);
if(received_fatal_codegen_report) {
received_fatal_codegen_report = false;
has_plugin_error = true;
report_error(
"Codegen plugin '{}' reported fatal error",
plugin_name
);
}
for(auto& file_write_stream : file_write_streams) {
file_write_stream.flush();
file_write_stream.close();
}
Expand Down
53 changes: 40 additions & 13 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ exports_files([
"test.ecsact",
])

cc_binary(
name = "test_codegen_plugin.so",
_TEST_PLUGINS = [
"test_codegen_plugin",
"test_codegen_plugin_multi_output",
]

[cc_binary(
name = "{}.so".format(plugin),
linkshared = True,
copts = copts,
tags = ["manual"],
srcs = [
"test_codegen_plugin.cc",
"{}.cc".format(plugin),
"@ecsact_runtime//dylib:dylib.cc",
],
deps = [
Expand All @@ -23,15 +28,15 @@ cc_binary(
"@ecsact_runtime//dylib:util",
],
defines = ["ECSACT_META_API_LOAD_AT_RUNTIME"],
)
) for plugin in _TEST_PLUGINS]

cc_binary(
name = "test_codegen_plugin.dll",
[cc_binary(
name = "{}.dll".format(plugin),
linkshared = True,
copts = copts,
tags = ["manual"],
srcs = [
"test_codegen_plugin.cc",
"{}.cc".format(plugin),
"@ecsact_runtime//dylib:dylib.cc",
],
deps = [
Expand All @@ -41,15 +46,15 @@ cc_binary(
"@ecsact_runtime//dylib:util",
],
defines = ["ECSACT_META_API_LOAD_AT_RUNTIME"],
)
) for plugin in _TEST_PLUGINS]

alias(
name = "test_codegen_plugin",
[alias(
name = plugin,
actual = select({
"@platforms//os:windows": "test_codegen_plugin.dll",
"@platforms//os:linux": "test_codegen_plugin.so",
"@platforms//os:windows": "{}.dll".format(plugin),
"@platforms//os:linux": "{}.so".format(plugin),
}),
)
) for plugin in _TEST_PLUGINS]

cc_test(
name = "test_codegen",
Expand All @@ -72,3 +77,25 @@ cc_test(
"@googletest//:gtest_main",
],
)

cc_test(
name = "test_codegen_multi_output",
copts = copts,
srcs = ["test_codegen_multi_output.cc"],
data = [
"@ecsact_cli",
"test.ecsact",
":test_codegen_plugin_multi_output",
],
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",
"@ecsact_cli//ecsact/cli/commands:codegen",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)
4 changes: 3 additions & 1 deletion test/test_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ TEST(Codegen, Success) {
std::istreambuf_iterator<char>(),
};

ASSERT_EQ(generated_file_contents, "this is just a test, breathe!");
ASSERT_EQ(generated_file_contents, "this is just a test, breathe!")
<< "Unexpected content at "
<< fs::absolute(generated_file_path).generic_string();
}
90 changes: 90 additions & 0 deletions test/test_codegen_multi_output.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <format>
#include <filesystem>
#include <fstream>
#include "ecsact/cli/commands/codegen.hh"
#include "tools/cpp/runfiles/runfiles.h"

using bazel::tools::cpp::runfiles::Runfiles;
using ecsact::cli::detail::codegen_command;

using namespace std::string_literals;
namespace fs = std::filesystem;

TEST(Codegen, Success) {
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;

auto generated_txt_file_path = fs::path{"_test_codegen_outdir/test.txt"};
auto generated_zomsky_file_path =
fs::path{"_test_codegen_outdir/test.zomsky"};

if(fs::exists(generated_txt_file_path)) {
fs::remove(generated_txt_file_path);
}

if(fs::exists(generated_zomsky_file_path)) {
fs::remove(generated_zomsky_file_path);
}

ASSERT_TRUE(fs::exists(test_codegen_plugin_path))
<< "Cannot find test plugin: "
<< fs::absolute(test_codegen_plugin_path).string();
ASSERT_TRUE(fs::exists(test_ecsact_file_path))
<< "Cannot find test ecsact file: "
<< fs::absolute(test_ecsact_file_path).string();

#if _WIN32
// TODO: this doesn't work on linux
auto exit_code = codegen_command(std::vector{
"ecsact"s,
"codegen"s,
std::string{test_ecsact_file_path},
std::format("--plugin={}", test_codegen_plugin_path),
"--outdir=_test_codegen_outdir"s,
});
#else
auto cmd = std::format(
"{} codegen {} --plugin={} --outdir=_test_codegen_outdir",
test_ecsact_cli,
std::string{test_ecsact_file_path},
test_codegen_plugin_path,
"_test_codegen_outdir"
);
auto exit_code = std::system(cmd.c_str());
#endif

ASSERT_EQ(exit_code, 0);

ASSERT_TRUE(fs::exists(generated_txt_file_path));
auto generated_txt_file = std::ifstream{generated_txt_file_path};
auto generated_txt_file_contents = std::string{
std::istreambuf_iterator<char>(generated_txt_file),
std::istreambuf_iterator<char>(),
};

ASSERT_EQ(generated_txt_file_contents, "throw this in the text file")
<< "Unexpected content at "
<< fs::absolute(generated_txt_file_path).generic_string();

ASSERT_TRUE(fs::exists(generated_zomsky_file_path));
auto generated_zomsky_file = std::ifstream{generated_zomsky_file_path};
auto generated_zomsky_file_contents = std::string{
std::istreambuf_iterator<char>(generated_zomsky_file),
std::istreambuf_iterator<char>(),
};

ASSERT_EQ(generated_zomsky_file_contents, "throw this in the zomsky file")
<< "Unexpected content at "
<< fs::absolute(generated_zomsky_file_path).generic_string();
}
26 changes: 2 additions & 24 deletions test/test_codegen_plugin.cc
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
#include <string_view>
#include <array>
#include "ecsact/codegen/plugin.hh"
#include "ecsact/runtime/meta.hh"
#include "ecsact/runtime/dylib.h"
#include "ecsact/codegen/plugin.h"

using namespace std::string_view_literals;

auto ecsact_codegen_output_filenames( //
ecsact_package_id package_id,
char* const* out_filenames,
int32_t max_filenames,
int32_t max_filename_length,
int32_t* out_filenames_length
) -> void {
auto pkg_filename = //
ecsact::meta::package_file_path(package_id).filename().string();
auto filenames = std::array{pkg_filename + ".txt"};
ecsact::set_codegen_plugin_output_filenames(
filenames,
out_filenames,
max_filenames,
max_filename_length,
out_filenames_length
);
}

auto ecsact_codegen_plugin_name() -> const char* {
return "Example Codegen";
return "txt";
}

auto ecsact_codegen_plugin( //
Expand Down
46 changes: 46 additions & 0 deletions test/test_codegen_plugin_multi_output.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <string_view>
#include <array>
#include "ecsact/codegen/plugin.hh"
#include "ecsact/runtime/meta.hh"

using namespace std::string_view_literals;

auto ecsact_codegen_output_filenames( //
ecsact_package_id package_id,
char* const* out_filenames,
int32_t max_filenames,
int32_t max_filename_length,
int32_t* out_filenames_length
) -> void {
auto pkg_basename = //
ecsact::meta::package_file_path(package_id)
.filename()
.replace_extension("")
.string();
ecsact::set_codegen_plugin_output_filenames(
std::array{
pkg_basename + ".txt",
pkg_basename + ".zomsky",
},
out_filenames,
max_filenames,
max_filename_length,
out_filenames_length
);
}

auto ecsact_codegen_plugin_name() -> const char* {
return "Example Codegen";
}

auto ecsact_codegen_plugin( //
ecsact_package_id package_id,
ecsact_codegen_write_fn_t write_fn,
ecsact_codegen_report_fn_t report_fn
) -> void {
auto test_message = "throw this in the text file"sv;
write_fn(0, test_message.data(), test_message.size());

test_message = "throw this in the zomsky file"sv;
write_fn(1, test_message.data(), test_message.size());
}

0 comments on commit ee72f38

Please sign in to comment.