Skip to content

Commit

Permalink
Add support for workspace chunking
Browse files Browse the repository at this point in the history
  • Loading branch information
CraZySacX committed Feb 11, 2024
1 parent 94ebbdd commit 761042a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
name = "cargo-matrix"
readme = "README.md"
repository = "https://github.com/rustyhorde/cargo-matrix"
version = "0.1.0"
version = "0.2.0"

[package.metadata.binstall]

Expand Down
18 changes: 18 additions & 0 deletions src/runtime/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ pub(crate) struct MatrixArgs {
#[arg(long, short)]
package: Option<String>,

/// Split the workspace packages into 'n' chunks of packages
#[arg(
long,
default_value_t = 1,
requires = "chunk",
help = "Split the workspace into n chunks, each chunk containing a roughly equal number of crates"
)]
num_chunks: usize,

/// The chunk number to test, i.e. if num_chunks = 4, chunks can be 1, 2, 3, or 4.
#[arg(
long,
default_value_t = 1,
requires = "n_chunks",
help = "Which chunk to test, indexed at 1"
)]
chunk: usize,

/// The supported cargo subcomand to run
#[command(subcommand)]
command: CargoSubcommands,
Expand Down
38 changes: 36 additions & 2 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
feature::FeatureMatrix,
runtime::execute::{Task, TaskKind},
};
use anyhow::Result;
use anyhow::{anyhow, Result};
use cargo_metadata::{Metadata, MetadataCommand, Package};
use clap::Parser;
use figment::{
Expand Down Expand Up @@ -77,7 +77,41 @@ where
.cloned()
.collect()
} else {
matricies
let num_chunks = matrix_args.num_chunks();
let chunk = matrix_args.chunk();
if *chunk == 0 {
return Err(anyhow!("chunk argument cannot be 0"));
}
if *num_chunks == 0 {
return Err(anyhow!("num_chunks argument cannot be 0"));
}
if chunk > num_chunks {
return Err(anyhow!("chunk must be less than or equal to num_chunks"));
}

let mut chunk_size = matricies.len() / num_chunks;
if matricies.len() % num_chunks != 0 {
chunk_size += 1;
}
let Some(matrix_chunk) = matricies.chunks(chunk_size).nth(chunk - 1) else {
println!(
"Chunk is empty (did you ask for more chunks than there are packages?"
);
return Ok(());
};
if *num_chunks != 1 {
let len = matrix_chunk.len();
let packages: String = matrix_chunk
.iter()
.flat_map(|(p, _)| [&p.name, ","])
.collect();
let packages = packages.trim_end_matches(',');
print!("{}", Paint::cyan(" Chunking ").bold());
println!(
"Running on chunk {chunk} out of {num_chunks} ({len} package(s): {packages})"
);
}
matrix_chunk.to_vec()
};

// Execute the task against the matricies
Expand Down

0 comments on commit 761042a

Please sign in to comment.