|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | | -use crate::{CommandExt, Settings}; |
| 5 | +use log::debug; |
| 6 | + |
6 | 7 | use std::{ |
7 | 8 | ffi::OsStr, |
8 | 9 | fs::{self, File}, |
9 | | - io::{self, BufWriter, Write}, |
| 10 | + io::{self, BufWriter}, |
10 | 11 | path::Path, |
11 | | - process::{Command, Stdio}, |
| 12 | + process::{Command, Output}, |
12 | 13 | }; |
13 | | -use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; |
14 | 14 |
|
15 | 15 | /// Returns true if the path has a filename indicating that it is a high-desity |
16 | 16 | /// "retina" icon. Specifically, returns true the the file stem ends with |
@@ -133,80 +133,32 @@ pub fn copy_dir(from: &Path, to: &Path) -> crate::Result<()> { |
133 | 133 | Ok(()) |
134 | 134 | } |
135 | 135 |
|
136 | | -/// Prints a message to stderr, in the same format that `cargo` uses, |
137 | | -/// indicating that we are creating a bundle with the given filename. |
138 | | -pub fn print_bundling(filename: &str) -> crate::Result<()> { |
139 | | - print_progress("Bundling", filename) |
140 | | -} |
141 | | - |
142 | | -/// Prints a message to stderr, in the same format that `cargo` uses, |
143 | | -/// indicating that we have finished the the given bundles. |
144 | | -pub fn print_finished(bundles: &[crate::bundle::Bundle]) -> crate::Result<()> { |
145 | | - let pluralised = if bundles.len() == 1 { |
146 | | - "bundle" |
147 | | - } else { |
148 | | - "bundles" |
149 | | - }; |
150 | | - let msg = format!("{} {} at:", bundles.len(), pluralised); |
151 | | - print_progress("Finished", &msg)?; |
152 | | - for bundle in bundles { |
153 | | - for path in &bundle.bundle_paths { |
154 | | - let mut note = ""; |
155 | | - if bundle.package_type == crate::PackageType::Updater { |
156 | | - note = " (updater)"; |
157 | | - } |
158 | | - println!(" {}{}", path.display(), note,); |
159 | | - } |
160 | | - } |
161 | | - Ok(()) |
162 | | -} |
163 | | - |
164 | | -/// Prints a formatted bundle progress to stderr. |
165 | | -fn print_progress(step: &str, msg: &str) -> crate::Result<()> { |
166 | | - let mut output = StandardStream::stderr(ColorChoice::Always); |
167 | | - let _ = output.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)); |
168 | | - write!(output, " {}", step)?; |
169 | | - output.reset()?; |
170 | | - writeln!(output, " {}", msg)?; |
171 | | - output.flush()?; |
172 | | - Ok(()) |
| 136 | +pub trait CommandExt { |
| 137 | + fn output_ok(&mut self) -> crate::Result<Output>; |
173 | 138 | } |
174 | 139 |
|
175 | | -/// Prints a warning message to stderr, in the same format that `cargo` uses. |
176 | | -#[allow(dead_code)] |
177 | | -pub fn print_warning(message: &str) -> crate::Result<()> { |
178 | | - let mut output = StandardStream::stderr(ColorChoice::Always); |
179 | | - let _ = output.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true)); |
180 | | - write!(output, "warning:")?; |
181 | | - output.reset()?; |
182 | | - writeln!(output, " {}", message)?; |
183 | | - output.flush()?; |
184 | | - Ok(()) |
185 | | -} |
| 140 | +impl CommandExt for Command { |
| 141 | + fn output_ok(&mut self) -> crate::Result<Output> { |
| 142 | + debug!(action = "Running"; "Command `{} {}`", self.get_program().to_string_lossy(), self.get_args().map(|arg| arg.to_string_lossy()).fold(String::new(), |acc, arg| format!("{} {}", acc, arg))); |
186 | 143 |
|
187 | | -/// Prints a Info message to stderr. |
188 | | -pub fn print_info(message: &str) -> crate::Result<()> { |
189 | | - let mut output = StandardStream::stderr(ColorChoice::Always); |
190 | | - let _ = output.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)); |
191 | | - write!(output, "info:")?; |
192 | | - output.reset()?; |
193 | | - writeln!(output, " {}", message)?; |
194 | | - output.flush()?; |
195 | | - Ok(()) |
196 | | -} |
| 144 | + let output = self.output()?; |
197 | 145 |
|
198 | | -pub fn execute_with_verbosity(cmd: &mut Command, settings: &Settings) -> crate::Result<()> { |
199 | | - if settings.is_verbose() { |
200 | | - cmd.pipe()?; |
201 | | - } else { |
202 | | - cmd.stdout(Stdio::null()).stderr(Stdio::null()); |
203 | | - } |
204 | | - let status = cmd.status().expect("failed to spawn command"); |
| 146 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 147 | + if !stdout.is_empty() { |
| 148 | + debug!("Stdout: {}", stdout); |
| 149 | + } |
| 150 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 151 | + if !stderr.is_empty() { |
| 152 | + debug!("Stderr: {}", stderr); |
| 153 | + } |
205 | 154 |
|
206 | | - if status.success() { |
207 | | - Ok(()) |
208 | | - } else { |
209 | | - Err(anyhow::anyhow!("command failed").into()) |
| 155 | + if output.status.success() { |
| 156 | + Ok(output) |
| 157 | + } else { |
| 158 | + Err(crate::Error::GenericError( |
| 159 | + String::from_utf8_lossy(&output.stderr).to_string(), |
| 160 | + )) |
| 161 | + } |
210 | 162 | } |
211 | 163 | } |
212 | 164 |
|
|
0 commit comments