diff --git a/include/poac/cmd/build.hpp b/include/poac/cmd/build.hpp index 1430b3170..985459b4f 100644 --- a/include/poac/cmd/build.hpp +++ b/include/poac/cmd/build.hpp @@ -19,10 +19,13 @@ using core::resolver::ResolvedDeps; struct Options : structopt::sub_command { /// Build artifacts in release mode, with optimizations Option release = false; + /// Build artifacts with the specified profile + Option profile; }; using FailedToBuild = Error<"failed to build package `{}`", String>; using FailedToInstallDeps = Error<"failed to install dependencies">; +using UnsupportedProfile = Error<"unsupported profile `{}`", String>; [[nodiscard]] Result build_impl( @@ -38,6 +41,6 @@ exec(const Options& opts); } // namespace poac::cmd::build -STRUCTOPT(poac::cmd::build::Options, release); +STRUCTOPT(poac::cmd::build::Options, release, profile); #endif // POAC_CMD_BUILD_HPP_ diff --git a/include/poac/cmd/run.hpp b/include/poac/cmd/run.hpp index dd83812ec..01e687c24 100644 --- a/include/poac/cmd/run.hpp +++ b/include/poac/cmd/run.hpp @@ -12,6 +12,8 @@ namespace poac::cmd::run { struct Options : structopt::sub_command { /// Build artifacts in release mode, with optimizations Option release = false; + /// Build artifacts with the specified profile + Option profile; }; [[nodiscard]] Result @@ -19,6 +21,6 @@ exec(const Options& opts); } // namespace poac::cmd::run -STRUCTOPT(poac::cmd::run::Options, release); +STRUCTOPT(poac::cmd::run::Options, release, profile); #endif // POAC_CMD_RUN_HPP_ diff --git a/include/poac/core/validator.hpp b/include/poac/core/validator.hpp index acd12b064..1140bf36e 100644 --- a/include/poac/core/validator.hpp +++ b/include/poac/core/validator.hpp @@ -80,6 +80,9 @@ valid_description(StringRef desc); [[nodiscard]] Result valid_manifest(const toml::value& manifest); +[[nodiscard]] Result, String> +valid_profile(const Option& profile, Option release); + } // namespace poac::core::validator #endif // POAC_CORE_VALIDATOR_HPP_ diff --git a/lib/cmd/build.cc b/lib/cmd/build.cc index 372c2872d..8c575b221 100644 --- a/lib/cmd/build.cc +++ b/lib/cmd/build.cc @@ -44,7 +44,15 @@ build(const Options& opts, const toml::value& manifest) { return Ok(None); } - const Mode mode = opts.release.value() ? Mode::release : Mode::debug; + const auto profile = + Try(core::validator::valid_profile(opts.profile, opts.release) + .map_err(to_anyhow)) + .value_or("debug"); + if (profile != "debug" && profile != "release") { + return Err(profile); + } + + const Mode mode = profile == "release" ? Mode::release : Mode::debug; const Path output_path = Try(build_impl(manifest, mode, resolved_deps)); return Ok(output_path); } diff --git a/lib/cmd/run.cc b/lib/cmd/run.cc index dafeccbd1..ffabafd9f 100644 --- a/lib/cmd/run.cc +++ b/lib/cmd/run.cc @@ -21,9 +21,10 @@ exec(const Options& opts) { const String name = toml::find(manifest, "package", "name"); const Option output = Try( - build::build({.release = opts.release}, manifest).with_context([&name] { - return Err(name).get(); - }) + build::build({.release = opts.release, .profile = opts.profile}, manifest) + .with_context([&name] { + return Err(name).get(); + }) ); if (!output.has_value()) { return Ok(); diff --git a/lib/core/validator.cc b/lib/core/validator.cc index 89634639b..2f25ee142 100644 --- a/lib/core/validator.cc +++ b/lib/core/validator.cc @@ -382,4 +382,24 @@ valid_manifest(const toml::value& manifest) { return Ok(package); } +[[nodiscard]] Result, String> +valid_profile(const Option& profile, Option release) { + if (release.has_value() && release.value()) { + if (profile.has_value() && profile.value() != "release") { + return Err(format( + "Specified profiles are conflicted: you specify --release and --profile={}.", + profile.value() + )); + } else { + return Ok("release"); + } + } else { + if (profile.has_value()) { + return Ok(profile.value()); + } else { + return Ok(None); + } + } +} + } // namespace poac::core::validator diff --git a/tests/unit/core/validator.cc b/tests/unit/core/validator.cc index 64887e361..412968f06 100644 --- a/tests/unit/core/validator.cc +++ b/tests/unit/core/validator.cc @@ -228,4 +228,39 @@ description = "Manifest Test library" ); })) << "incorrect data type"; }; + + "test valid_profile"_test = [] { + using poac::core::validator::valid_profile; + using poac::None; + + { + auto x = valid_profile(None, None); + expect(x.is_ok()); + expect(!x.unwrap().has_value()); + } + { + auto x = valid_profile(None, false); + expect(x.is_ok()); + expect(!x.unwrap().has_value()); + } + { + auto x = valid_profile(None, true); + expect(x.is_ok()); + expect(x.unwrap() == "release"); + } + { + auto x = valid_profile("debug", None); + expect(x.is_ok()); + expect(x.unwrap() == "debug"); + } + { + auto x = valid_profile("debug", false); + expect(x.is_ok()); + expect(x.unwrap() == "debug"); + } + { + auto x = valid_profile("debug", true); + expect(x.is_err()); + } + }; }