Skip to content

Commit

Permalink
Add --profile Option (#661)
Browse files Browse the repository at this point in the history
* implement core::validator::valid_profile

* add --profile option to build/run subcommands

* swap branch body

* cpplint doesn't like alternative tokens
  • Loading branch information
wx257osn2 committed Jul 24, 2022
1 parent ec13052 commit b67aee8
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
5 changes: 4 additions & 1 deletion include/poac/cmd/build.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ using core::resolver::ResolvedDeps;
struct Options : structopt::sub_command {
/// Build artifacts in release mode, with optimizations
Option<bool> release = false;
/// Build artifacts with the specified profile
Option<String> profile;
};

using FailedToBuild = Error<"failed to build package `{}`", String>;
using FailedToInstallDeps = Error<"failed to install dependencies">;
using UnsupportedProfile = Error<"unsupported profile `{}`", String>;

[[nodiscard]] Result<Path>
build_impl(
Expand All @@ -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_
4 changes: 3 additions & 1 deletion include/poac/cmd/run.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ namespace poac::cmd::run {
struct Options : structopt::sub_command {
/// Build artifacts in release mode, with optimizations
Option<bool> release = false;
/// Build artifacts with the specified profile
Option<String> profile;
};

[[nodiscard]] Result<void>
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_
3 changes: 3 additions & 0 deletions include/poac/core/validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ valid_description(StringRef desc);
[[nodiscard]] Result<data::manifest::PartialPackage, String>
valid_manifest(const toml::value& manifest);

[[nodiscard]] Result<Option<String>, String>
valid_profile(const Option<String>& profile, Option<bool> release);

} // namespace poac::core::validator

#endif // POAC_CORE_VALIDATOR_HPP_
10 changes: 9 additions & 1 deletion lib/cmd/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnsupportedProfile>(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);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/cmd/run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ exec(const Options& opts) {
const String name = toml::find<String>(manifest, "package", "name");

const Option<Path> output = Try(
build::build({.release = opts.release}, manifest).with_context([&name] {
return Err<build::FailedToBuild>(name).get();
})
build::build({.release = opts.release, .profile = opts.profile}, manifest)
.with_context([&name] {
return Err<build::FailedToBuild>(name).get();
})
);
if (!output.has_value()) {
return Ok();
Expand Down
20 changes: 20 additions & 0 deletions lib/core/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,24 @@ valid_manifest(const toml::value& manifest) {
return Ok(package);
}

[[nodiscard]] Result<Option<String>, String>
valid_profile(const Option<String>& profile, Option<bool> 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
35 changes: 35 additions & 0 deletions tests/unit/core/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};
}

0 comments on commit b67aee8

Please sign in to comment.