Skip to content

Commit

Permalink
Cmd: add subcommand remove
Browse files Browse the repository at this point in the history
Remove project dependencies from cabin.toml
  • Loading branch information
SunPodder committed Jan 27, 2025
1 parent 4974f68 commit 7cb7a10
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LIBCURL_VERREQ := libcurl >= 7.79.1, libcurl < 9.0.0
NLOHMANN_JSON_VERREQ := nlohmann_json >= 3.10.5, nlohmann_json < 4.0.0
TBB_VERREQ := tbb >= 2021.5.0, tbb < 2023.0.0
FMT_VERREQ := fmt >= 9, fmt < 12.0.0
TOML11_VER := $(shell grep -m1 toml11 cabin.toml | sed 's/.*tag = \(.*\)}/\1/' | tr -d '"')
TOML11_VER := fdd5e29f78eb9e58cc02736f2dc4263ed0058662
RESULT_VER := $(shell grep -m1 cpp-result cabin.toml | sed 's/.*tag = \(.*\)}/\1/' | tr -d '"')

DEFINES := -DCABIN_CABIN_PKG_VERSION='"$(VERSION)"' \
Expand Down
2 changes: 1 addition & 1 deletion cabin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/cabinpkg/cabin"
version = "0.12.0-dev"

[dependencies]
toml11 = {git = "https://github.com/ToruNiina/toml11.git", tag = "v4.2.0"}
toml11 = {git = "https://github.com/ToruNiina/toml11.git", rev = "fdd5e29f78eb9e58cc02736f2dc4263ed0058662"}
mitama-cpp-result = {git = "https://github.com/loliGothicK/mitama-cpp-result.git", tag = "v11.0.0"}
fmt = {version = ">=9 && <12", system = true}
libcurl = {version = ">=7.79.1 && <9", system = true}
Expand Down
9 changes: 9 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ Downloaded ToruNiina/toml11 846abd9a49082fe51440aa07005c360f13a67bbf
Finished debug target(s) in 0.70s
```
### Remove dependencies
Use the `remove` command to remove dependencies from cabin.toml:
```console
you:~/hello_world$ cabin remove my_dep
Removed my_dep from cabin.toml
```
## Unit tests
You can write unit tests in any source files within the `src` directory. Create a new file like:
Expand Down
2 changes: 2 additions & 0 deletions src/Cabin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Algos.hpp"
#include "Cli.hpp"
#include "Cmd.hpp"
#include "Cmd/Remove.hpp"
#include "Logger.hpp"
#include "Rustify/Result.hpp"
#include "TermColor.hpp"
Expand Down Expand Up @@ -59,6 +60,7 @@ getCli() noexcept {
.addSubcmd(INIT_CMD)
.addSubcmd(LINT_CMD)
.addSubcmd(NEW_CMD)
.addSubcmd(REMOVE_CMD)
.addSubcmd(RUN_CMD)
.addSubcmd(SEARCH_CMD)
.addSubcmd(TEST_CMD)
Expand Down
61 changes: 61 additions & 0 deletions src/Cmd/Remove.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "Remove.hpp"

#include "../Cli.hpp"
#include "../Logger.hpp"
#include "../Manifest.hpp"
#include "../Rustify/Result.hpp"

#include <cstdlib>
#include <fmt/std.h>
#include <fstream>
#include <functional>
#include <span>
#include <string>
#include <string_view>
#include <toml.hpp>
#include <toml11/types.hpp>
#include <vector>

namespace cabin {

static Result<void> removeMain(std::span<const std::string_view> args);

const Subcmd REMOVE_CMD = Subcmd{ "remove" }
.setDesc("Remove dependencies from cabin.toml")
.setArg(Arg{ "args" }
.setDesc("Dependencies to remove")
.setRequired(true)
.setVariadic(true))
.setMainFn(removeMain);

static Result<void>
removeMain(std::span<const std::string_view> args) {
Ensure(!args.empty(), "No dependencies to remove");

std::vector<std::string_view> removedDeps = {};
const fs::path manifestPath = Try(findManifest());

auto data = toml::parse<toml::ordered_type_config>(manifestPath);
auto& deps = data["dependencies"];

Ensure(!deps.is_empty(), "No dependencies to remove");

for (const std::string_view& dep : args) {
const std::string depName = std::string(dep);
if (deps.contains(depName)) {
deps.as_table().erase(depName);
removedDeps.push_back(dep);
} else {
logger::warn("Dependency `{}` not found in cabin.toml", dep);
}
}

if (!removedDeps.empty()) {
std::ofstream out(manifestPath);
out << data;
logger::info("Removed", "{} from cabin.toml", fmt::join(removedDeps, ", "));
}
return Ok();
}

} // namespace cabin
9 changes: 9 additions & 0 deletions src/Cmd/Remove.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "../Cli.hpp"

namespace cabin {

extern const Subcmd REMOVE_CMD;

} // namespace cabin

0 comments on commit 7cb7a10

Please sign in to comment.