From b52d2ca961c11e5f7c25e01057ed76adba3a9a5c Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 13:58:47 -0600 Subject: [PATCH 1/9] a config's 'profile' is either 'Debug' or 'Release' --- dfx/src/commands/build.rs | 32 +++++++++++++++++++++++--------- dfx/src/config/dfinity.rs | 12 +++++++++++- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 5a8463f657..474fc13adf 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -1,4 +1,4 @@ -use crate::config::dfinity::ConfigCanistersCanister; +use crate::config::dfinity::{ConfigCanistersCanister, Config, Profile}; use crate::lib::env::{BinaryResolverEnv, ProjectConfigEnv}; use crate::lib::error::{BuildErrorKind, DfxError, DfxResult}; use clap::{App, Arg, ArgMatches, SubCommand}; @@ -11,7 +11,7 @@ pub fn construct() -> App<'static, 'static> { .arg(Arg::with_name("canister").help("The canister name to build.")) } -fn build_file(env: &T, input_path: &Path, output_path: &Path) -> DfxResult +fn build_file(env: &T, config:&Config, input_path: &Path, output_path: &Path) -> DfxResult where T: BinaryResolverEnv, { @@ -30,12 +30,26 @@ where Some("as") => { let output_idl_path = output_path.with_extension("did"); let output_js_path = output_path.with_extension("js"); - - env.get_binary_command("asc")? - .arg(&input_path) - .arg("-o") - .arg(&output_wasm_path) - .output()?; + // invoke the compiler in debug (development) or release mode, + // based on the current profile: + match config.config.profile { + None | Some(Profile::Debug) => { + env.get_binary_command("asc")? + .arg(&input_path) + .arg("--debug") + .arg("-o") + .arg(&output_wasm_path) + .output()?; + } + Some(Profile::Release) => { + env.get_binary_command("asc")? + .arg(&input_path) + .arg("--release") + .arg("-o") + .arg(&output_wasm_path) + .output()?; + } + } env.get_binary_command("asc")? .arg("--idl") .arg(&input_path) @@ -89,7 +103,7 @@ where let output_path = build_root.join(x.as_str()).with_extension("wasm"); std::fs::create_dir_all(output_path.parent().unwrap())?; - build_file(env, &input_as_path, &output_path)?; + build_file(env, &config, &input_as_path, &output_path)?; } } } diff --git a/dfx/src/config/dfinity.rs b/dfx/src/config/dfinity.rs index 4161a005df..1292e0f627 100644 --- a/dfx/src/config/dfinity.rs +++ b/dfx/src/config/dfinity.rs @@ -38,6 +38,14 @@ pub struct ConfigDefaultsBuild { pub output: Option, } +#[derive(Debug, Serialize, Deserialize)] +pub enum Profile { + // debug is for development only + Debug, + // release is for production + Release, +} + #[derive(Debug, Serialize, Deserialize)] pub struct ConfigDefaults { pub build: Option, @@ -46,6 +54,7 @@ pub struct ConfigDefaults { #[derive(Debug, Serialize, Deserialize)] pub struct ConfigInterface { + pub profile: Option, pub version: Option, pub dfx: Option, pub canisters: Option>, @@ -135,7 +144,8 @@ impl ConfigInterface { pub struct Config { path: PathBuf, json: Value, - config: ConfigInterface, + // public interface to the config: + pub config: ConfigInterface, } #[allow(dead_code)] From 76fec5615ff4e0058be1f779da42abdb307290ee Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 14:24:01 -0600 Subject: [PATCH 2/9] finish refactor --- dfx/src/commands/build.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 474fc13adf..12c53cfcba 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -156,7 +156,9 @@ mod tests { out_file: &out_file, }; - build_file(&env, Path::new("/in/file.as"), Path::new("/out/file.wasm")) + build_file(&env, + &env.get_config().unwrap(), + Path::new("/in/file.as"), Path::new("/out/file.wasm")) .expect("Function failed."); out_file.flush().expect("Could not flush."); @@ -202,7 +204,9 @@ mod tests { assert!(!output_path.exists()); std::fs::write(input_path.as_path(), wat).expect("Could not create input."); - build_file(&env, input_path.as_path(), output_path.as_path()).expect("Function failed."); + build_file(&env, + &env.get_config().unwrap(), + input_path.as_path(), output_path.as_path()).expect("Function failed."); assert!(output_path.exists()); } From ab4a1a94a8604916c014589ae355f95f72cc953b Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 15:22:31 -0600 Subject: [PATCH 3/9] review nit --- dfx/src/commands/build.rs | 8 ++++---- dfx/src/config/dfinity.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 12c53cfcba..354dc8f95d 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -1,4 +1,4 @@ -use crate::config::dfinity::{ConfigCanistersCanister, Config, Profile}; +use crate::config::dfinity::{ConfigCanistersCanister, Profile}; use crate::lib::env::{BinaryResolverEnv, ProjectConfigEnv}; use crate::lib::error::{BuildErrorKind, DfxError, DfxResult}; use clap::{App, Arg, ArgMatches, SubCommand}; @@ -11,7 +11,7 @@ pub fn construct() -> App<'static, 'static> { .arg(Arg::with_name("canister").help("The canister name to build.")) } -fn build_file(env: &T, config:&Config, input_path: &Path, output_path: &Path) -> DfxResult +fn build_file(env: &T, profile:Option, input_path: &Path, output_path: &Path) -> DfxResult where T: BinaryResolverEnv, { @@ -32,7 +32,7 @@ where let output_js_path = output_path.with_extension("js"); // invoke the compiler in debug (development) or release mode, // based on the current profile: - match config.config.profile { + match profile { None | Some(Profile::Debug) => { env.get_binary_command("asc")? .arg(&input_path) @@ -103,7 +103,7 @@ where let output_path = build_root.join(x.as_str()).with_extension("wasm"); std::fs::create_dir_all(output_path.parent().unwrap())?; - build_file(env, &config, &input_as_path, &output_path)?; + build_file(env, config.config.profile.clone(), &input_as_path, &output_path)?; } } } diff --git a/dfx/src/config/dfinity.rs b/dfx/src/config/dfinity.rs index 1292e0f627..5696b5ae94 100644 --- a/dfx/src/config/dfinity.rs +++ b/dfx/src/config/dfinity.rs @@ -38,7 +38,7 @@ pub struct ConfigDefaultsBuild { pub output: Option, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub enum Profile { // debug is for development only Debug, From 0c077dbb00443687388f5941ab5c815073d2e727 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 15:40:32 -0600 Subject: [PATCH 4/9] fix tests --- dfx/src/commands/build.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 354dc8f95d..8609705db3 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -156,8 +156,7 @@ mod tests { out_file: &out_file, }; - build_file(&env, - &env.get_config().unwrap(), + build_file(&env, None, Path::new("/in/file.as"), Path::new("/out/file.wasm")) .expect("Function failed."); @@ -204,8 +203,7 @@ mod tests { assert!(!output_path.exists()); std::fs::write(input_path.as_path(), wat).expect("Could not create input."); - build_file(&env, - &env.get_config().unwrap(), + build_file(&env, None, input_path.as_path(), output_path.as_path()).expect("Function failed."); assert!(output_path.exists()); From f59955539224aa6f5bf1f73a4e80900e26b5d2e5 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 16:01:58 -0600 Subject: [PATCH 5/9] fix a(nother) test --- dfx/src/commands/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 8609705db3..8f882985da 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -169,7 +169,7 @@ mod tests { assert_eq!( s.trim(), - r#"asc /in/file.as -o /out/file.wasm + r#"asc /in/file.as --debug -o /out/file.wasm asc --idl /in/file.as -o /out/file.did didc --js /out/file.did -o /out/file.js"# .replace(" ", "") From 47dd6a628ca781fca146ecd1c6c8be69a6414135 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 16:10:32 -0600 Subject: [PATCH 6/9] cargo fmt; I feel like a cog now --- dfx/src/commands/build.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 8f882985da..dc2fde145c 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -11,7 +11,12 @@ pub fn construct() -> App<'static, 'static> { .arg(Arg::with_name("canister").help("The canister name to build.")) } -fn build_file(env: &T, profile:Option, input_path: &Path, output_path: &Path) -> DfxResult +fn build_file( + env: &T, + profile: Option, + input_path: &Path, + output_path: &Path, +) -> DfxResult where T: BinaryResolverEnv, { @@ -103,7 +108,12 @@ where let output_path = build_root.join(x.as_str()).with_extension("wasm"); std::fs::create_dir_all(output_path.parent().unwrap())?; - build_file(env, config.config.profile.clone(), &input_as_path, &output_path)?; + build_file( + env, + config.config.profile.clone(), + &input_as_path, + &output_path, + )?; } } } @@ -156,9 +166,13 @@ mod tests { out_file: &out_file, }; - build_file(&env, None, - Path::new("/in/file.as"), Path::new("/out/file.wasm")) - .expect("Function failed."); + build_file( + &env, + None, + Path::new("/in/file.as"), + Path::new("/out/file.wasm"), + ) + .expect("Function failed."); out_file.flush().expect("Could not flush."); @@ -203,8 +217,8 @@ mod tests { assert!(!output_path.exists()); std::fs::write(input_path.as_path(), wat).expect("Could not create input."); - build_file(&env, None, - input_path.as_path(), output_path.as_path()).expect("Function failed."); + build_file(&env, None, input_path.as_path(), output_path.as_path()) + .expect("Function failed."); assert!(output_path.exists()); } From f58207ee776d8f2f4541902dc91a0717c9138336 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 16:14:41 -0600 Subject: [PATCH 7/9] attempt to address Gabor's comment --- dfx/src/commands/build.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index dc2fde145c..29555064dc 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -37,24 +37,16 @@ where let output_js_path = output_path.with_extension("js"); // invoke the compiler in debug (development) or release mode, // based on the current profile: - match profile { - None | Some(Profile::Debug) => { - env.get_binary_command("asc")? - .arg(&input_path) - .arg("--debug") - .arg("-o") - .arg(&output_wasm_path) - .output()?; - } - Some(Profile::Release) => { - env.get_binary_command("asc")? - .arg(&input_path) - .arg("--release") - .arg("-o") - .arg(&output_wasm_path) - .output()?; - } - } + let arg_profile = match profile { + None | Some(Profile::Debug) => &"--debug", + Some(Profile::Release) => &"--release", + }; + env.get_binary_command("asc")? + .arg(&input_path) + .arg(arg_profile) + .arg("-o") + .arg(&output_wasm_path) + .output()?; env.get_binary_command("asc")? .arg("--idl") .arg(&input_path) From 8341fe073ea1adebe048b46b6088b2772b99e4b5 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Wed, 2 Oct 2019 16:18:33 -0600 Subject: [PATCH 8/9] cargo fmt, again --- dfx/src/commands/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 29555064dc..391f5004cf 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -39,7 +39,7 @@ where // based on the current profile: let arg_profile = match profile { None | Some(Profile::Debug) => &"--debug", - Some(Profile::Release) => &"--release", + Some(Profile::Release) => &"--release", }; env.get_binary_command("asc")? .arg(&input_path) From 3bf596061ffe0ce3f3f221d3a6aa6389f17361ad Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Thu, 3 Oct 2019 08:53:38 -0600 Subject: [PATCH 9/9] Hans' nit --- dfx/src/commands/build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 391f5004cf..938785a8d1 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -38,8 +38,8 @@ where // invoke the compiler in debug (development) or release mode, // based on the current profile: let arg_profile = match profile { - None | Some(Profile::Debug) => &"--debug", - Some(Profile::Release) => &"--release", + None | Some(Profile::Debug) => "--debug", + Some(Profile::Release) => "--release", }; env.get_binary_command("asc")? .arg(&input_path)