xtask: fix progress bar for "xtask run"

This commit is contained in:
Alex Janka 2024-08-19 13:29:05 +10:00
parent f808a58492
commit bf3a651f95

View file

@ -95,7 +95,7 @@ fn run(args: RunArgs, debug: bool) -> Result<(), Box<dyn std::error::Error>> {
match binary {
Binary::Gui => run_gui(platform, &triple, renderer, debug),
Binary::Cli => run_cli(platform, &triple, renderer, debug),
Binary::Cli => run_cli(&triple, renderer, debug),
Binary::Vst => {
eprintln!("twinc_emu_vst doesn't support standalone usage!");
std::process::exit(1);
@ -123,14 +123,14 @@ fn build_binary(
}
}
Binary::Cli => {
for (platform, triple) in platforms_and_triples(platforms, architectures) {
for (_, triple) in platforms_and_triples(platforms, architectures) {
let output_dir = OUTPUT_DIR
.get()
.unwrap()
.join(executable_dir(binary, &triple, renderer));
std::fs::create_dir_all(&output_dir)?;
build_cli(output_dir, platform, &triple, renderer, debug)?;
build_cli(output_dir, &triple, renderer, debug)?;
}
}
Binary::Vst => {
@ -179,7 +179,6 @@ fn build_gui(
run_build(
"gui",
platform,
triple,
renderer,
debug,
@ -199,27 +198,26 @@ fn run_gui(
let _ = cargo_exec(
"run",
"gui",
platform,
triple,
renderer,
debug,
Some(["-F", ui].into_iter().map(String::from).collect()),
true,
)?;
Ok(())
}
fn build_cli(
output_dir: std::path::PathBuf,
platform: Platform,
triple: &cfg_expr::targets::Triple,
renderer: Renderer,
debug: bool,
) -> Result<(), Box<dyn std::error::Error>> {
run_build("cli", platform, triple, renderer, debug, None, output_dir)
run_build("cli", triple, renderer, debug, None, output_dir)
}
fn run_cli(
platform: Platform,
triple: &cfg_expr::targets::Triple,
renderer: Renderer,
debug: bool,
@ -230,21 +228,20 @@ fn run_cli(
.position(|arg| arg == "--")
.map(|extra_args_index| args[extra_args_index..].to_vec());
let _ = cargo_exec(
for _ in cargo_exec(
"run",
"cli",
platform,
triple,
renderer,
debug,
additional_flags,
)?;
true,
)? {}
Ok(())
}
fn run_build(
package: &str,
platform: Platform,
triple: &cfg_expr::targets::Triple,
renderer: Renderer,
debug: bool,
@ -254,11 +251,11 @@ fn run_build(
for (name, executable_path) in cargo_exec(
"build",
package,
platform,
triple,
renderer,
debug,
additional_flags,
true,
)?
.filter_map(|m| as_artifact(m.ok()?))
.filter_map(|a| a.executable.map(|e| (a.target.name, e)))
@ -286,11 +283,11 @@ fn is_toolchain_installed(triple: &cfg_expr::targets::Triple) -> bool {
fn cargo_exec(
verb: &str,
package: &str,
_platform: Platform,
triple: &cfg_expr::targets::Triple,
renderer: Renderer,
debug: bool,
additional_flags: Option<Vec<String>>,
show_progress: bool,
) -> Result<
impl Iterator<Item = std::io::Result<cargo_metadata::Message>>,
Box<dyn std::error::Error>,
@ -306,26 +303,39 @@ fn cargo_exec(
let args = if let Some(additional_flags) = additional_flags {
args.chain(additional_flags).collect::<Vec<_>>()
} else {
args.collect::<Vec<_>>()
args.collect()
};
let mut pb = if show_progress {
let build_plan: serde_json::Value = {
let mut args = args.clone();
args.extend_from_slice(&["-Zunstable-options".into(), "--build-plan".into()]);
let args = [String::from("build")]
.into_iter()
.chain(args.iter().skip(1).cloned().take_while(|v| v != "--"))
.chain(["-Zunstable-options".into(), "--build-plan".into()])
.collect::<Vec<_>>();
let output = duct::cmd("cargo", args).read()?;
serde_json::from_str(output.lines().nth(1).unwrap())?
};
let num_invocations = build_plan["invocations"].as_array().unwrap().len();
let output = duct::cmd("cargo", args).stdout_capture().reader()?;
let mut pb = pbr::ProgressBar::new(num_invocations.try_into().unwrap());
pb.message("Building crate ");
pb.show_time_left = false;
pb.show_speed = false;
Some(pb)
} else {
None
};
let output = duct::cmd("cargo", args)
.stdout_capture()
.stderr_capture()
.reader()?;
Ok(
cargo_metadata::Message::parse_stream(std::io::BufReader::new(output)).inspect(move |v| {
if let Some(pb) = pb.as_mut() {
match v {
Ok(cargo_metadata::Message::BuildScriptExecuted(_))
| Ok(cargo_metadata::Message::CompilerArtifact(_)) => {
@ -333,6 +343,7 @@ fn cargo_exec(
}
_ => {}
}
}
}),
)
}