From bf3a651f95bcb86d4dfa02dc3e9b7328ea7e4cd9 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 19 Aug 2024 13:29:05 +1000 Subject: [PATCH] xtask: fix progress bar for "xtask run" --- xtask/src/main.rs | 75 +++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 30cce5e..02bab61 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -95,7 +95,7 @@ fn run(args: RunArgs, debug: bool) -> Result<(), Box> { 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> { - 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>, + show_progress: bool, ) -> Result< impl Iterator>, Box, @@ -306,32 +303,46 @@ fn cargo_exec( let args = if let Some(additional_flags) = additional_flags { args.chain(additional_flags).collect::>() } else { - args.collect::>() + args.collect() }; - let build_plan: serde_json::Value = { - let mut args = args.clone(); - args.extend_from_slice(&["-Zunstable-options".into(), "--build-plan".into()]); - let output = duct::cmd("cargo", args).read()?; - serde_json::from_str(output.lines().nth(1).unwrap())? + let mut pb = if show_progress { + let build_plan: serde_json::Value = { + 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::>(); + 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 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 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; + 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| { - match v { - Ok(cargo_metadata::Message::BuildScriptExecuted(_)) - | Ok(cargo_metadata::Message::CompilerArtifact(_)) => { - pb.inc(); + if let Some(pb) = pb.as_mut() { + match v { + Ok(cargo_metadata::Message::BuildScriptExecuted(_)) + | Ok(cargo_metadata::Message::CompilerArtifact(_)) => { + pb.inc(); + } + _ => {} } - _ => {} } }), )