Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions dfx/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::dfinity::ConfigCanistersCanister;
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};
Expand All @@ -11,7 +11,12 @@ pub fn construct() -> App<'static, 'static> {
.arg(Arg::with_name("canister").help("The canister name to build."))
}

fn build_file<T>(env: &T, input_path: &Path, output_path: &Path) -> DfxResult
fn build_file<T>(
env: &T,
profile: Option<Profile>,
input_path: &Path,
output_path: &Path,
) -> DfxResult
where
T: BinaryResolverEnv,
{
Expand All @@ -30,9 +35,15 @@ where
Some("as") => {
let output_idl_path = output_path.with_extension("did");
let output_js_path = output_path.with_extension("js");

// 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",
};
env.get_binary_command("asc")?
.arg(&input_path)
.arg(arg_profile)
.arg("-o")
.arg(&output_wasm_path)
.output()?;
Expand Down Expand Up @@ -89,7 +100,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, &input_as_path, &output_path)?;
build_file(
env,
config.config.profile.clone(),
&input_as_path,
&output_path,
)?;
}
}
}
Expand Down Expand Up @@ -142,8 +158,13 @@ mod tests {
out_file: &out_file,
};

build_file(&env, 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.");

Expand All @@ -154,7 +175,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(" ", "")
Expand Down Expand Up @@ -188,7 +209,8 @@ 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, None, input_path.as_path(), output_path.as_path())
.expect("Function failed.");

assert!(output_path.exists());
}
Expand Down
12 changes: 11 additions & 1 deletion dfx/src/config/dfinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ pub struct ConfigDefaultsBuild {
pub output: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Profile {
// debug is for development only
Debug,
// release is for production
Release,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ConfigDefaults {
pub build: Option<ConfigDefaultsBuild>,
Expand All @@ -46,6 +54,7 @@ pub struct ConfigDefaults {

#[derive(Debug, Serialize, Deserialize)]
pub struct ConfigInterface {
pub profile: Option<Profile>,
pub version: Option<u32>,
pub dfx: Option<String>,
pub canisters: Option<Map<String, Value>>,
Expand Down Expand Up @@ -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)]
Expand Down