Skip to content

Commit

Permalink
Add --partition M/N to distribute builds
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoqun committed Jul 16, 2024
1 parent d3332d2 commit 1340194
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ OPTIONS:
--keep-going
Keep going on failure.

--partition <M/N>
Partition runs and execute only its subset according to M/N.

--log-group <KIND>
Log grouping: none, github-actions.

Expand Down
10 changes: 9 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use lexopt::{
ValueExt,
};

use crate::{term, version::VersionRange, Feature, LogGroup, Rustup};
use crate::{term, version::VersionRange, Feature, LogGroup, Partition, Rustup};

pub(crate) struct Args {
pub(crate) leading_args: Vec<String>,
Expand Down Expand Up @@ -53,6 +53,8 @@ pub(crate) struct Args {
pub(crate) clean_per_version: bool,
/// --keep-going
pub(crate) keep_going: bool,
/// --partition
pub(crate) partition: Option<Partition>,
/// --print-command-list
pub(crate) print_command_list: bool,
/// --version-range/--rust-version
Expand Down Expand Up @@ -155,6 +157,7 @@ impl Args {
let mut clean_per_run = false;
let mut clean_per_version = false;
let mut keep_going = false;
let mut partition = None;
let mut print_command_list = false;
let mut no_manifest_path = false;
let mut locked = false;
Expand Down Expand Up @@ -308,6 +311,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("partition") => parse_opt!(partition, false),
Long("print-command-list") => parse_flag!(print_command_list),
Long("no-manifest-path") => parse_flag!(no_manifest_path),
Long("locked") => parse_flag!(locked),
Expand Down Expand Up @@ -571,6 +575,8 @@ impl Args {
None => LogGroup::auto(),
};

let partition = partition.as_deref().map(str::parse).transpose()?;

if no_dev_deps || no_private {
let flag = if no_dev_deps && no_private {
"--no-dev-deps and --no-private modify"
Expand Down Expand Up @@ -620,6 +626,7 @@ impl Args {
clean_per_run,
clean_per_version,
keep_going,
partition,
print_command_list,
no_manifest_path,
include_features: include_features.into_iter().map(Into::into).collect(),
Expand Down Expand Up @@ -813,6 +820,7 @@ const HELP: &[HelpText<'_>] = &[
"This flag can only be used together with --version-range flag.",
]),
("", "--keep-going", "", "Keep going on failure", &[]),
("", "--partition", "<M/N>", "Partition runs and execute only its subset according to M/N", &[]),
("", "--log-group", "<KIND>", "Log grouping: none, github-actions", &[
"If this option is not used, the environment will be automatically detected."
]),
Expand Down
39 changes: 37 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,22 @@ impl FromStr for LogGroup {
}
}

pub(crate) struct Partition {
index: usize,
count: usize,
}

impl FromStr for Partition {
type Err = Error;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.split('/').map(str::parse::<usize>).collect::<Vec<_>>()[..] {
[Ok(index), Ok(count)] if 0 < index && index <= count => Ok(Self { index, count }),
_ => bail!("bad or out-of-range partition: {s}"),
}
}
}

fn exec_cargo(
cx: &Context,
id: &PackageId,
Expand Down Expand Up @@ -672,7 +688,26 @@ fn exec_cargo_inner(
if progress.count != 0 && !cx.print_command_list && cx.log_group == LogGroup::None {
eprintln!();
}
progress.count += 1;

let new_count = progress.count + 1;
let mut skip = false;
if let Some(partition) = &cx.partition {
if progress.count % partition.count != partition.index - 1 {
let mut msg = String::new();
if term::verbose() {
write!(msg, "skipping {line}").unwrap();
} else {
write!(msg, "skipping {line} on {}", cx.packages(id).name).unwrap();
}
write!(msg, " ({}/{})", new_count, progress.total).unwrap();
let _guard = cx.log_group.print(&msg);
skip = true;
}
}
progress.count = new_count;
if skip {
return Ok(());
}

if cx.clean_per_run {
cargo_clean(cx, Some(id))?;
Expand All @@ -690,7 +725,7 @@ fn exec_cargo_inner(
} else {
write!(msg, "running {line} on {}", cx.packages(id).name).unwrap();
}
write!(msg, " ({}/{})", progress.count, progress.total).unwrap();
write!(msg, " ({}/{})", new_count, progress.total).unwrap();
let _guard = cx.log_group.print(&msg);

line.run()
Expand Down
3 changes: 3 additions & 0 deletions tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ OPTIONS:
--keep-going
Keep going on failure.

--partition <M/N>
Partition runs and execute only its subset according to M/N.

--log-group <KIND>
Log grouping: none, github-actions.

Expand Down
2 changes: 2 additions & 0 deletions tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ OPTIONS:
command
--clean-per-version Remove artifacts per Rust version
--keep-going Keep going on failure
--partition <M/N> Partition runs and execute only its subset according to
M/N
--log-group <KIND> Log grouping: none, github-actions
--print-command-list Print commands without run (Unstable)
--no-manifest-path Do not pass --manifest-path option to cargo (Unstable)
Expand Down

0 comments on commit 1340194

Please sign in to comment.