Skip to content

Commit

Permalink
other spawned commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Feb 10, 2025
1 parent 5128f08 commit cef5f92
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/dfx/src/lib/builders/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn build_frontend(
command
);

super::run_command(command, &vars, project_root)
super::run_command(env, command, &vars, project_root)
.with_context(|| format!("Failed to run {command}.",))?;
}
} else if build_frontend {
Expand Down
4 changes: 2 additions & 2 deletions src/dfx/src/lib/builders/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl CanisterBuilder for CustomBuilder {
#[context("Failed to build custom canister {}.", info.get_name())]
fn build(
&self,
_: &dyn Environment,
env: &dyn Environment,
pool: &CanisterPool,
info: &CanisterInfo,
config: &BuildConfig,
Expand Down Expand Up @@ -123,7 +123,7 @@ impl CanisterBuilder for CustomBuilder {
command
);

super::run_command(&command, &vars, info.get_workspace_root())
super::run_command(env, &command, &vars, info.get_workspace_root())
.with_context(|| format!("Failed to run {}.", command))?;
}

Expand Down
29 changes: 22 additions & 7 deletions src/dfx/src/lib/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::lib::environment::Environment;
use crate::lib::error::{BuildError, DfxError, DfxResult};
use crate::lib::models::canister::CanisterPool;
use crate::util::command::direct_or_shell_command;
use crate::util::with_suspend_all_spinners;
use anyhow::{bail, Context};
use candid::Principal as CanisterId;
use candid_parser::utils::CandidSource;
Expand Down Expand Up @@ -331,6 +332,7 @@ fn ensure_trailing_newline(s: String) -> String {
/// Execute a command and return its output bytes.
/// If the catch_output is false, the return bytes will always be empty.
pub fn execute_command(
env: &dyn Environment,
command: &str,
vars: &[Env<'_>],
cwd: &Path,
Expand All @@ -350,9 +352,12 @@ pub fn execute_command(
for (key, value) in vars {
cmd.env(key.as_ref(), value);
}
let output = cmd
.output()
.with_context(|| format!("Error executing custom build step {cmd:#?}"))?;
let output = if catch_output {
cmd.output()
} else {
with_suspend_all_spinners(env, || cmd.output())
}
.with_context(|| format!("Error executing custom build step {cmd:#?}"))?;
if output.status.success() {
Ok(output.stdout)
} else {
Expand All @@ -362,13 +367,23 @@ pub fn execute_command(
}
}

pub fn run_command(command: &str, vars: &[Env<'_>], cwd: &Path) -> DfxResult<()> {
execute_command(command, vars, cwd, false)?;
pub fn run_command(
env: &dyn Environment,
command: &str,
vars: &[Env<'_>],
cwd: &Path,
) -> DfxResult<()> {
execute_command(env, command, vars, cwd, false)?;
Ok(())
}

pub fn command_output(command: &str, vars: &[Env<'_>], cwd: &Path) -> DfxResult<Vec<u8>> {
execute_command(command, vars, cwd, true)
pub fn command_output(
env: &dyn Environment,
command: &str,
vars: &[Env<'_>],
cwd: &Path,
) -> DfxResult<Vec<u8>> {
execute_command(env, command, vars, cwd, true)
}

type Env<'a> = (Cow<'static, str>, Cow<'a, OsStr>);
Expand Down
16 changes: 9 additions & 7 deletions src/dfx/src/lib/metadata/dfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! The cli tool dfx should consolidate its usage of canister metadata into this single section
//! It's originally for pulling dependencies. But open to extend for other usage.
use crate::lib::{builders::command_output, error::DfxResult};
use crate::lib::{builders::command_output, environment::Environment, error::DfxResult};
use anyhow::{bail, Context};
use dfx_core::config::model::dfinity::{Pullable, TechStack, TechStackCategoryMap};
use schemars::JsonSchema;
Expand Down Expand Up @@ -39,15 +39,16 @@ impl DfxMetadata {

pub fn set_tech_stack(
&mut self,
env: &dyn Environment,
tech_stack_config: &TechStack,
project_root: &Path,
) -> DfxResult<()> {
let mut tech_stack = tech_stack_config.clone();
overwrite_field_from_command("cdk", tech_stack.cdk.as_mut(), project_root)?;
overwrite_field_from_command("language", tech_stack.language.as_mut(), project_root)?;
overwrite_field_from_command("lib", tech_stack.lib.as_mut(), project_root)?;
overwrite_field_from_command("tool", tech_stack.tool.as_mut(), project_root)?;
overwrite_field_from_command("other", tech_stack.other.as_mut(), project_root)?;
overwrite_field_from_command(env, "cdk", tech_stack.cdk.as_mut(), project_root)?;
overwrite_field_from_command(env, "language", tech_stack.language.as_mut(), project_root)?;
overwrite_field_from_command(env, "lib", tech_stack.lib.as_mut(), project_root)?;
overwrite_field_from_command(env, "tool", tech_stack.tool.as_mut(), project_root)?;
overwrite_field_from_command(env, "other", tech_stack.other.as_mut(), project_root)?;
self.tech_stack = Some(tech_stack);
Ok(())
}
Expand All @@ -56,6 +57,7 @@ impl DfxMetadata {
// If the value of a field starts with "$(", and ends with ")", then it's a command to calculate the value.
// The field value will be overwritten by the output of the command.
fn overwrite_field_from_command(
env: &dyn Environment,
category: &str,
category_map: Option<&mut TechStackCategoryMap>,
project_root: &Path,
Expand All @@ -66,7 +68,7 @@ fn overwrite_field_from_command(
if value.starts_with("$(") && value.ends_with(')') {
let triple = format!("{}->{}->{}", category, name, field);
let command = &value[2..value.len() - 1];
let bytes = command_output(command, &[], project_root)
let bytes = command_output(env, command, &[], project_root)
.with_context(|| format!("Failed to run the value_command: {}.", triple))?;
let calculated_value = String::from_utf8(bytes).with_context(|| {
format!(
Expand Down
9 changes: 5 additions & 4 deletions src/dfx/src/lib/models/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl Canister {
#[context("Failed to post-process wasm of canister '{}'.", self.info.get_name())]
pub(crate) fn wasm_post_process(
&self,
env: &dyn Environment,
logger: &Logger,
build_output: &BuildOutput,
) -> DfxResult {
Expand Down Expand Up @@ -187,7 +188,7 @@ impl Canister {

if let Some(tech_stack_config) = info.get_tech_stack() {
set_dfx_metadata = true;
dfx_metadata.set_tech_stack(tech_stack_config, info.get_workspace_root())?;
dfx_metadata.set_tech_stack(env, tech_stack_config, info.get_workspace_root())?;
} else if info.is_rust() {
// TODO: remove this when we have rust extension
set_dfx_metadata = true;
Expand All @@ -204,7 +205,7 @@ impl Canister {
}
}"#;
let tech_stack_config: TechStack = serde_json::from_str(s)?;
dfx_metadata.set_tech_stack(&tech_stack_config, info.get_workspace_root())?;
dfx_metadata.set_tech_stack(env, &tech_stack_config, info.get_workspace_root())?;
} else if info.is_motoko() {
// TODO: remove this when we have motoko extension
set_dfx_metadata = true;
Expand All @@ -214,7 +215,7 @@ impl Canister {
}
}"#;
let tech_stack_config: TechStack = serde_json::from_str(s)?;
dfx_metadata.set_tech_stack(&tech_stack_config, info.get_workspace_root())?;
dfx_metadata.set_tech_stack(env, &tech_stack_config, info.get_workspace_root())?;
}

if set_dfx_metadata {
Expand Down Expand Up @@ -727,7 +728,7 @@ impl CanisterPool {
) -> DfxResult<()> {
canister.candid_post_process(self.get_logger(), build_config, build_output)?;

canister.wasm_post_process(self.get_logger(), build_output)?;
canister.wasm_post_process(env, self.get_logger(), build_output)?;

build_canister_js(&canister.canister_id(), &canister.info)?;

Expand Down
5 changes: 4 additions & 1 deletion src/dfx/src/lib/operations/canister/install_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::util::assets::wallet_wasm;
use crate::util::clap::install_mode::InstallModeHint;
use crate::util::{
ask_for_consent, blob_from_arguments, get_candid_init_type, read_module_metadata,
with_suspend_all_spinners,
};
use anyhow::{anyhow, bail, Context};
use backoff::backoff::Backoff;
Expand Down Expand Up @@ -477,6 +478,7 @@ fn run_customized_install_tasks(
};
for task in tasks {
run_customized_install_task(
env,
canister,
task,
is_pre_install,
Expand All @@ -491,6 +493,7 @@ fn run_customized_install_tasks(

#[context("Failed to run {}-install task {}", if is_pre_install { "pre" } else { "post" }, task)]
fn run_customized_install_task(
env: &dyn Environment,
canister: &CanisterInfo,
task: &str,
is_pre_install: bool,
Expand Down Expand Up @@ -519,7 +522,7 @@ fn run_customized_install_task(
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

let status = command.status()?;
let status = with_suspend_all_spinners(env, || command.status())?;
if !status.success() {
match status.code() {
Some(code) => {
Expand Down

0 comments on commit cef5f92

Please sign in to comment.