From a5e7249178cf0a1aec94a96eb0854ed6cd459294 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 2 Jul 2020 18:37:13 -0300 Subject: [PATCH] chore(bundler) show scripts output (#728) --- cli/tauri-bundler/src/bundle.rs | 1 - .../src/bundle/appimage_bundle.rs | 21 +++------ cli/tauri-bundler/src/bundle/common.rs | 26 ++++++++++- cli/tauri-bundler/src/bundle/dmg_bundle.rs | 20 ++++---- .../src/bundle/templates/appimage | 2 + cli/tauri-bundler/src/bundle/wix.rs | 46 ++++--------------- 6 files changed, 51 insertions(+), 65 deletions(-) diff --git a/cli/tauri-bundler/src/bundle.rs b/cli/tauri-bundler/src/bundle.rs index 065af9061..bf341e2b8 100644 --- a/cli/tauri-bundler/src/bundle.rs +++ b/cli/tauri-bundler/src/bundle.rs @@ -15,7 +15,6 @@ mod tauri_config; #[cfg(target_os = "windows")] mod wix; -#[cfg(target_os = "windows")] pub use self::common::print_info; pub use self::common::{print_error, print_finished}; pub use self::settings::{PackageType, Settings}; diff --git a/cli/tauri-bundler/src/bundle/appimage_bundle.rs b/cli/tauri-bundler/src/bundle/appimage_bundle.rs index 2833347bf..e9b00bbaf 100644 --- a/cli/tauri-bundler/src/bundle/appimage_bundle.rs +++ b/cli/tauri-bundler/src/bundle/appimage_bundle.rs @@ -88,19 +88,12 @@ pub fn bundle_project(settings: &Settings) -> crate::Result> { .expect("Failed to chmod script"); // execute the shell script to build the appimage. - let status = Command::new(&sh_file) - .current_dir(output_path) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .status() - .expect("Failed to execute shell script"); + let mut cmd = Command::new(&sh_file); + cmd.current_dir(output_path); - if !status.success() { - Err(crate::Error::ShellScriptError( - "error running build_appimage.sh".to_owned(), - )) - } else { - remove_dir_all(&package_dir)?; - Ok(vec![appimage_path]) - } + common::execute_with_output(&mut cmd) + .map_err(|_| crate::Error::ShellScriptError("error running build_appimage.sh".to_owned()))?; + + remove_dir_all(&package_dir)?; + Ok(vec![appimage_path]) } diff --git a/cli/tauri-bundler/src/bundle/common.rs b/cli/tauri-bundler/src/bundle/common.rs index 91bb1cd39..a7255066b 100644 --- a/cli/tauri-bundler/src/bundle/common.rs +++ b/cli/tauri-bundler/src/bundle/common.rs @@ -1,8 +1,9 @@ use std; use std::ffi::OsStr; use std::fs::{self, File}; -use std::io::{self, BufWriter, Write}; +use std::io::{self, BufRead, BufReader, BufWriter, Write}; use std::path::{Component, Path, PathBuf}; +use std::process::{Command, Stdio}; use term; use walkdir; @@ -214,7 +215,6 @@ pub fn print_warning(message: &str) -> crate::Result<()> { } /// Prints a Info message to stderr. -#[cfg(windows)] pub fn print_info(message: &str) -> crate::Result<()> { if let Some(mut output) = term::stderr() { safe_term_attr(&mut output, term::Attr::Bold)?; @@ -267,6 +267,28 @@ pub fn print_error(error: &anyhow::Error) -> crate::Result<()> { } } +pub fn execute_with_output(cmd: &mut Command) -> crate::Result<()> { + let mut child = cmd + .stdout(Stdio::piped()) + .spawn() + .expect("failed to spawn command"); + { + let stdout = child.stdout.as_mut().expect("Failed to get stdout handle"); + let reader = BufReader::new(stdout); + + for line in reader.lines() { + print_info(line.expect("Failed to get line").as_str())?; + } + } + + let status = child.wait()?; + if status.success() { + Ok(()) + } else { + Err(anyhow::anyhow!("command failed").into()) + } +} + #[cfg(test)] mod tests { use super::{copy_dir, create_file, is_retina, resource_relpath, symlink_file}; diff --git a/cli/tauri-bundler/src/bundle/dmg_bundle.rs b/cli/tauri-bundler/src/bundle/dmg_bundle.rs index 085638c8b..a5a505250 100644 --- a/cli/tauri-bundler/src/bundle/dmg_bundle.rs +++ b/cli/tauri-bundler/src/bundle/dmg_bundle.rs @@ -105,19 +105,15 @@ pub fn bundle_project(settings: &Settings) -> crate::Result> { } // execute the bundle script - let status = Command::new(&bundle_script_path) + let mut cmd = Command::new(&bundle_script_path); + cmd .current_dir(bundle_dir.clone()) .args(args) - .args(vec![dmg_name.as_str(), bundle_name.as_str()]) - .status() - .expect("Failed to execute shell script"); + .args(vec![dmg_name.as_str(), bundle_name.as_str()]); - if !status.success() { - Err(crate::Error::ShellScriptError( - "error running bundle_dmg.sh".to_owned(), - )) - } else { - fs::rename(bundle_dir.join(dmg_name.clone()), dmg_path.clone())?; - Ok(vec![bundle_path, dmg_path]) - } + common::execute_with_output(&mut cmd) + .map_err(|_| crate::Error::ShellScriptError("error running bundle_dmg.sh".to_owned()))?; + + fs::rename(bundle_dir.join(dmg_name.clone()), dmg_path.clone())?; + Ok(vec![bundle_path, dmg_path]) } diff --git a/cli/tauri-bundler/src/bundle/templates/appimage b/cli/tauri-bundler/src/bundle/templates/appimage index 82745caf7..99f99fc2a 100644 --- a/cli/tauri-bundler/src/bundle/templates/appimage +++ b/cli/tauri-bundler/src/bundle/templates/appimage @@ -1,5 +1,7 @@ #!/bin/bash +set -e + mkdir -p {{app_name}}.AppDir cp -r ../deb/{{bundle_name}}/data/usr {{app_name}}.AppDir diff --git a/cli/tauri-bundler/src/bundle/wix.rs b/cli/tauri-bundler/src/bundle/wix.rs index c924edfcd..306d5ea6a 100644 --- a/cli/tauri-bundler/src/bundle/wix.rs +++ b/cli/tauri-bundler/src/bundle/wix.rs @@ -342,27 +342,13 @@ fn run_candle( let candle_exe = wix_toolset_path.join("candle.exe"); common::print_info(format!("running candle for {}", wxs_file_name).as_str())?; - let mut cmd = Command::new(&candle_exe) + let mut cmd = Command::new(&candle_exe); + cmd .args(&args) .stdout(Stdio::piped()) - .current_dir(build_path) - .spawn() - .expect("error running candle.exe"); - { - let stdout = cmd.stdout.as_mut().expect("Failed to get stdout handle"); - let reader = BufReader::new(stdout); + .current_dir(build_path); - for line in reader.lines() { - common::print_info(line.expect("Failed to get line").as_str())?; - } - } - - let status = cmd.wait()?; - if status.success() { - Ok(()) - } else { - Err(crate::Error::CandleError) - } + common::execute_with_output(&mut cmd).map_err(|_| crate::Error::CandleError) } /// Runs the Light.exe file. Light takes the generated code from Candle and produces an MSI Installer. @@ -387,27 +373,15 @@ fn run_light( common::print_info(format!("running light to produce {}", output_path.display()).as_str())?; - let mut cmd = Command::new(&light_exe) + let mut cmd = Command::new(&light_exe); + cmd .args(&args) .stdout(Stdio::piped()) - .current_dir(build_path) - .spawn() - .expect("error running light.exe"); - { - let stdout = cmd.stdout.as_mut().expect("Failed to get stdout handle"); - let reader = BufReader::new(stdout); + .current_dir(build_path); - for line in reader.lines() { - common::print_info(line.expect("Failed to get line").as_str())?; - } - } - - let status = cmd.wait()?; - if status.success() { - Ok(output_path.to_path_buf()) - } else { - Err(crate::Error::LightError) - } + common::execute_with_output(&mut cmd) + .map(|_| output_path.to_path_buf()) + .map_err(|_| crate::Error::LightError) } // fn get_icon_data() -> crate::Result<()> {