Skip to content

Commit

Permalink
Cmd: add subcommand remove (#1125)
Browse files Browse the repository at this point in the history
Remove project dependencies from cabin.toml

closes #1074
  • Loading branch information
SunPodder authored Jan 30, 2025
1 parent 58fa97b commit 6a0717b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 10 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 := $(shell grep -m1 toml11 cabin.toml | sed 's/.*rev = \(.*\)}/\1/' | tr -d '"')
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
1 change: 1 addition & 0 deletions src/Cabin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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
1 change: 1 addition & 0 deletions src/Cmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Cmd/Init.hpp"
#include "Cmd/Lint.hpp"
#include "Cmd/New.hpp"
#include "Cmd/Remove.hpp"
#include "Cmd/Run.hpp"
#include "Cmd/Search.hpp"
#include "Cmd/Test.hpp"
Expand Down
64 changes: 64 additions & 0 deletions src/Cmd/Remove.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "Remove.hpp"

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

#include <cstdlib>
#include <fmt/ranges.h>
#include <fstream>
#include <string>
#include <toml.hpp>
#include <toml11/types.hpp>
#include <vector>

namespace cabin {

static Result<void> removeMain(CliArgsView args);

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

static Result<void>
removeMain(const CliArgsView args) {
Ensure(!args.empty(), "`cabin remove` requires at least one argument");

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& dep : args) {
if (deps.contains(dep)) {
deps.as_table().erase(dep);
removedDeps.push_back(dep);
} else {
// manifestPath needs to be converted to string
// or it adds extra quotes around the path
logger::warn(
"Dependency `{}` not found in {}", dep, manifestPath.string()
);
}
}

if (!removedDeps.empty()) {
std::ofstream out(manifestPath);
out << data;
logger::info(
"Removed", "{} from {}", fmt::join(removedDeps, ", "),
manifestPath.string()
);
}
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
8 changes: 0 additions & 8 deletions src/Rustify/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ try_find(const toml::value& v, const U&... u) noexcept {
}
}

template <std::default_initializable T, typename... K>
inline auto
find_or_default(
const toml::value& v, const K&... keys
) noexcept(std::is_nothrow_default_constructible_v<T>) {
return toml::find_or<T>(v, keys..., T{});
}

} // namespace toml

#endif
Expand Down
26 changes: 26 additions & 0 deletions tests/07-remove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

test_description='Test the remove command'

WHEREAMI=$(dirname "$(realpath "$0")")
. $WHEREAMI/setup.sh

test_expect_success 'cabin remove tbb mydep toml11' '
test_when_finished "rm -rf remove_test" &&
"$CABIN_BIN" new remove_test &&
cd remove_test &&
echo "[dependencies]" >> cabin.toml &&
echo "tbb = {}" >> cabin.toml &&
echo "toml11 = {}" >> cabin.toml &&
(
"$CABIN_BIN" remove tbb mydep toml11 2>actual &&
! grep -q "tbb" cabin.toml &&
! grep -q "toml11" cabin.toml
) &&
cat >expected <<-EOF &&
Warning: Dependency \`mydep\` not found in $WHEREAMI/trash directory.07-remove.sh/remove_test/cabin.toml
Removed tbb, toml11 from $WHEREAMI/trash directory.07-remove.sh/remove_test/cabin.toml
EOF
test_cmp expected actual
'
test_done

0 comments on commit 6a0717b

Please sign in to comment.