From 0b22be0cc6a99d2df8e3b4d9eae2c7b94b71d108 Mon Sep 17 00:00:00 2001 From: I <1091761+wx257osn2@users.noreply.github.com> Date: Sun, 24 Jul 2022 18:15:36 +0900 Subject: [PATCH 1/4] implement clean command --- include/poac/cmd.hpp | 1 + include/poac/cmd/clean.hpp | 30 ++++++++++++++++++++++++ lib/cmd/CMakeLists.txt | 1 + lib/cmd/clean.cc | 48 ++++++++++++++++++++++++++++++++++++++ src/main.cc | 9 ++++++- 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 include/poac/cmd/clean.hpp create mode 100644 lib/cmd/clean.cc diff --git a/include/poac/cmd.hpp b/include/poac/cmd.hpp index 17527ea0e..5961d88fa 100644 --- a/include/poac/cmd.hpp +++ b/include/poac/cmd.hpp @@ -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" diff --git a/include/poac/cmd/clean.hpp b/include/poac/cmd/clean.hpp new file mode 100644 index 000000000..caf8a7a63 --- /dev/null +++ b/include/poac/cmd/clean.hpp @@ -0,0 +1,30 @@ +#ifndef POAC_CMD_CLEAN_HPP_ +#define POAC_CMD_CLEAN_HPP_ + +// external +#include + +// 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 release = false; + /// Clean artifacts of the specified profile + Option profile; +}; + +[[nodiscard]] Result +clean(const Options& opts); + +[[nodiscard]] Result +exec(const Options& opts); + +} // namespace poac::cmd::clean + +STRUCTOPT(poac::cmd::clean::Options, release, profile); + +#endif // POAC_CMD_CLEAN_HPP_ diff --git a/lib/cmd/CMakeLists.txt b/lib/cmd/CMakeLists.txt index 77a379133..f7e3146fa 100644 --- a/lib/cmd/CMakeLists.txt +++ b/lib/cmd/CMakeLists.txt @@ -1,6 +1,7 @@ add_poac_target(poac_cmd SOURCES build.cc + clean.cc create.cc fmt.cc graph.cc diff --git a/lib/cmd/clean.cc b/lib/cmd/clean.cc new file mode 100644 index 000000000..f631e9679 --- /dev/null +++ b/lib/cmd/clean.cc @@ -0,0 +1,48 @@ +// std +#include +#include + +// external +#include // 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 +clean(const Options& opts) { + const auto 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(profile.value()); + } + + const auto path = profile.has_value() + ? config::path::output_dir / profile.value() + : config::path::output_dir; + + spdlog::trace("Removing ./{}", path); + + if (std::filesystem::exists(path)) { + log::status("Removing", std::filesystem::canonical(path).string()); + std::filesystem::remove_all(path); + } + + return Ok(); +} + +[[nodiscard]] Result +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 diff --git a/src/main.cc b/src/main.cc index f150e94c8..de88edfbf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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 cmd::create::Options create; @@ -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) { @@ -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()) { From 7c16a8f07a3121236869c74779cb089fc1bdb81c Mon Sep 17 00:00:00 2001 From: I <1091761+wx257osn2@users.noreply.github.com> Date: Tue, 26 Jul 2022 07:27:09 +0900 Subject: [PATCH 2/4] remove unused includes --- lib/cmd/clean.cc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/cmd/clean.cc b/lib/cmd/clean.cc index f631e9679..e6d148a6a 100644 --- a/lib/cmd/clean.cc +++ b/lib/cmd/clean.cc @@ -1,7 +1,3 @@ -// std -#include -#include - // external #include // NOLINT(build/include_order) From 934649cd54e0d4775fb03e246a633022d072818e Mon Sep 17 00:00:00 2001 From: I <1091761+wx257osn2@users.noreply.github.com> Date: Tue, 26 Jul 2022 07:27:21 +0900 Subject: [PATCH 3/4] Use concrete types instead of auto Co-authored-by: Ken Matsui <26405363+ken-matsui@users.noreply.github.com> --- lib/cmd/clean.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cmd/clean.cc b/lib/cmd/clean.cc index e6d148a6a..d0277e183 100644 --- a/lib/cmd/clean.cc +++ b/lib/cmd/clean.cc @@ -11,7 +11,7 @@ namespace poac::cmd::clean { [[nodiscard]] Result clean(const Options& opts) { - const auto profile = + const Option profile = Try(util::validator::valid_profile(opts.profile, opts.release) .map_err(to_anyhow)); if (profile.has_value() && profile.value() != "debug" @@ -19,7 +19,7 @@ clean(const Options& opts) { return Err(profile.value()); } - const auto path = profile.has_value() + const Path path = profile.has_value() ? config::path::output_dir / profile.value() : config::path::output_dir; From 7f04cebe3c8f41871fba4c21c223be674582e16f Mon Sep 17 00:00:00 2001 From: I <1091761+wx257osn2@users.noreply.github.com> Date: Tue, 26 Jul 2022 07:29:54 +0900 Subject: [PATCH 4/4] s/std::filesystem/fs/ --- lib/cmd/clean.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cmd/clean.cc b/lib/cmd/clean.cc index d0277e183..a51c9d85c 100644 --- a/lib/cmd/clean.cc +++ b/lib/cmd/clean.cc @@ -25,9 +25,9 @@ clean(const Options& opts) { spdlog::trace("Removing ./{}", path); - if (std::filesystem::exists(path)) { - log::status("Removing", std::filesystem::canonical(path).string()); - std::filesystem::remove_all(path); + if (fs::exists(path)) { + log::status("Removing", fs::canonical(path).string()); + fs::remove_all(path); } return Ok();