Skip to content

Commit 35f2147

Browse files
feat(cli): Improve CLI logging (#4060)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent d4b49d7 commit 35f2147

File tree

31 files changed

+449
-534
lines changed

31 files changed

+449
-534
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": major
3+
---
4+
5+
Remove `Settings::verbose` option. You may now bring your own `log` frontend to receive logging output from the bundler while remaining in control of verbosity and formatting.

.changes/cli-improved-logging.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": minor
3+
"cli.js": minor
4+
---
5+
6+
Improve CLI's logging output, making use of the standard rust `log` system.

tooling/bundler/Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,20 @@ exclude = [
2323

2424
[dependencies]
2525
tauri-utils = { version = "1.0.0-rc.5", path = "../../core/tauri-utils", features = [ "resources" ] }
26-
ar = "0.9.0"
2726
icns = "0.3"
2827
image = "0.24.2"
2928
libflate = "1.2"
30-
md5 = "0.7.0"
3129
anyhow = "1.0"
3230
thiserror = "1.0"
3331
serde_json = "1.0"
3432
serde = { version = "1.0", features = [ "derive" ] }
3533
strsim = "0.10.0"
3634
tar = "0.4.38"
37-
termcolor = "1.1.3"
3835
toml = "0.5.9"
3936
walkdir = "2"
40-
handlebars = { version = "4.2" }
41-
zip = { version = "0.6" }
37+
handlebars = "4.2"
4238
tempfile = "3.3.0"
43-
os_pipe = "1"
39+
log = { version = "0.4.17", features = ["kv_unstable"]}
4440

4541
[target."cfg(target_os = \"windows\")".dependencies]
4642
attohttpc = "0.19"
@@ -50,6 +46,7 @@ winreg = "0.10"
5046
sha2 = "0.10"
5147
hex = "0.4"
5248
glob = "0.3"
49+
zip = "0.6"
5350

5451
[target."cfg(target_os = \"macos\")".dependencies]
5552
time = { version = "0.3", features = [ "formatting" ] }
@@ -60,6 +57,8 @@ regex = "1"
6057

6158
[target."cfg(target_os = \"linux\")".dependencies]
6259
heck = "0.4"
60+
ar = "0.9.0"
61+
md5 = "0.7.0"
6362

6463
[lib]
6564
name = "tauri_bundler"

tooling/bundler/src/bundle.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ pub use self::{
2222
Settings, SettingsBuilder, UpdaterSettings,
2323
},
2424
};
25+
use log::{info, warn};
2526
pub use settings::{WindowsSettings, WixLanguage, WixLanguageConfig, WixSettings};
2627

27-
use common::{print_finished, print_info};
28-
2928
use std::path::PathBuf;
3029

3130
/// Generated bundle metadata.
@@ -63,7 +62,7 @@ pub fn bundle_project(settings: Settings) -> crate::Result<Vec<Bundle>> {
6362
// updater is dependant of multiple bundle, we send our bundles to prevent rebuilding
6463
PackageType::Updater => updater_bundle::bundle_project(&settings, &bundles)?,
6564
_ => {
66-
print_info(&format!("ignoring {:?}", package_type))?;
65+
warn!("ignoring {:?}", package_type);
6766
continue;
6867
}
6968
};
@@ -74,7 +73,24 @@ pub fn bundle_project(settings: Settings) -> crate::Result<Vec<Bundle>> {
7473
});
7574
}
7675

77-
print_finished(&bundles)?;
76+
let pluralised = if bundles.len() == 1 {
77+
"bundle"
78+
} else {
79+
"bundles"
80+
};
81+
82+
let mut printable_paths = String::new();
83+
for bundle in &bundles {
84+
for path in &bundle.bundle_paths {
85+
let mut note = "";
86+
if bundle.package_type == crate::PackageType::Updater {
87+
note = " (updater)";
88+
}
89+
printable_paths.push_str(&format!(" {}{}\n", path.display(), note));
90+
}
91+
}
92+
93+
info!(action = "Finished"; "{} {} at:\n{}", bundles.len(), pluralised, printable_paths);
7894

