[M4] add cargoxx run/test/clean

This commit is contained in:
2026-05-10 00:12:25 +00:00
parent f6e8699a72
commit 0a398d1c31
10 changed files with 349 additions and 0 deletions

View File

@@ -23,6 +23,23 @@ auto cmd_build(const std::filesystem::path& project_root, bool no_build, bool re
std::optional<std::filesystem::path> overlay_path = std::nullopt)
-> util::Result<void>;
// Builds the project, picks a binary target, and execs it with `args`.
// `bin_name` is required when the project declares more than one binary.
// Returns the binary's exit code, or an Error if selection or build fails.
auto cmd_run(const std::filesystem::path& project_root, bool release,
std::optional<std::string> bin_name, const std::vector<std::string>& args,
std::optional<std::filesystem::path> overlay_path = std::nullopt)
-> util::Result<int>;
// Builds the project then runs `ctest --output-on-failure` inside the
// profile-specific binary directory. Returns ctest's exit code.
auto cmd_test(const std::filesystem::path& project_root, bool release,
std::optional<std::filesystem::path> overlay_path = std::nullopt)
-> util::Result<int>;
// Removes <project_root>/build/. Leaves Cargoxx.lock and flake.lock alone.
auto cmd_clean(const std::filesystem::path& project_root) -> util::Result<void>;
auto run(int argc, char** argv) -> int;
} // namespace cargoxx::cli

27
src/cli/cmd_clean.cpp Normal file
View File

@@ -0,0 +1,27 @@
module cargoxx.cli;
import std;
import cargoxx.util;
namespace cargoxx::cli {
namespace fs = std::filesystem;
auto cmd_clean(const fs::path& project_root) -> util::Result<void> {
const auto build_dir = project_root / "build";
std::error_code ec;
if (!fs::exists(build_dir, ec)) {
return {};
}
fs::remove_all(build_dir, ec);
if (ec) {
return std::unexpected(util::Error{
util::ErrorCode::Internal,
std::format("cannot remove '{}': {}", build_dir.string(), ec.message()),
"", build_dir, std::nullopt,
});
}
return {};
}
} // namespace cargoxx::cli

99
src/cli/cmd_run.cpp Normal file
View File

@@ -0,0 +1,99 @@
module cargoxx.cli;
import std;
import cargoxx.util;
import cargoxx.manifest;
import cargoxx.layout;
import cargoxx.exec;
namespace cargoxx::cli {
namespace {
namespace fs = std::filesystem;
auto join_names(const std::vector<layout::Target>& bins) -> std::string {
std::string out;
for (std::size_t i = 0; i < bins.size(); ++i) {
if (i > 0) {
out += ", ";
}
out += bins[i].name;
}
return out;
}
} // namespace
auto cmd_run(const fs::path& project_root, bool release,
std::optional<std::string> bin_name, const std::vector<std::string>& args,
std::optional<fs::path> overlay_path) -> util::Result<int> {
auto manifest_path = project_root / "Cargoxx.toml";
auto m = manifest::parse(manifest_path);
if (!m) {
return std::unexpected(m.error());
}
auto layout_result = layout::discover(project_root, m->package.name);
if (!layout_result) {
return std::unexpected(layout_result.error());
}
if (layout_result->binaries.empty()) {
return std::unexpected(util::Error{
util::ErrorCode::LayoutNoTarget,
"no binary to run",
"add src/main.cpp or a src/bin/<name>.cpp file",
project_root, std::nullopt,
});
}
const layout::Target* selected = nullptr;
if (bin_name) {
for (const auto& b : layout_result->binaries) {
if (b.name == *bin_name) {
selected = &b;
break;
}
}
if (!selected) {
return std::unexpected(util::Error{
util::ErrorCode::LayoutNoTarget,
std::format("binary '{}' not found", *bin_name),
std::format("available: {}", join_names(layout_result->binaries)),
project_root, std::nullopt,
});
}
} else {
if (layout_result->binaries.size() > 1) {
return std::unexpected(util::Error{
util::ErrorCode::LayoutNoTarget,
"multiple binaries; --bin <name> is required",
std::format("available: {}", join_names(layout_result->binaries)),
project_root, std::nullopt,
});
}
selected = &layout_result->binaries.front();
}
if (auto r = cmd_build(project_root, false, release, std::nullopt, overlay_path); !r) {
return std::unexpected(r.error());
}
const std::string profile = release ? "release" : "debug";
auto bin_path = project_root / "build" / profile / selected->name;
auto r = exec::run(bin_path.string(), args,
exec::ExecOptions{
.cwd = project_root,
.env_overrides = {},
.timeout = std::nullopt,
.inherit_stdio = true,
});
if (!r) {
return std::unexpected(r.error());
}
return r->exit_code;
}
} // namespace cargoxx::cli

36
src/cli/cmd_test.cpp Normal file
View File

@@ -0,0 +1,36 @@
module cargoxx.cli;
import std;
import cargoxx.util;
import cargoxx.exec;
namespace cargoxx::cli {
namespace fs = std::filesystem;
auto cmd_test(const fs::path& project_root, bool release,
std::optional<fs::path> overlay_path) -> util::Result<int> {
if (auto r = cmd_build(project_root, false, release, std::nullopt, std::move(overlay_path));
!r) {
return std::unexpected(r.error());
}
const std::string profile = release ? "release" : "debug";
const auto build_dir = std::format("build/{}", profile);
auto r = exec::run("nix",
{"develop", "--command", "ctest", "--test-dir", build_dir,
"--output-on-failure"},
exec::ExecOptions{
.cwd = project_root,
.env_overrides = {},
.timeout = std::nullopt,
.inherit_stdio = true,
});
if (!r) {
return std::unexpected(r.error());
}
return r->exit_code;
}
} // namespace cargoxx::cli

View File

@@ -30,6 +30,21 @@ auto run(int argc, char** argv) -> int {
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");
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
@@ -72,6 +87,38 @@ auto run(int argc, char** argv) -> int {
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;
}
return 0;
}