[M5] add cargoxx add/remove (linkdb-validated)

This commit is contained in:
2026-05-10 00:26:21 +00:00
parent 0a398d1c31
commit 8b87d98083
9 changed files with 351 additions and 0 deletions

View File

@@ -40,6 +40,19 @@ auto cmd_test(const std::filesystem::path& project_root, bool release,
// Removes <project_root>/build/. Leaves Cargoxx.lock and flake.lock alone.
auto cmd_clean(const std::filesystem::path& project_root) -> util::Result<void>;
// Adds a dependency to Cargoxx.toml. The version is required for v0.1
// (auto-resolution against nixhub.io is deferred). The package and version
// are validated against the linkdb so the user gets an immediate error when
// the recipe is unknown.
auto cmd_add(const std::filesystem::path& project_root, const std::string& name,
const std::string& version_spec, std::vector<std::string> components,
std::optional<std::filesystem::path> overlay_path = std::nullopt)
-> util::Result<void>;
// Removes a dependency from Cargoxx.toml. Errors if the dep is not declared.
auto cmd_remove(const std::filesystem::path& project_root, const std::string& name)
-> util::Result<void>;
auto run(int argc, char** argv) -> int;
} // namespace cargoxx::cli

66
src/cli/cmd_add.cpp Normal file
View File

@@ -0,0 +1,66 @@
module cargoxx.cli;
import std;
import cargoxx.util;
import cargoxx.manifest;
import cargoxx.linkdb;
namespace cargoxx::cli {
namespace fs = std::filesystem;
auto cmd_add(const fs::path& project_root, const std::string& name,
const std::string& version_spec, std::vector<std::string> components,
std::optional<fs::path> overlay_path) -> util::Result<void> {
if (name.empty()) {
return std::unexpected(util::Error{
util::ErrorCode::ManifestInvalidField,
"package name is required",
"", std::nullopt, std::nullopt,
});
}
if (version_spec.empty()) {
return std::unexpected(util::Error{
util::ErrorCode::ManifestVersionInvalid,
std::format("version required for '{}'", name),
"use the form 'cargoxx add <pkg>@<version>' "
"(auto-resolve against nixhub is deferred)",
std::nullopt, std::nullopt,
});
}
auto manifest_path = project_root / "Cargoxx.toml";
auto m = manifest::parse(manifest_path);
if (!m) {
return std::unexpected(m.error());
}
for (const auto& d : m->dependencies) {
if (d.name == name) {
return std::unexpected(util::Error{
util::ErrorCode::ManifestInvalidField,
std::format("dependency '{}' is already declared", name),
"edit Cargoxx.toml directly to change the version",
manifest_path, std::nullopt,
});
}
}
auto db = linkdb::Database::open(std::move(overlay_path));
if (!db) {
return std::unexpected(db.error());
}
if (auto check = db->resolve(name, version_spec, components); !check) {
return std::unexpected(check.error());
}
m->dependencies.push_back(manifest::Dependency{
.name = name,
.version_spec = version_spec,
.components = std::move(components),
});
return manifest::write(*m, manifest_path);
}
} // namespace cargoxx::cli

33
src/cli/cmd_remove.cpp Normal file
View File

@@ -0,0 +1,33 @@
module cargoxx.cli;
import std;
import cargoxx.util;
import cargoxx.manifest;
namespace cargoxx::cli {
namespace fs = std::filesystem;
auto cmd_remove(const fs::path& project_root, const std::string& name)
-> util::Result<void> {
auto manifest_path = project_root / "Cargoxx.toml";
auto m = manifest::parse(manifest_path);
if (!m) {
return std::unexpected(m.error());
}
auto before = m->dependencies.size();
std::erase_if(m->dependencies,
[&](const manifest::Dependency& d) { return d.name == name; });
if (m->dependencies.size() == before) {
return std::unexpected(util::Error{
util::ErrorCode::ManifestInvalidField,
std::format("dependency '{}' is not declared", name),
"", manifest_path, std::nullopt,
});
}
return manifest::write(*m, manifest_path);
}
} // namespace cargoxx::cli

View File

@@ -45,6 +45,19 @@ auto run(int argc, char** argv) -> int {
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();
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
@@ -119,6 +132,49 @@ auto run(int argc, char** argv) -> int {
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;
}
return 0;
}