From 59c4d0430a52063aeb3bb1b4e3eed8e9b7be729f Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 12 Nov 2019 09:58:41 -0800 Subject: [PATCH] feat: add support for argument to dfx build (#158) Before it was completely ignored. Fixes SDK-544 --- dfx/src/commands/build.rs | 15 ++++++++++++--- dfx/src/lib/error/build.rs | 7 +++++++ e2e/build.bash | 8 ++++++++ e2e/utils/assertions.bash | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index ce162ad38f..1733789d1f 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -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}; @@ -106,7 +107,11 @@ fn build_file(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(); @@ -162,7 +167,7 @@ where Ok(()) } -pub fn exec(env: &T, _args: &ArgMatches<'_>) -> DfxResult +pub fn exec(env: &T, args: &ArgMatches<'_>) -> DfxResult where T: BinaryResolverEnv + ProjectConfigEnv, { @@ -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)?; diff --git a/dfx/src/lib/error/build.rs b/dfx/src/lib/error/build.rs index 757ddb63e9..4805179e4b 100644 --- a/dfx/src/lib/error/build.rs +++ b/dfx/src/lib/error/build.rs @@ -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 { @@ -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, + )), } } } diff --git a/e2e/build.bash b/e2e/build.bash index 8862ff1351..671a0ca17e 100644 --- a/e2e/build.bash +++ b/e2e/build.bash @@ -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.*" +} diff --git a/e2e/utils/assertions.bash b/e2e/utils/assertions.bash index c8f5bce6f2..0f85424044 100644 --- a/e2e/utils/assertions.bash +++ b/e2e/utils/assertions.bash @@ -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)