229 lines
7.7 KiB
C++
229 lines
7.7 KiB
C++
module;
|
|
|
|
#include <CLI11.hpp>
|
|
|
|
module cargoxx.cli;
|
|
|
|
import std;
|
|
import cargoxx.util;
|
|
|
|
namespace cargoxx::cli {
|
|
|
|
auto run(int argc, char** argv) -> int {
|
|
CLI::App app{"cargoxx — a Cargo-style project tool for modern C++"};
|
|
app.require_subcommand(1);
|
|
|
|
auto* new_cmd = app.add_subcommand("new", "Create a new cargoxx project");
|
|
std::string new_name;
|
|
bool new_lib = false;
|
|
new_cmd->add_option("name", new_name, "Project name")->required();
|
|
new_cmd->add_flag("--lib", new_lib, "Create a library project");
|
|
|
|
auto* build_cmd = app.add_subcommand(
|
|
"build", "Generate flake.nix and build/CMakeLists.txt; build with nix+cmake");
|
|
bool build_no_build = false;
|
|
bool build_release = false;
|
|
std::string build_target;
|
|
build_cmd->add_flag("--no-build", build_no_build,
|
|
"Generate files only; do not invoke nix/cmake");
|
|
build_cmd->add_flag("--release", build_release, "Build the release profile");
|
|
build_cmd->add_option("--target", build_target,
|
|
"Build a specific target (passed to cmake --build)");
|
|
|
|
auto* run_cmd = app.add_subcommand("run", "Build and run a binary target");
|
|
bool run_release = false;
|
|
std::string run_bin;
|
|
std::vector<std::string> run_args;
|
|
run_cmd->add_flag("--release", run_release, "Run the release profile");
|
|
run_cmd->add_option("--bin", run_bin, "Binary target to run");
|
|
run_cmd->add_option("args", run_args, "Arguments passed to the binary")
|
|
->expected(0, -1);
|
|
|
|
auto* test_cmd = app.add_subcommand("test", "Build and run all test targets via ctest");
|
|
bool test_release = false;
|
|
test_cmd->add_flag("--release", test_release, "Test the release profile");
|
|
|
|
auto* clean_cmd = app.add_subcommand("clean", "Remove the build/ directory");
|
|
|
|
auto* add_cmd = app.add_subcommand(
|
|
"add", "Add a dependency to Cargoxx.toml (e.g. cargoxx add fmt@10.2)");
|
|
std::string add_spec;
|
|
std::string add_components;
|
|
add_cmd->add_option("spec", add_spec, "Package spec: <name>@<version>")->required();
|
|
add_cmd->add_option("--components", add_components,
|
|
"Comma-separated components (e.g. filesystem,system)");
|
|
|
|
auto* remove_cmd =
|
|
app.add_subcommand("remove", "Remove a dependency from Cargoxx.toml");
|
|
std::string remove_name;
|
|
remove_cmd->add_option("name", remove_name, "Package name to remove")->required();
|
|
|
|
auto* linkdb_cmd =
|
|
app.add_subcommand("linkdb", "Manage the link database");
|
|
linkdb_cmd->require_subcommand(1);
|
|
auto* linkdb_add_cmd =
|
|
linkdb_cmd->add_subcommand("add", "Insert a manual recipe");
|
|
std::string ldb_package;
|
|
std::string ldb_version = "*";
|
|
std::string ldb_find_package;
|
|
std::string ldb_targets;
|
|
std::string ldb_nixpkgs_attr;
|
|
linkdb_add_cmd->add_option("package", ldb_package, "Package name")->required();
|
|
linkdb_add_cmd->add_option("--version", ldb_version, "Version range (default: *)");
|
|
linkdb_add_cmd->add_option("--find-package", ldb_find_package,
|
|
"Body of the find_package(...) call (e.g. \"fmt CONFIG REQUIRED\")")
|
|
->required();
|
|
linkdb_add_cmd->add_option("--targets", ldb_targets,
|
|
"Comma-separated CMake targets (e.g. fmt::fmt)")
|
|
->required();
|
|
linkdb_add_cmd->add_option("--nixpkgs-attr", ldb_nixpkgs_attr,
|
|
"nixpkgs attribute name (e.g. fmt_10)")
|
|
->required();
|
|
|
|
try {
|
|
app.parse(argc, argv);
|
|
} catch (const CLI::ParseError& e) {
|
|
return app.exit(e);
|
|
}
|
|
|
|
std::error_code ec;
|
|
auto cwd = std::filesystem::current_path(ec);
|
|
if (ec) {
|
|
std::cerr << std::format("error: cannot determine current working directory: {}\n",
|
|
ec.message());
|
|
return 1;
|
|
}
|
|
|
|
if (*new_cmd) {
|
|
auto r = cmd_new(new_name, new_lib, cwd);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
std::cout << std::format(" Created `{}` project\n", new_name);
|
|
return 0;
|
|
}
|
|
|
|
if (*build_cmd) {
|
|
std::optional<std::string> target;
|
|
if (!build_target.empty()) {
|
|
target = build_target;
|
|
}
|
|
auto r = cmd_build(cwd, build_no_build, build_release, target);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
if (build_no_build) {
|
|
std::cout << " Generated build/flake.nix, build/CMakeLists.txt, Cargoxx.lock\n";
|
|
} else {
|
|
std::cout << " Built\n";
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
if (*run_cmd) {
|
|
std::optional<std::string> bin;
|
|
if (!run_bin.empty()) {
|
|
bin = run_bin;
|
|
}
|
|
auto r = cmd_run(cwd, run_release, bin, run_args);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
return *r;
|
|
}
|
|
|
|
if (*test_cmd) {
|
|
auto r = cmd_test(cwd, test_release);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
return *r;
|
|
}
|
|
|
|
if (*clean_cmd) {
|
|
auto r = cmd_clean(cwd);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
std::cout << " Cleaned build/\n";
|
|
return 0;
|
|
}
|
|
|
|
if (*add_cmd) {
|
|
std::string name = add_spec;
|
|
std::string version;
|
|
if (auto at = add_spec.find('@'); at != std::string::npos) {
|
|
name = add_spec.substr(0, at);
|
|
version = add_spec.substr(at + 1);
|
|
}
|
|
std::vector<std::string> components;
|
|
if (!add_components.empty()) {
|
|
std::size_t pos = 0;
|
|
while (pos <= add_components.size()) {
|
|
auto comma = add_components.find(',', pos);
|
|
auto piece = add_components.substr(
|
|
pos, comma == std::string::npos ? add_components.size() - pos
|
|
: comma - pos);
|
|
if (!piece.empty()) {
|
|
components.push_back(std::move(piece));
|
|
}
|
|
if (comma == std::string::npos) {
|
|
break;
|
|
}
|
|
pos = comma + 1;
|
|
}
|
|
}
|
|
auto r = cmd_add(cwd, name, version, std::move(components));
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
std::cout << std::format(" Added {} {}\n", name, version);
|
|
return 0;
|
|
}
|
|
|
|
if (*remove_cmd) {
|
|
auto r = cmd_remove(cwd, remove_name);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
std::cout << std::format(" Removed {}\n", remove_name);
|
|
return 0;
|
|
}
|
|
|
|
if (*linkdb_add_cmd) {
|
|
std::vector<std::string> targets;
|
|
std::size_t pos = 0;
|
|
while (pos <= ldb_targets.size()) {
|
|
auto comma = ldb_targets.find(',', pos);
|
|
auto piece = ldb_targets.substr(
|
|
pos, comma == std::string::npos ? ldb_targets.size() - pos : comma - pos);
|
|
if (!piece.empty()) {
|
|
targets.push_back(std::move(piece));
|
|
}
|
|
if (comma == std::string::npos) {
|
|
break;
|
|
}
|
|
pos = comma + 1;
|
|
}
|
|
auto r = cmd_linkdb_add(ldb_package, ldb_version, ldb_find_package,
|
|
std::move(targets), ldb_nixpkgs_attr);
|
|
if (!r) {
|
|
std::cerr << util::format(r.error());
|
|
return 1;
|
|
}
|
|
std::cout << std::format(" Added manual recipe for {}\n", ldb_package);
|
|
return 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
} // namespace cargoxx::cli
|