xtask: show cargo build progress

This commit is contained in:
Alex Janka 2024-08-18 16:26:58 +10:00
parent 27464daae3
commit b42dd002b1
3 changed files with 90 additions and 31 deletions

75
Cargo.lock generated
View file

@ -1450,6 +1450,18 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
[[package]]
name = "duct"
version = "0.13.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4ab5718d1224b63252cd0c6f74f6480f9ffeb117438a2e0f5cf6d9a4798929c"
dependencies = [
"libc",
"once_cell",
"os_pipe",
"shared_child",
]
[[package]]
name = "dunce"
version = "1.0.4"
@ -3627,6 +3639,16 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "os_pipe"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ffd2b0a5634335b135d5728d84c5e0fd726954b87111f7506a61c502280d982"
dependencies = [
"libc",
"windows-sys 0.59.0",
]
[[package]]
name = "os_str_bytes"
version = "6.6.1"
@ -3698,6 +3720,17 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pbr"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5827dfa0d69b6c92493d6c38e633bbaa5937c153d0d7c28bf12313f8c6d514"
dependencies = [
"crossbeam-channel",
"libc",
"winapi",
]
[[package]]
name = "percent-encoding"
version = "2.3.1"
@ -4353,9 +4386,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.122"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "784b6203951c57ff748476b126ccb5e8e2959a5c19e5c617ab1956be3dbc68da"
checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
dependencies = [
"itoa",
"memchr",
@ -4402,6 +4435,16 @@ dependencies = [
"syn 2.0.71",
]
[[package]]
name = "shared_child"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09fa9338aed9a1df411814a5b2252f7cd206c55ae9bf2fa763f8de84603aa60c"
dependencies = [
"libc",
"windows-sys 0.59.0",
]
[[package]]
name = "shlex"
version = "1.3.0"
@ -5536,6 +5579,15 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
@ -5864,21 +5916,6 @@ version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791978798f0597cfc70478424c2b4fdc2b7a8024aaff78497ef00f24ef674193"
[[package]]
name = "xshell"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db0ab86eae739efd1b054a8d3d16041914030ac4e01cd1dca0cf252fd8b6437"
dependencies = [
"xshell-macros",
]
[[package]]
name = "xshell-macros"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d422e8e38ec76e2f06ee439ccc765e9c6a9638b9e7c9f2e8255e4d41e8bd852"
[[package]]
name = "xtask"
version = "0.1.0"
@ -5886,11 +5923,13 @@ dependencies = [
"cargo_metadata",
"cfg-expr 0.16.0",
"clap",
"duct",
"env_logger",
"log",
"nih_plug_xtask",
"pbr",
"serde_json",
"strum",
"xshell",
]
[[package]]

View file

@ -8,9 +8,11 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.15", features = ["derive"] }
strum = { version = "0.26.3", features = ["derive"] }
xshell = "0.2.6"
duct = "0.13.7"
cargo_metadata = "0.18.1"
cfg-expr = "0.16.0"
pbr = "1.1.1"
serde_json = "1.0.125"
nih_plug_xtask = { workspace = true }
log = { workspace = true }
env_logger = { workspace = true }

View file

@ -1,8 +1,5 @@
use std::io::BufRead;
use clap::Parser;
use strum::IntoEnumIterator;
use xshell::Shell;
use args::*;
use types::*;
@ -281,9 +278,8 @@ fn run_build(
}
static INSTALLED_TRIPLES: std::sync::LazyLock<Vec<String>> = std::sync::LazyLock::new(|| {
let sh = Shell::new().expect("failed to open shell");
match xshell::cmd!(sh, "rustup target list --installed").output() {
Ok(output) => output.stdout.lines().map_while(Result::ok).collect(),
match duct::cmd!("rustup", "target", "list", "--installed").read() {
Ok(output) => output.lines().map(String::from).collect(),
Err(e) => {
log::error!("Error listing installed targets: {e:?}");
std::process::exit(0);
@ -307,11 +303,12 @@ fn cargo_exec(
impl Iterator<Item = std::io::Result<cargo_metadata::Message>>,
Box<dyn std::error::Error>,
> {
let sh = Shell::new()?;
let release = if debug { "" } else { "--release" };
let renderer = renderer.as_feature();
println!("Building {package} with {renderer} renderer for target {triple}");
let args=format!("{verb} -q -p {package} --target {triple} {release} --no-default-features -F {renderer} --message-format=json");
let args = args.split_whitespace().map(|s| s.to_string());
let args = if let Some(additional_flags) = additional_flags {
@ -320,11 +317,32 @@ fn cargo_exec(
args.collect::<Vec<_>>()
};
let output = sh.cmd("cargo").args(args).read()?;
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())?
};
Ok(cargo_metadata::Message::parse_stream(std::io::Cursor::new(
output,
)))
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;
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();
}
_ => {}
}
}),
)
}
fn as_artifact(message: cargo_metadata::Message) -> Option<cargo_metadata::Artifact> {