Skip to content

Commit 5533a45

Browse files
committed
tasks: add package filter so can run on a subset of crates
Match cargo's package filtering interface.
1 parent ae24339 commit 5533a45

File tree

6 files changed

+54
-26
lines changed

6 files changed

+54
-26
lines changed

tasks/src/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::toolchain::{check_toolchain, Toolchain};
66
use xshell::Shell;
77

88
/// Run benchmark tests for all crates in the workspace.
9-
pub fn run(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
9+
pub fn run(sh: &Shell, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> {
1010
check_toolchain(sh, Toolchain::Nightly)?;
1111

12-
let crate_dirs = get_crate_dirs(sh)?;
12+
let crate_dirs = get_crate_dirs(sh, packages)?;
1313

1414
quiet_println(&format!(
1515
"Running bench tests for {} crates",

tasks/src/docs.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ use xshell::Shell;
88
///
99
/// This verifies that `cargo doc` works correctly for users with stable Rust.
1010
/// Uses basic rustdoc warnings to catch common documentation issues.
11-
pub fn run(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
11+
pub fn run(sh: &Shell, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> {
1212
check_toolchain(sh, Toolchain::Stable)?;
1313

14-
quiet_cmd!(sh, "cargo doc --all-features")
15-
.env("RUSTDOCFLAGS", "-D warnings")
16-
.run()?;
14+
let mut cmd = quiet_cmd!(sh, "cargo doc --all-features");
15+
16+
// Add package filters if specified.
17+
for package in packages {
18+
cmd = cmd.args(&["-p", package]);
19+
}
20+
21+
cmd.env("RUSTDOCFLAGS", "-D warnings").run()?;
1722

1823
Ok(())
1924
}
@@ -22,15 +27,21 @@ pub fn run(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
2227
///
2328
/// This emulates the docs.rs build environment by using the nightly toolchain
2429
/// with `--cfg docsrs` enabled. This catches docs.rs-specific issues.
25-
pub fn run_docsrs(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
30+
pub fn run_docsrs(sh: &Shell, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> {
2631
check_toolchain(sh, Toolchain::Nightly)?;
2732

28-
quiet_cmd!(sh, "cargo doc --all-features")
29-
.env(
30-
"RUSTDOCFLAGS",
31-
"--cfg docsrs -D warnings -D rustdoc::broken-intra-doc-links",
32-
)
33-
.run()?;
33+
let mut cmd = quiet_cmd!(sh, "cargo doc --all-features");
34+
35+
// Add package filters if specified.
36+
for package in packages {
37+
cmd = cmd.args(&["-p", package]);
38+
}
39+
40+
cmd.env(
41+
"RUSTDOCFLAGS",
42+
"--cfg docsrs -D warnings -D rustdoc::broken-intra-doc-links",
43+
)
44+
.run()?;
3445

3546
Ok(())
3647
}

tasks/src/environment.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ pub fn change_to_repo_root(sh: &Shell) {
5959

6060
/// Get list of crate directories in the workspace using cargo metadata.
6161
/// Returns fully qualified paths to support various workspace layouts including nested crates.
62-
pub fn get_crate_dirs(sh: &Shell) -> Result<Vec<String>, Box<dyn std::error::Error>> {
62+
///
63+
/// # Arguments
64+
///
65+
/// * `packages` - Optional filter for specific package names. If empty, returns all packages.
66+
pub fn get_crate_dirs(sh: &Shell, packages: &[String]) -> Result<Vec<String>, Box<dyn std::error::Error>> {
6367
let metadata = cmd!(sh, "cargo metadata --no-deps --format-version 1").read()?;
6468
let json: serde_json::Value = serde_json::from_str(&metadata)?;
6569

@@ -72,6 +76,15 @@ pub fn get_crate_dirs(sh: &Shell) -> Result<Vec<String>, Box<dyn std::error::Err
7276
// Extract directory path from the manifest path,
7377
// e.g., "/path/to/repo/releases/Cargo.toml" -> "/path/to/repo/releases".
7478
let dir_path = manifest_path.trim_end_matches("/Cargo.toml");
79+
80+
// Filter by package name if specified.
81+
if !packages.is_empty() {
82+
let package_name = package["name"].as_str()?;
83+
if !packages.contains(&package_name.to_string()) {
84+
return None;
85+
}
86+
}
87+
7588
Some(dir_path.to_string())
7689
})
7790
.collect();

tasks/src/lint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ impl LintConfig {
3737
}
3838

3939
/// Run the lint task.
40-
pub fn run(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
40+
pub fn run(sh: &Shell, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> {
4141
check_toolchain(sh, Toolchain::Nightly)?;
4242
quiet_println("Running lint task...");
4343

4444
lint_workspace(sh)?;
45-
lint_crates(sh)?;
45+
lint_crates(sh, packages)?;
4646
check_duplicate_deps(sh)?;
4747

4848
quiet_println("Lint task completed successfully");
@@ -78,10 +78,10 @@ fn lint_workspace(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
7878
/// even when a crate's own default features are disabled. Running clippy on each crate
7979
/// individually ensures that each crate truly compiles and passes lints with only its
8080
/// explicitly enabled features.
81-
fn lint_crates(sh: &Shell) -> Result<(), Box<dyn std::error::Error>> {
81+
fn lint_crates(sh: &Shell, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> {
8282
quiet_println("Running crate-specific lints...");
8383

84-
let crate_dirs = get_crate_dirs(sh)?;
84+
let crate_dirs = get_crate_dirs(sh, packages)?;
8585
quiet_println(&format!("Found crates: {}", crate_dirs.join(", ")));
8686

8787
for crate_dir in crate_dirs {

tasks/src/main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ struct Cli {
2121
/// Lock file to use for dependencies (defaults to recent).
2222
#[arg(long, global = true, value_enum)]
2323
lock_file: Option<LockFile>,
24-
24+
25+
/// Filter to specific package (can be specified multiple times).
26+
#[arg(short = 'p', long = "package", global = true)]
27+
packages: Vec<String>,
28+
2529
#[command(subcommand)]
2630
command: Commands,
2731
}
@@ -64,31 +68,31 @@ fn main() {
6468

6569
match cli.command {
6670
Commands::Lint => {
67-
if let Err(e) = lint::run(&sh) {
71+
if let Err(e) = lint::run(&sh, &cli.packages) {
6872
eprintln!("Error running lint task: {}", e);
6973
process::exit(1);
7074
}
7175
}
7276
Commands::Docs => {
73-
if let Err(e) = docs::run(&sh) {
77+
if let Err(e) = docs::run(&sh, &cli.packages) {
7478
eprintln!("Error building docs: {}", e);
7579
process::exit(1);
7680
}
7781
}
7882
Commands::Docsrs => {
79-
if let Err(e) = docs::run_docsrs(&sh) {
83+
if let Err(e) = docs::run_docsrs(&sh, &cli.packages) {
8084
eprintln!("Error building docs.rs docs: {}", e);
8185
process::exit(1);
8286
}
8387
}
8488
Commands::Bench => {
85-
if let Err(e) = bench::run(&sh) {
89+
if let Err(e) = bench::run(&sh, &cli.packages) {
8690
eprintln!("Error running bench tests: {}", e);
8791
process::exit(1);
8892
}
8993
}
9094
Commands::Test { toolchain } => {
91-
if let Err(e) = test::run(&sh, toolchain) {
95+
if let Err(e) = test::run(&sh, toolchain, &cli.packages) {
9296
eprintln!("Error running tests: {}", e);
9397
process::exit(1);
9498
}

tasks/src/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ impl TestConfig {
8383
}
8484

8585
/// Run build and test for all crates with the specified toolchain.
86-
pub fn run(sh: &Shell, toolchain: Toolchain) -> Result<(), Box<dyn std::error::Error>> {
86+
pub fn run(sh: &Shell, toolchain: Toolchain, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> {
8787
check_toolchain(sh, toolchain)?;
8888

89-
let crate_dirs = get_crate_dirs(sh)?;
89+
let crate_dirs = get_crate_dirs(sh, packages)?;
9090
quiet_println(&format!("Testing {} crates", crate_dirs.len()));
9191

9292
for crate_dir in &crate_dirs {

0 commit comments

Comments
 (0)