Skip to content

Commit

Permalink
feat(packager): always show command output, enhance formatting (#196)
Browse files Browse the repository at this point in the history
* feat(packager): always show command output, enhance formatting

* revert to debug! and use error! for stderr instead

* hide dates, level for event formatter

* Update .changes/enhance-shell-output.md

* always show shell output for beforePackaging commands

---------

Co-authored-by: amr-crabnebula <[email protected]>
  • Loading branch information
lucasfernog-crabnebula and amr-crabnebula authored Apr 17, 2024
1 parent cf76357 commit 1375380
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/enhance-shell-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cargo-packager": patch
"@crabnebula/packager": patch
---

Always show shell commands output for `beforePackageCommand` and `beforeEachPackagingCommand` .
3 changes: 3 additions & 0 deletions crates/packager/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ where

if !cli.quite {
init_tracing_subscriber(cli.verbose);
if std::env::var_os("CARGO_TERM_COLOR").is_none() {
std::env::set_var("CARGO_TERM_COLOR", "always");
}
}

run_cli(cli)
Expand Down
52 changes: 52 additions & 0 deletions crates/packager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,61 @@ pub fn init_tracing_subscriber(verbosity: u8) {
.with_line_number(tracing)
.with_file(tracing)
.with_max_level(level)
.event_format(TracingFormatter {
formatter: tracing_subscriber::fmt::format()
.compact()
.with_target(debug)
.with_line_number(tracing)
.without_time()
.with_file(tracing),
})
.init();
}

struct TracingFormatter {
formatter:
tracing_subscriber::fmt::format::Format<tracing_subscriber::fmt::format::Compact, ()>,
}

struct ShellFieldVisitor {
message: String,
}

impl tracing::field::Visit for ShellFieldVisitor {
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
if field.name() == "message" {
self.message = value.to_string();
}
}

fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
if field.name() == "message" {
self.message = format!("{value:?}");
}
}
}

impl<S, N> tracing_subscriber::fmt::FormatEvent<S, N> for TracingFormatter
where
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
N: for<'a> tracing_subscriber::fmt::FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
mut writer: tracing_subscriber::fmt::format::Writer<'_>,
event: &tracing::Event<'_>,
) -> std::fmt::Result {
if event.fields().any(|f| f.name() == "shell") {
let mut visitor = ShellFieldVisitor { message: "".into() };
event.record(&mut visitor);
writeln!(writer, "{}", visitor.message)
} else {
self.formatter.format_event(ctx, writer, event)
}
}
}

/// Sign the specified packages and return the signatures paths.
///
/// If `packages` contain a directory in the case of [`PackageFormat::App`]
Expand Down
4 changes: 2 additions & 2 deletions crates/packager/src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn run_before_each_packaging_command_hook(
let output = cmd
.env("CARGO_PACKAGER_FORMATS", formats_comma_separated)
.env("CARGO_PACKAGER_FORMAT", format)
.output_ok()
.output_ok_info()
.map_err(|e| {
crate::Error::HookCommandFailure(
"beforeEachPackageCommand".into(),
Expand Down Expand Up @@ -249,7 +249,7 @@ fn run_before_packaging_command_hook(
tracing::info!("Running beforePackageCommand `{script}`");
let output = cmd
.env("CARGO_PACKAGER_FORMATS", formats_comma_separated)
.output_ok()
.output_ok_info()
.map_err(|e| {
crate::Error::HookCommandFailure("beforePackagingCommand".into(), script.into(), e)
})?;
Expand Down
31 changes: 29 additions & 2 deletions crates/packager/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT

use std::{
borrow::Cow,
io::{BufRead, BufReader},
process::{Command, ExitStatus, Output, Stdio},
sync::{Arc, Mutex},
Expand All @@ -14,6 +15,8 @@ pub trait CommandExt {
// show the command output in the Node.js wrapper.
fn piped(&mut self) -> std::io::Result<ExitStatus>;
fn output_ok(&mut self) -> std::io::Result<Output>;
fn output_ok_info(&mut self) -> std::io::Result<Output>;
fn output_ok_inner(&mut self, level: tracing::Level) -> std::io::Result<Output>;
}

impl CommandExt for Command {
Expand All @@ -25,6 +28,14 @@ impl CommandExt for Command {
}

fn output_ok(&mut self) -> std::io::Result<Output> {
self.output_ok_inner(tracing::Level::DEBUG)
}

fn output_ok_info(&mut self) -> std::io::Result<Output> {
self.output_ok_inner(tracing::Level::INFO)
}

fn output_ok_inner(&mut self, level: tracing::Level) -> std::io::Result<Output> {
tracing::debug!("Running Command `{self:?}`");

self.stdout(Stdio::piped());
Expand All @@ -43,7 +54,11 @@ impl CommandExt for Command {
if let Ok(0) = stdout.read_until(b'\n', &mut buf) {
break;
}
tracing::debug!("{}", String::from_utf8_lossy(&buf[..buf.len() - 1]));
log(
level,
"stdout",
String::from_utf8_lossy(&buf[..buf.len() - 1]),
);
lines.extend(&buf);
}
});
Expand All @@ -59,7 +74,11 @@ impl CommandExt for Command {
if let Ok(0) = stderr.read_until(b'\n', &mut buf) {
break;
}
tracing::debug!("{}", String::from_utf8_lossy(&buf[..buf.len() - 1]));
log(
level,
"stderr",
String::from_utf8_lossy(&buf[..buf.len() - 1]),
);
lines.extend(&buf);
}
});
Expand All @@ -78,3 +97,11 @@ impl CommandExt for Command {
}
}
}

#[inline]
fn log(level: tracing::Level, shell: &str, msg: Cow<'_, str>) {
match level {
tracing::Level::INFO => tracing::info!(shell = shell, "{msg}"),
_ => tracing::debug!(shell = shell, "{msg}"),
}
}

0 comments on commit 1375380

Please sign in to comment.