Skip to content

Commit

Permalink
feat(cli): implement -k/--keep-going
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Nov 23, 2024
1 parent 560e6c0 commit 7e1ab54
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ pub fn clap() -> clap::Command {
.help("if multiple tasks targets are available, execute them all")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("keep_going")
.help("keep going until N jobs / tasks fail (0 means infinity)")
.short('k')
.long("keep_going")
.env("LAZE_KEEP_GOING")
.num_args(1)
.default_value("1")
.value_parser(clap::value_parser!(usize)),
)
.arg(jobs())
.next_help_heading("What to build")
.arg(
Expand Down
25 changes: 23 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fn ninja_run(
verbose: bool,
targets: Option<Vec<Utf8PathBuf>>,
jobs: Option<usize>,
keep_going: Option<usize>,
) -> Result<i32, Error> {
let mut ninja_cmd = NinjaCmdBuilder::default();

Expand All @@ -86,6 +87,10 @@ fn ninja_run(
ninja_cmd.jobs(jobs);
}

if let Some(keep_going) = keep_going {
ninja_cmd.keep_going(keep_going);
}

let ninja_cmd = ninja_cmd.build().unwrap();
let ninja_binary = ninja_cmd.binary;
let ninja_exit = ninja_cmd
Expand Down Expand Up @@ -270,6 +275,7 @@ fn try_main() -> Result<i32> {
};

let jobs = build_matches.get_one::<usize>("jobs").copied();
let keep_going = build_matches.get_one::<usize>("keep_going").copied();

let partitioner = build_matches
.get_one::<task_partitioner::PartitionerBuilder>("partition")
Expand Down Expand Up @@ -390,6 +396,8 @@ fn try_main() -> Result<i32> {
verbose > 0,
Some(ninja_targets),
jobs,
None, // have to fail on build error b/c no way of knowing *which* target
// failed
)? != 0
{
return Err(anyhow!("build error"));
Expand All @@ -401,6 +409,7 @@ fn try_main() -> Result<i32> {
targets.iter(),
args.as_ref(),
verbose,
keep_going.unwrap(),
project_root.as_std_path(),
)?;

Expand Down Expand Up @@ -440,7 +449,13 @@ fn try_main() -> Result<i32> {
)
};

ninja_run(ninja_build_file.as_path(), verbose > 0, targets, jobs)?;
ninja_run(
ninja_build_file.as_path(),
verbose > 0,
targets,
jobs,
keep_going,
)?;
}
}
Some(("clean", clean_matches)) => {
Expand All @@ -456,7 +471,13 @@ fn try_main() -> Result<i32> {
false => "clean",
};
let clean_target: Option<Vec<Utf8PathBuf>> = Some(vec!["-t".into(), tool.into()]);
ninja_run(ninja_build_file.as_path(), verbose > 0, clean_target, None)?;
ninja_run(
ninja_build_file.as_path(),
verbose > 0,
clean_target,
None,
None,
)?;
}
_ => {}
};
Expand Down
13 changes: 9 additions & 4 deletions src/ninja/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ use std::process::{Command, ExitStatus, Stdio};
use camino::{Utf8Path, Utf8PathBuf};
use indexmap::IndexMap;

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Default)]
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub enum NinjaRuleDeps {
#[default]
None,
GCC(String),
}



#[derive(Builder, Debug, PartialEq, Eq, Clone)]
#[builder(setter(into))]
pub struct NinjaRule<'a> {
Expand Down Expand Up @@ -317,6 +314,9 @@ pub struct NinjaCmd<'a> {

#[builder(default = "None")]
jobs: Option<usize>,

#[builder(default = "None")]
keep_going: Option<usize>,
}

impl<'a> NinjaCmd<'a> {
Expand All @@ -333,6 +333,11 @@ impl<'a> NinjaCmd<'a> {
cmd.arg(jobs.to_string());
}

if let Some(keep_going) = self.keep_going {
cmd.arg("-k");
cmd.arg(keep_going.to_string());
}

if let Some(targets) = &self.targets {
for target in targets {
cmd.arg(target);
Expand Down
17 changes: 11 additions & 6 deletions src/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn run_tasks<'a, I>(
tasks: I,
args: std::option::Option<&Vec<&str>>,
verbose: u8,
keep_going: usize,
project_root: &Path,
) -> Result<(Vec<RunTaskResult<'a>>, usize), Error>
where
Expand All @@ -33,16 +34,20 @@ where
);

let result = task.execute(project_root, args, verbose);
if result.is_err() {
errors += 1;
}
let is_error = result.is_err();

let result = RunTaskResult {
results.push(RunTaskResult {
build,
task,
result,
};
results.push(result);
});

if is_error {
errors += 1;
if keep_going > 0 && errors >= keep_going {
break;
}
}
}

Ok((results, errors))
Expand Down

0 comments on commit 7e1ab54

Please sign in to comment.