Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement clean subcommand #664

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/poac/cmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define POAC_CMD_HPP_

#include "poac/cmd/build.hpp"
#include "poac/cmd/clean.hpp"
#include "poac/cmd/create.hpp"
#include "poac/cmd/fmt.hpp"
#include "poac/cmd/graph.hpp"
Expand Down
30 changes: 30 additions & 0 deletions include/poac/cmd/clean.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef POAC_CMD_CLEAN_HPP_
#define POAC_CMD_CLEAN_HPP_

// external
#include <structopt/app.hpp>

// internal
#include "poac/cmd/build.hpp"
#include "poac/poac.hpp"

namespace poac::cmd::clean {

struct Options : structopt::sub_command {
/// Whether or not to clean release artifacts
Option<bool> release = false;
/// Clean artifacts of the specified profile
Option<String> profile;
};

[[nodiscard]] Result<void>
clean(const Options& opts);

[[nodiscard]] Result<void>
exec(const Options& opts);

} // namespace poac::cmd::clean

STRUCTOPT(poac::cmd::clean::Options, release, profile);

#endif // POAC_CMD_CLEAN_HPP_
1 change: 1 addition & 0 deletions lib/cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_poac_target(poac_cmd
SOURCES
build.cc
clean.cc
create.cc
fmt.cc
graph.cc
Expand Down
44 changes: 44 additions & 0 deletions lib/cmd/clean.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// external
#include <spdlog/spdlog.h> // NOLINT(build/include_order)

// internal
#include "poac/cmd/clean.hpp"
#include "poac/config.hpp"
#include "poac/data/manifest.hpp"
#include "poac/util/validator.hpp"

namespace poac::cmd::clean {

[[nodiscard]] Result<void>
clean(const Options& opts) {
const Option<String> profile =
Try(util::validator::valid_profile(opts.profile, opts.release)
.map_err(to_anyhow));
if (profile.has_value() && profile.value() != "debug"
&& profile.value() != "release") {
return Err<build::UnsupportedProfile>(profile.value());
}

const Path path = profile.has_value()
? config::path::output_dir / profile.value()
: config::path::output_dir;

spdlog::trace("Removing ./{}", path);

if (fs::exists(path)) {
log::status("Removing", fs::canonical(path).string());
fs::remove_all(path);
}

return Ok();
}

[[nodiscard]] Result<void>
exec(const Options& opts) {
spdlog::trace("Checking if required config exists ...");
Try(util::validator::required_config_exists().map_err(to_anyhow));

return clean(opts);
}

} // namespace poac::cmd::clean
9 changes: 8 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct Commands {
/// Compile a local package and all of its dependencies
cmd::build::Options build;

/// Remove the output directory
cmd::clean::Options clean;

/// Create a new poac package at <package_name>
cmd::create::Options create;

Expand Down Expand Up @@ -75,7 +78,9 @@ struct Commands {
STRUCTOPT(Commands, verbose, quiet, color, __VA_ARGS__); \
DECL_CMDS(__VA_ARGS__)

structopt(build, create, fmt, graph, init, lint, login, publish, run, search);
structopt(
build, clean, create, fmt, graph, init, lint, login, publish, run, search
);

inline String
colorize_structopt_error(String s) {
Expand Down Expand Up @@ -134,6 +139,8 @@ exec(const structopt::app& app, const Commands& args) {

if (args.build.has_value()) {
return cmd::build::exec(args.build);
} else if (args.clean.has_value()) {
return cmd::clean::exec(args.clean);
} else if (args.create.has_value()) {
return cmd::create::exec(args.create);
} else if (args.fmt.has_value()) {
Expand Down