Skip to content

Commit

Permalink
feat: add support for argument to dfx build (#158)
Browse files Browse the repository at this point in the history
Before it was completely ignored.

Fixes SDK-544
  • Loading branch information
Hans authored Nov 12, 2019
1 parent 8ad3376 commit 59c4d04
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
15 changes: 12 additions & 3 deletions dfx/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::dfinity::{Config, Profile};
use crate::lib::canister_info::CanisterInfo;
use crate::lib::env::{BinaryResolverEnv, ProjectConfigEnv};
use crate::lib::error::DfxError::BuildError;
use crate::lib::error::{BuildErrorKind, DfxError, DfxResult};
use crate::lib::message::UserMessage;
use clap::{App, Arg, ArgMatches, SubCommand};
Expand Down Expand Up @@ -106,7 +107,11 @@ fn build_file<T>(env: &T, config: &Config, name: &str) -> DfxResult
where
T: BinaryResolverEnv,
{
let canister_info = CanisterInfo::load(config, name)?;
let canister_info = CanisterInfo::load(config, name).map_err(|_| {
BuildError(BuildErrorKind::CanisterNameIsNotInConfigError(
name.to_owned(),
))
})?;
let config = config.get_config();
let profile = config.profile.clone();
let input_path = canister_info.get_main_path();
Expand Down Expand Up @@ -162,7 +167,7 @@ where
Ok(())
}

pub fn exec<T>(env: &T, _args: &ArgMatches<'_>) -> DfxResult
pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult
where
T: BinaryResolverEnv + ProjectConfigEnv,
{
Expand All @@ -171,7 +176,11 @@ where
.get_config()
.ok_or(DfxError::CommandMustBeRunInAProject)?;

if let Some(canisters) = &config.get_config().canisters {
// Get the canister name (if any).
if let Some(canister_name) = args.value_of("canister") {
println!("Building {}...", canister_name);
build_file(env, &config, &canister_name)?;
} else if let Some(canisters) = &config.get_config().canisters {
for k in canisters.keys() {
println!("Building {}...", k);
build_file(env, &config, &k)?;
Expand Down
7 changes: 7 additions & 0 deletions dfx/src/lib/error/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub enum BuildErrorKind {

/// An error happened while compiling WAT to WASM.
WatCompileError(wabt::Error),

/// Could not find the canister to build in the config.
CanisterNameIsNotInConfigError(String),
}

impl fmt::Display for BuildErrorKind {
Expand All @@ -39,6 +42,10 @@ impl fmt::Display for BuildErrorKind {
WatCompileError(e) => {
f.write_fmt(format_args!("Error while compiling WAT to WASM: {}", e))
}
CanisterNameIsNotInConfigError(name) => f.write_fmt(format_args!(
r#"Could not find the canister named "{}" in the dfx.json configuration."#,
name,
)),
}
}
}
8 changes: 8 additions & 0 deletions e2e/build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ teardown() {
assert_command dfx build
[[ -f canisters/hello/_canister.id ]]
}

@test "build can take a single argument" {
assert_command dfx build hello
assert_match "Building hello..."

assert_command_fail dfx build unknown_canister
assert_match "Could not find.*unknown_canister.*"
}
2 changes: 1 addition & 1 deletion e2e/utils/assertions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ assert_match() {
else
text="$2"
fi
[[ "$text" =~ "$regex" ]] || \
[[ "$text" =~ $regex ]] || \
(batslib_print_kv_single_or_multi 10 "regex" "$regex" "actual" "$text" \
| batslib_decorate "output does not match" \
| fail)
Expand Down

0 comments on commit 59c4d04

Please sign in to comment.