Skip to content

Commit

Permalink
feat: implement RTBs reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Oct 2, 2023
1 parent c06d639 commit a7e78ea
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 8 deletions.
43 changes: 42 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("@rules_ecsact//ecsact:toolchain.bzl", "ecsact_toolchain")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//bazel/tools:cc_stamp_header.bzl", "cc_stamp_header")
Expand All @@ -15,6 +15,47 @@ cc_stamp_header(
out = "bazel_stamp_header.hh",
)

cc_library(
name = "cli_json_report",
visibility = ["//:__subpackages__"],
copts = copts,
hdrs = ["ecsact/cli/detail/json_report.hh"],
srcs = ["ecsact/cli/detail/json_report.cc"],
deps = [
":cli_report_message",
"@nlohmann_json//:json",
],
)

cc_library(
name = "cli_text_report",
visibility = ["//:__subpackages__"],
copts = copts,
hdrs = ["ecsact/cli/detail/text_report.hh"],
srcs = ["ecsact/cli/detail/text_report.cc"],
deps = [
":cli_report_message",
],
)

cc_library(
name = "cli_report_message",
visibility = ["//:__subpackages__"],
copts = copts,
hdrs = ["ecsact/cli/report_message.hh"],
)

cc_library(
name = "cli_report",
visibility = ["//:__subpackages__"],
copts = copts,
hdrs = ["ecsact/cli/report.hh"],
srcs = ["ecsact/cli/report.cc"],
deps = [
":cli_report_message",
],
)

