Skip to content

Commit

Permalink
pretty command err output + fix build process
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Nowak-Liebiediew committed Aug 8, 2023
1 parent 0a6de4f commit 14ed7b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
30 changes: 5 additions & 25 deletions extensions-utils/src/dependencies/call.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::anyhow;
use anyhow::Context;
use fn_error_context::context;
use std::{
ffi::OsStr,
Expand Down Expand Up @@ -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<String, anyhow::Error> {
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())
}
26 changes: 5 additions & 21 deletions extensions-utils/src/dependencies/dfx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::dfx_executable::DfxError;
use anyhow::anyhow;
use anyhow::Context;
use fn_error_context::context;
use semver::Version;

Expand Down Expand Up @@ -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<String, DfxError> {
Expand Down
26 changes: 22 additions & 4 deletions extensions/nns/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::PathBuf;

const REPLICA_REV: &str = "90e2799c255733409d0e61682685afcc2431c928";

Expand All @@ -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();
}
}
25 changes: 21 additions & 4 deletions extensions/sns/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::PathBuf;

const REPLICA_REV: &str = "90e2799c255733409d0e61682685afcc2431c928";

Expand All @@ -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();
}
}

0 comments on commit 14ed7b3

Please sign in to comment.