From 14ed7b3b1d71b5e0d214b71af0415e9d2e027f24 Mon Sep 17 00:00:00 2001 From: Marcin Nowak-Liebiediew Date: Tue, 8 Aug 2023 15:14:32 +0200 Subject: [PATCH] pretty command err output + fix build process --- extensions-utils/src/dependencies/call.rs | 30 ++++------------------- extensions-utils/src/dependencies/dfx.rs | 26 ++++---------------- extensions/nns/build.rs | 26 +++++++++++++++++--- extensions/sns/build.rs | 25 ++++++++++++++++--- 4 files changed, 53 insertions(+), 54 deletions(-) diff --git a/extensions-utils/src/dependencies/call.rs b/extensions-utils/src/dependencies/call.rs index 74ca9e7..6257422 100644 --- a/extensions-utils/src/dependencies/call.rs +++ b/extensions-utils/src/dependencies/call.rs @@ -1,4 +1,4 @@ -use anyhow::anyhow; +use anyhow::Context; use fn_error_context::context; use std::{ ffi::OsStr, @@ -30,34 +30,14 @@ where ) })?; let binary_to_call = extension_dir_path.join(binary_name); - // TODO - dbg!(&binary_to_call); - std::fs::remove_file(binary_to_call.clone()).unwrap(); - panic!("trying to prettify command output, but something is not working the way I expect."); let mut command = Command::new(&binary_to_call); // If extension's dependency calls dfx; it should call dfx in this dir. command.env("PATH", dfx_cache_path.join("dfx")); command.args(args); - command + let output = command .stdin(process::Stdio::null()) .output() - .map_err(anyhow::Error::from) - .and_then(|output| -> Result { - if output.status.success() { - Ok(String::from_utf8_lossy(&output.stdout).into_owned()) - } else { - let args: Vec<_> = command - .get_args() - .into_iter() - .map(OsStr::to_string_lossy) - .collect(); - Err(anyhow!( - "Call failed:\n{:?} {}\nStdout:\n{}\n\nStderr:\n{}", - command.get_program(), - args.join(" "), - String::from_utf8_lossy(&output.stdout), - String::from_utf8_lossy(&output.stderr) - )) - } - }) + .with_context(|| format!("Error executing {:#?}", command))? + .stdout; + Ok(String::from_utf8_lossy(&output).to_string()) } diff --git a/extensions-utils/src/dependencies/dfx.rs b/extensions-utils/src/dependencies/dfx.rs index 992a576..da42b09 100644 --- a/extensions-utils/src/dependencies/dfx.rs +++ b/extensions-utils/src/dependencies/dfx.rs @@ -1,5 +1,5 @@ use crate::error::dfx_executable::DfxError; -use anyhow::anyhow; +use anyhow::Context; use fn_error_context::context; use semver::Version; @@ -27,28 +27,12 @@ where // If extension's dependency calls dfx; it should call dfx in this dir. command.env("PATH", dfx_cache_path.join("dfx")); command.args(args); - command + let output = command .stdin(process::Stdio::null()) .output() - .map_err(anyhow::Error::from) - .and_then(|output| { - if output.status.success() { - Ok(String::from_utf8_lossy(&output.stdout).into_owned()) - } else { - let args: Vec<_> = command - .get_args() - .into_iter() - .map(OsStr::to_string_lossy) - .collect(); - Err(anyhow!( - "Call failed:\n{:?} {}\nStdout:\n{}\n\nStderr:\n{}", - command.get_program(), - args.join(" "), - String::from_utf8_lossy(&output.stdout), - String::from_utf8_lossy(&output.stderr) - )) - } - }) + .with_context(|| format!("Error executing {:#?}", command))? + .stdout; + Ok(String::from_utf8_lossy(&output).to_string()) } pub fn replica_rev(dfx_cache_path: &Path) -> Result { diff --git a/extensions/nns/build.rs b/extensions/nns/build.rs index 43a2294..40d1e11 100644 --- a/extensions/nns/build.rs +++ b/extensions/nns/build.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::PathBuf; const REPLICA_REV: &str = "90e2799c255733409d0e61682685afcc2431c928"; @@ -10,9 +10,27 @@ const BINARY_DEPENDENCIES: &[(&str, &str)] = &[ ]; fn main() { - let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + // keep copy of the dependency in the root of the project, so that cargo-dist will be able to package it into a tarball + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + // and also in `target/debug` or `target/release` for development purposes (e.g. cargo run) + let target_dir = manifest_dir + .parent() + .unwrap() + .parent() + .unwrap() + .join("target") + .join(std::env::var("PROFILE").unwrap()); for (binary_name, renamed_binary_name) in BINARY_DEPENDENCIES { - let destination_path = Path::new(&manifest_dir).join(renamed_binary_name); - dfx_extensions_utils::download_ic_binary(REPLICA_REV, binary_name, &destination_path); + let destination_paths = ( + manifest_dir.join(renamed_binary_name), + target_dir.join(renamed_binary_name), + ); + dbg!(&destination_paths); + dfx_extensions_utils::download_ic_binary(REPLICA_REV, binary_name, &destination_paths.0); + if destination_paths.1.exists() { + std::fs::remove_file(&destination_paths.1).unwrap(); + } + std::fs::create_dir_all(destination_paths.1.parent().unwrap()).unwrap(); + std::fs::copy(destination_paths.0, destination_paths.1).unwrap(); } } diff --git a/extensions/sns/build.rs b/extensions/sns/build.rs index 5d8a598..086cdd1 100644 --- a/extensions/sns/build.rs +++ b/extensions/sns/build.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::PathBuf; const REPLICA_REV: &str = "90e2799c255733409d0e61682685afcc2431c928"; @@ -8,9 +8,26 @@ const BINARY_DEPENDENCIES: &[(&str, &str)] = &[ ]; fn main() { - let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + // keep copy of the dependency in the root of the project, so that cargo-dist will be able to package it into a tarball + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + // and also in `target/debug` or `target/release` for development purposes (e.g. cargo run) + let target_dir = manifest_dir + .parent() + .unwrap() + .parent() + .unwrap() + .join("target") + .join(std::env::var("PROFILE").unwrap()); for (binary_name, renamed_binary_name) in BINARY_DEPENDENCIES { - let destination_path = Path::new(&manifest_dir).join(renamed_binary_name); - dfx_extensions_utils::download_ic_binary(REPLICA_REV, binary_name, &destination_path); + let destination_paths = ( + manifest_dir.join(renamed_binary_name), + target_dir.join(renamed_binary_name), + ); + dbg!(&destination_paths); + dfx_extensions_utils::download_ic_binary(REPLICA_REV, binary_name, &destination_paths.0); + if destination_paths.1.exists() { + std::fs::remove_file(&destination_paths.1).unwrap(); + } + std::fs::copy(destination_paths.0, destination_paths.1).unwrap(); } }