Skip to content

Commit

Permalink
Add unstable --print-command-list flag
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Feb 28, 2023
1 parent 997d21b commit 224536d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Add unstable `--print-command-list` flag. ([#175](https://github.com/taiki-e/cargo-hack/pull/175))

## [0.5.27] - 2023-01-25

- Update `toml_edit` to 0.18.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ OPTIONS:
--keep-going
Keep going on failure.

--print-command-list
Print commands without run (Unstable).

--no-manifest-path
Do not pass --manifest-path option to cargo (Unstable).

Expand Down
8 changes: 7 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub(crate) struct Args {
pub(crate) clean_per_version: bool,
/// --keep-going
pub(crate) keep_going: bool,
/// --print-command-list
pub(crate) print_command_list: bool,
/// --version-range
pub(crate) version_range: Option<String>,
/// --version-step
Expand All @@ -55,7 +57,7 @@ pub(crate) struct Args {
// options for --each-feature and --feature-powerset
/// --optional-deps [DEPS]...
pub(crate) optional_deps: Option<Vec<String>>,
/// --include-features
/// --include-features <FEATURES>...
pub(crate) include_features: Vec<Feature>,
/// --include-deps-features
pub(crate) include_deps_features: bool,
Expand Down Expand Up @@ -139,6 +141,7 @@ impl Args {
let mut clean_per_run = false;
let mut clean_per_version = false;
let mut keep_going = false;
let mut print_command_list = false;
let mut no_manifest_path = false;
let mut version_range = None;
let mut version_step = None;
Expand Down Expand Up @@ -278,6 +281,7 @@ impl Args {
Long("clean-per-run") => parse_flag!(clean_per_run),
Long("clean-per-version") => parse_flag!(clean_per_version),
Long("keep-going") => parse_flag!(keep_going),
Long("print-command-list") => parse_flag!(print_command_list),
Long("no-manifest-path") => parse_flag!(no_manifest_path),
Long("ignore-unknown-features") => parse_flag!(ignore_unknown_features),
Short('v') | Long("verbose") => verbose += 1,
Expand Down Expand Up @@ -555,6 +559,7 @@ impl Args {
clean_per_run,
clean_per_version,
keep_going,
print_command_list,
no_manifest_path,
include_features: include_features.into_iter().map(Into::into).collect(),
include_deps_features,
Expand Down Expand Up @@ -717,6 +722,7 @@ const HELP: &[HelpText<'_>] = &[
"This flag can only be used together with --version-range flag.",
]),
("", "--keep-going", "", "Keep going on failure", &[]),
("", "--print-command-list", "", "Print commands without run (Unstable)", &[]),
("", "--no-manifest-path", "", "Do not pass --manifest-path option to cargo (Unstable)", &[]),
("-v", "--verbose", "", "Use verbose output", &[]),
("", "--color", "<WHEN>", "Coloring: auto, always, never", &[
Expand Down
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ fn exec_cargo_inner(
line: &mut ProcessBuilder<'_>,
progress: &mut Progress,
) -> Result<()> {
if progress.count != 0 {
if progress.count != 0 && !cx.print_command_list {
eprintln!();
}
progress.count += 1;
Expand All @@ -503,6 +503,11 @@ fn exec_cargo_inner(
cargo_clean(cx, Some(id))?;
}

if cx.print_command_list {
print_command(line.clone());
return Ok(());
}

// running `<command>` (on <package>) (<count>/<total>)
let mut msg = String::new();
if term::verbose() {
Expand All @@ -524,10 +529,22 @@ fn cargo_clean(cx: &Context, id: Option<&PackageId>) -> Result<()> {
line.arg(&cx.packages(id).name);
}

if cx.print_command_list {
print_command(line);
return Ok(());
}

if term::verbose() {
// running `cargo clean [--package <package>]`
info!("running {line}");
}

line.run()
}

fn print_command(mut line: ProcessBuilder<'_>) {
let _guard = term::verbose::scoped(true);
line.strip_program_path = true;
let l = line.to_string();
println!("{}", &l[1..l.len() - 1]);
}
4 changes: 3 additions & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(crate) struct ProcessBuilder<'a> {
/// This list always has a trailing comma if it is not empty.
// cargo less than Rust 1.38 cannot handle multiple '--features' flags, so it creates another String.
features: String,
pub(crate) strip_program_path: bool,
}

impl<'a> ProcessBuilder<'a> {
Expand All @@ -56,6 +57,7 @@ impl<'a> ProcessBuilder<'a> {
leading_args: Vec::new(),
args: Vec::new(),
features: String::new(),
strip_program_path: false,
}
}

Expand Down Expand Up @@ -185,7 +187,7 @@ impl fmt::Display for ProcessBuilder<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "`")?;

if f.alternate() || term::verbose() {
if !self.strip_program_path && (f.alternate() || term::verbose()) {
write!(f, "{}", self.program.to_string_lossy())?;
} else {
write!(f, "{}", Path::new(&*self.program).file_stem().unwrap().to_string_lossy())?;
Expand Down
3 changes: 3 additions & 0 deletions tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ OPTIONS:
--keep-going
Keep going on failure.

--print-command-list
Print commands without run (Unstable).

--no-manifest-path
Do not pass --manifest-path option to cargo (Unstable).

Expand Down
1 change: 1 addition & 0 deletions tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ OPTIONS:
command
--clean-per-version Remove artifacts per Rust version
--keep-going Keep going on failure
--print-command-list Print commands without run (Unstable)
--no-manifest-path Do not pass --manifest-path option to cargo (Unstable)
-v, --verbose Use verbose output
--color <WHEN> Coloring: auto, always, never
Expand Down
17 changes: 17 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,3 +1461,20 @@ fn empty_string() {
.assert_success("real")
.stderr_not_contains("not found");
}

#[test]
fn print_command_list() {
cargo_hack(["check", "--each-feature", "--print-command-list"])
.assert_success("real")
.stdout_contains(
"
cargo check --manifest-path Cargo.toml --no-default-features
cargo check --manifest-path Cargo.toml --no-default-features --features a
cargo check --manifest-path Cargo.toml --no-default-features --features b
cargo check --manifest-path Cargo.toml --no-default-features --features c
cargo check --manifest-path Cargo.toml --no-default-features --features default
cargo check --manifest-path Cargo.toml --no-default-features --all-features
",
)
.stdout_not_contains("`");
}

0 comments on commit 224536d

Please sign in to comment.