Skip to content

Config command #57

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
Sep 15, 2022
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
1 change: 1 addition & 0 deletions cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cc_binary(
deps = [
"//cli/commands:codegen",
"//cli/commands:command",
"//cli/commands:config",
],
)

Expand Down
2 changes: 2 additions & 0 deletions cli/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "./commands/command.hh"
#include "./commands/codegen.hh"
#include "./commands/config.hh"

namespace fs = std::filesystem;

Expand All @@ -24,6 +25,7 @@ int main(int argc, char* argv[]) {

const std::unordered_map<std::string, command_fn_t> commands{
{"codegen", &ecsact::cli::detail::codegen_command},
{"config", &ecsact::cli::detail::config_command},
};

for(int i=1; argc > i; ++i) {
Expand Down
12 changes: 12 additions & 0 deletions cli/commands/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ cc_library(
"@ecsact_runtime//:meta",
],
)

cc_library(
name = "config",
srcs = ["config.cc"],
hdrs = ["config.hh"],
copts = copts,
deps = [
":command",
"//executable_path",
"@bazelregistry_docopt_cpp//:docopt",
],
)
124 changes: 124 additions & 0 deletions cli/commands/config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "./config.hh"

#include <iostream>
#include <filesystem>
#include <map>
#include <string>
#include "docopt.h"

#include "executable_path/executable_path.hh"

namespace fs = std::filesystem;

constexpr auto USAGE = R"(Ecsact Config Command

Usage:
ecsact config [--include_dir] [--plugin_dir] [--builtin_plugins]

Options:
--include_dir
Prints the include directory containing Ecsact headers.
--plugin_dir
Prints the directory containing all built-in Ecsact codegen plugins.
--builtin_plugins
Prints a list of built-in Ecsact codegen plugins available.
)";

constexpr auto CANNOT_FIND_INCLUDE_DIR = R"(
[ERROR] Cannot find Ecsact include directory.
Expected include directory next to bin directory. Make sure you're using a
standard Ecsact SDK installation.
If you believe this is a mistake please file an issue at
https://github.com/ecsact-dev/ecsact_sdk/issues
)";

constexpr auto CANNOT_FIND_PLUGIN_DIR = R"(
[ERROR] Cannot find Ecsact built-in plugin directory.
Make sure you're using a standard Ecsact SDK installation.
If you believe this is a mistake please file an issue at
https://github.com/ecsact-dev/ecsact_sdk/issues
)";

int ecsact::cli::detail::config_command(int argc, char* argv[]) {
using namespace std::string_literals;
using executable_path::executable_path;

auto args = docopt::docopt(USAGE, {argv + 1, argv + argc});
auto exec_path = executable_path();
if(exec_path.empty()) {
exec_path = fs::path{argv[0]};
std::error_code ec;
exec_path = fs::canonical(exec_path, ec);
if(ec) {
exec_path = fs::weakly_canonical(fs::path{argv[0]});
}
}

auto install_prefix = exec_path.parent_path().parent_path();
auto plugin_dir = install_prefix / "share" / "ecsact" / "plugins";
std::map<std::string, std::string> output;

if(args.at("--include_dir").asBool()) {
auto includedir = install_prefix / "include";
auto core_hdr = includedir / "ecsact" / "runtime" / "core.h";

if(fs::exists(core_hdr)) {
output["include_dir"] = includedir.string();
} else {
std::cerr << CANNOT_FIND_INCLUDE_DIR;
return 1;
}
}

if(args.at("--plugin_dir").asBool()) {
if(fs::exists(plugin_dir)) {
output["plugin_dir"] = plugin_dir.string();
} else {
std::cerr << CANNOT_FIND_PLUGIN_DIR;
return 1;
}
}

if(args.at("--builtin_plugins").asBool()) {
if(fs::exists(plugin_dir)) {
auto& builtin_plugins_str = output["builtin_plugins"];
std::vector<std::string> builtin_plugins;
for(auto& entry : fs::directory_iterator(plugin_dir)) {
auto filename = entry.path().filename().replace_extension("").string();
const auto prefix = "ecsact_"s;
const auto suffix = "_codegen"s;
if(filename.starts_with(prefix) && filename.ends_with(suffix)) {
builtin_plugins.emplace_back() = filename.substr(
prefix.size(),
filename.size() - prefix.size() - suffix.size()
);
}
}

if(!builtin_plugins.empty()) {
auto& builtin_plugins_str = output["builtin_plugins"];
for(auto i=0; builtin_plugins.size() - 1 > i; ++i) {
builtin_plugins_str += builtin_plugins[i] + "\n";
}
builtin_plugins_str += builtin_plugins.back();
}
} else {
std::cerr << CANNOT_FIND_PLUGIN_DIR;
return 1;
}
}

if(output.empty()) {
return 1;
}

if(output.size() == 1) {
std::cout << output.begin()->second << "\n";
} else {
for(auto&& [key, value] : output) {
std::cout << key << ": " << value << "\n";
}
}

return 0;
}
10 changes: 10 additions & 0 deletions cli/commands/config.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <type_traits>

#include "./command.hh"

namespace ecsact::cli::detail {
int config_command(int argc, char* argv[]);
static_assert(std::is_same_v<command_fn_t, decltype(&config_command)>);
}