7995
Ok(bundles)
8096
}

tooling/bundler/src/bundle/common.rs

Lines changed: 25 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use crate::{CommandExt, Settings};
5+
use log::debug;
6+
67
use std::{
78
ffi::OsStr,
89
fs::{self, File},
9-
io::{self, BufWriter, Write},
10+
io::{self, BufWriter},
1011
path::Path,
11-
process::{Command, Stdio},
12+
process::{Command, Output},
1213
};
13-
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
1414

1515
/// Returns true if the path has a filename indicating that it is a high-desity
1616
/// "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<()> {
133133
Ok(())
134134
}
135135

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>;
173138
}
174139

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)));
186143

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()?;
197145

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+
}
205154

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+
}
210162
}
211163
}
212164

tooling/bundler/src/bundle/linux/appimage.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// SPDX-License-Identifier: MIT
44

55
use super::{
6-
super::{common, path_utils},
6+
super::{common::CommandExt, path_utils},
77
debian,
88
};
99
use crate::Settings;
10-
10+
use anyhow::Context;
1111
use handlebars::Handlebars;
12-
12+
use log::info;
1313
use std::{
1414
collections::BTreeMap,
1515
fs::{remove_dir_all, write},
@@ -76,7 +76,9 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
7676

7777
// create the shell script file in the target/ folder.
7878
let sh_file = output_path.join("build_appimage.sh");
79-
common::print_bundling(appimage_path.file_name().unwrap().to_str().unwrap())?;
79+
80+
info!(action = "Bundling"; "{} ({})", appimage_filename, appimage_path.display());
81+
8082
write(&sh_file, temp)?;
8183

8284
// chmod script for execution
@@ -90,19 +92,10 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
9092
.expect("Failed to chmod script");
9193

9294
// execute the shell script to build the appimage.
93-
let mut cmd = Command::new(&sh_file);
94-
cmd.current_dir(output_path);
95-
96-
common::execute_with_verbosity(&mut cmd, settings).map_err(|_| {
97-
crate::Error::ShellScriptError(format!(
98-
"error running appimage.sh{}",
99-
if settings.is_verbose() {
100-
""
101-
} else {
102-
", try running with --verbose to see command output"
103-
}
104-
))
105-
})?;
95+
Command::new(&sh_file)
96+
.current_dir(output_path)
97+
.output_ok()
98+
.context("error running appimage.sh")?;
10699

107100
remove_dir_all(&package_dir)?;
108101
Ok(vec![appimage_path])

tooling/bundler/src/bundle/linux/debian.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
use super::super::common;
2626
use crate::Settings;
27-
2827
use anyhow::Context;
2928
use heck::ToKebabCase;
3029
use image::{self, codecs::png::PngDecoder, GenericImageView, ImageDecoder};
3130
use libflate::gzip;
31+
use log::info;
3232
use walkdir::WalkDir;
3333

3434
use std::{
@@ -64,14 +64,16 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
6464
arch
6565
);
6666
let package_name = format!("{}.deb", package_base_name);
67-
common::print_bundling(&package_name)?;
67+
6868
let base_dir = settings.project_out_directory().join("bundle/deb");
6969
let package_dir = base_dir.join(&package_base_name);
7070
if package_dir.exists() {
7171
fs::remove_dir_all(&package_dir)
7272
.with_context(|| format!("Failed to remove old {}", package_base_name))?;
7373
}
74-
let package_path = base_dir.join(package_name);
74+
let package_path = base_dir.join(&package_name);
75+
76+
info!(action = "Bundling"; "{} ({})", package_name, package_path.display());
7577

7678
let (data_dir, _) = generate_data(settings, &package_dir)
7779
.with_context(|| "Failed to build data folders and files")?;

0 commit comments

Comments
 (0)