cc_binary(
name = "ecsact",
srcs = [
Expand Down
1 change: 0 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ bazel_dep(name = "magic_enum", version = "0.9.3")
bazel_dep(name = "docopt.cpp", version = "0.6.2")
bazel_dep(name = "rules_ecsact", version = "0.4.2")
bazel_dep(name = "yaml-cpp")

git_override(
module_name = "yaml-cpp",
commit = "37f1b8b8c9e5172ff3a79a3d5fdbb87f2994842b",
Expand Down
4 changes: 4 additions & 0 deletions commands/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ cc_library(
copts = copts,
deps = [
":command",
"//:cli_report",
"//:cli_report_message",
"//:cli_json_report",
"//:cli_text_report",
"//executable_path",
"@docopt.cpp//:docopt",
"@yaml-cpp",
Expand Down
33 changes: 27 additions & 6 deletions commands/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <format>
#include "docopt.h"
#include "magic_enum.hpp"
#include "ecsact/cli/report.hh"
#include "ecsact/cli/detail/json_report.hh"
#include "ecsact/cli/detail/text_report.hh"

#include "commands/build/build_recipe.hh"
#include "commands/build/cc_compiler.hh"
Expand All @@ -16,14 +19,19 @@ namespace fs = std::filesystem;
constexpr auto USAGE = R"docopt(Ecsact Build Command
Usage:
ecsact build (-h | --help)
ecsact build <files>... --recipe=<name> --output=<path> [--temp_dir=<path>]
ecsact build (-h | --help)
ecsact build <files>... --recipe=<name> --output=<path> [--format=<type>] [--temp_dir=<path>]
Options:
<files> Ecsact files used to build Ecsact Runtime
-r --recipe=<name> Name or path to recipe
-o --output=<path> Runtime output path
--temp_dir Optional temporary directoy to use instead of generated one
<files> Ecsact files used to build Ecsact Runtime
-r --recipe=<name> Name or path to recipe
-o --output=<path> Runtime output path
--temp_dir=<path> Optional temporary directoy to use instead of generated one
-f --format=<type> The format used to report progress of the build [default: text]
Allowed Formats:
none - No output
json - Each line of stdout/stderr is a JSON object
text - Human readable text format
)docopt";

auto handle_source( //
Expand Down Expand Up @@ -99,6 +107,19 @@ auto ecsact::cli::detail::build_command( //
char* argv[]
) -> int {
auto args = docopt::docopt(USAGE, {argv + 1, argv + argc});
auto format = args["--format"].asString();

if(format == "text") {
set_report_handler(text_report{});
} else if(format == "json") {
set_report_handler(json_report{});
} else if(format == "none") {
set_report_handler({});
} else {
std::cerr << "Invalid --format value: " << format << "\n";
std::cout << USAGE;
return 1;
}

auto files = args.at("<files>").asStringList();
auto file_paths = std::vector<fs::path>{};
Expand Down
36 changes: 36 additions & 0 deletions ecsact/cli/detail/json_report.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "ecsact/cli/detail/json_report.hh"

#include <iostream>
#include <nlohmann/json.hpp>

namespace ecsact::cli {
// clang-format off
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(alert_message, content)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(info_message, content)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(error_message, content)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ecsact_error_message, ecsact_source_path, message, line, character)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(warning_message, content)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(success_message, content)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(module_methods_message::method_info, method_name, available)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(module_methods_message, module_name, methods)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(subcommand_start_message, id, executable, arguments)
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)
// clang-format on
} // namespace ecsact::cli

template<typename MessageT>
void print_json_report(const MessageT& message) {
auto message_json = "{}"_json;
to_json(message_json, message);
message_json["type"] = MessageT::type;
std::cout << message_json.dump() + "\n";
}

auto ecsact::cli::detail::json_report::operator()( //
const message_variant_t& message
) const -> void {
std::visit([](const auto& message) { print_json_report(message); }, message);
}
9 changes: 9 additions & 0 deletions ecsact/cli/detail/json_report.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "ecsact/cli/report_message.hh"

namespace ecsact::cli::detail {
struct json_report {
auto operator()(const message_variant_t&) const -> void;
};
}
122 changes: 122 additions & 0 deletions ecsact/cli/detail/text_report.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "ecsact/cli/detail/text_report.hh"

#include <iostream>
#include <format>

using ecsact::cli::alert_message;
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::subcommand_end_message;
using ecsact::cli::subcommand_progress_message;
using ecsact::cli::subcommand_start_message;
using ecsact::cli::subcommand_stderr_message;
using ecsact::cli::subcommand_stdout_message;
using ecsact::cli::success_message;
using ecsact::cli::warning_message;

namespace {
auto print_text_report(const alert_message& msg) -> void {
std::cout << std::format( //
"[ALERT] {}\n",
msg.content
);
}

auto print_text_report(const info_message& msg) -> void {
std::cout << std::format( //
"[INFO] {}\n",
msg.content
);
}

auto print_text_report(const error_message& msg) -> void {
std::cout << std::format( //
"[ERROR] {}\n",
msg.content
);
}

auto print_text_report(const ecsact_error_message& msg) -> void {
std::cerr << std::format( //
"[ERROR] {}:{}:{}\n"
" {}\n",
msg.ecsact_source_path,
msg.line,
msg.character,
msg.message
);
}

auto print_text_report(const warning_message& msg) -> void {
std::cout << std::format( //
"[WARNING] {}\n",
msg.content
);
}

auto print_text_report(const success_message& msg) -> void {
std::cout << std::format( //
"[SUCCESS] {}\n",
msg.content
);
}

auto print_text_report(const module_methods_message& msg) -> void {
std::cout << "[Module Methods for " << msg.module_name << "]\n";
for(auto& method : msg.methods) {
std::cout //
<< " " << (method.available ? "YES " : " NO ") << method.method_name
<< "\n";
}
}

auto print_text_report(const subcommand_start_message& msg) -> void {
std::cout //
<< "[SUBCOMMAND START id=(" << std::to_string(msg.id) << ")] "
<< msg.executable << " ";
for(auto& arg : msg.arguments) {
std::cout << arg << " ";
}
std::cout << "\n";
}

auto print_text_report(const subcommand_stdout_message& msg) -> void {
std::cout << std::format( //
"[SUBCOMMAND STDOUT id=({})] {}\n",
msg.id,
msg.line
);
}

auto print_text_report(const subcommand_stderr_message& msg) -> void {
std::cout << std::format( //
"[SUBCOMMAND STDERR id=({})] {}\n",
msg.id,
msg.line
);
}

auto print_text_report(const subcommand_progress_message& msg) -> void {
std::cout << std::format( //
"[SUBCOMMAND PROG id=({})] {}\n",
msg.id,
msg.description
);
}

auto print_text_report(const subcommand_end_message& msg) -> void {
std::cout << std::format( //
"[SUBCOMMAND END id=({})] exit code {}\n",
msg.id,
msg.exit_code
);
}
} // namespace

auto ecsact::cli::detail::text_report::operator()( //
const message_variant_t& message
) const -> void {
std::visit([](const auto& message) { print_text_report(message); }, message);
}
9 changes: 9 additions & 0 deletions ecsact/cli/detail/text_report.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "ecsact/cli/report_message.hh"

namespace ecsact::cli::detail {
struct text_report {
auto operator()(const message_variant_t&) const -> void;
};
}
18 changes: 18 additions & 0 deletions ecsact/cli/report.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "ecsact/cli/report.hh"

static auto _report_handler =
std::function<void(const ecsact::cli::message_variant_t&)>{};

auto ecsact::cli::report( //
const message_variant_t& message
) -> void {
if(_report_handler) {
_report_handler(message);
}
}

auto ecsact::cli::set_report_handler( //
std::function<void(const message_variant_t&)> handler
) -> void {
_report_handler = handler;
}
15 changes: 15 additions & 0 deletions ecsact/cli/report.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "ecsact/cli/report_message.hh"

namespace ecsact::cli {

auto report(const message_variant_t& message) -> void;
auto set_report_handler(std::function<void(const message_variant_t&)>) -> void;

template<typename StringLike>
auto report_error(StringLike message) -> void {
report(error_message{.content{message}});
}

} // namespace ecsact::cli
Loading

0 comments on commit a7e78ea

Please sign in to comment.