Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print underlying cargo command on --verbose #13

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ serde = "1.0.194"
serde_derive = "1.0.194"
serde_json = "1.0.110"
serde_path_to_error = "0.1"
shlex = "1.3"
termcolor = "1.4"
toml = "0.8"
34 changes: 33 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ mod metadata;
mod parser;

use crate::metadata::{DocumentationOptions, Metadata};
use crate::parser::{Doc, Subcommand};
use crate::parser::{Coloring, Doc, Subcommand};
use anyhow::{bail, Context as _, Result};
use clap::{CommandFactory as _, Parser as _, ValueEnum as _};
use std::borrow::Cow;
use std::collections::BTreeMap as Map;
use std::env;
use std::ffi::OsStr;
use std::io::{self, Write as _};
use std::iter;
use std::mem;
use std::process::{self, Command, Stdio};
use termcolor::{Color::Green, ColorChoice, ColorSpec, StandardStream, WriteColor as _};

cargo_subcommand_metadata::description!("Imitate the documentation build that docs.rs would do");

Expand Down Expand Up @@ -253,6 +257,11 @@ fn do_main() -> Result<()> {
cargo_rustdoc.env_remove("CARGO_ENCODED_RUSTFLAGS");
cargo_rustdoc.env_remove("CARGO_ENCODED_RUSTDOCFLAGS");

if args.verbose {
let color = args.color.unwrap_or(Coloring::Auto);
print_command(&cargo_rustdoc, color)?;
}

let status = cargo_rustdoc.status()?;
if !status.success() {
process::exit(status.code().unwrap_or(1));
Expand Down Expand Up @@ -287,3 +296,26 @@ fn propagate_common_args(cargo: &mut Command, args: &Doc) {
cargo.arg("--offline");
}
}

fn print_command(cmd: &Command, color: Coloring) -> Result<()> {
let cmd: Vec<Cow<str>> = iter::once(cmd.get_program().to_string_lossy())
.chain(cmd.get_args().map(OsStr::to_string_lossy))
.collect();

let shell_words = shlex::Quoter::new()
.allow_nul(true)
.join(cmd.iter().map(Cow::as_ref))?;

let color_choice = match color {
Coloring::Auto => ColorChoice::Auto,
Coloring::Always => ColorChoice::Always,
Coloring::Never => ColorChoice::Never,
};

let mut stream = StandardStream::stderr(color_choice);
let _ = stream.set_color(ColorSpec::new().set_bold(true).set_fg(Some(Green)));
let _ = write!(stream, "{:>12}", "Running");
let _ = stream.reset();
let _ = writeln!(stream, " `{}`", shell_words);
Ok(())
}
Loading