Skip to main content

parallel/
parallel.rs

1//! Steps running at the same time, with sub-steps, a log line, and a panic hook in case
2//! something goes badly wrong.
3//!
4//!   cargo run --example parallel
5//!   cargo run --example parallel -- --json     the same run as JSON events
6
7use std::thread::{scope, sleep};
8use std::time::Duration;
9
10use tinsel::prelude::*;
11
12fn main() {
13    tinsel::install_panic_hook();
14    if std::env::args().any(|a| a == "--json") {
15        tinsel::set_output(tinsel::Output::Json);
16    }
17
18    let live = Live::new("release", "v2.1.0");
19    let build = live.step("Build");
20    // sub-steps are drawn under their parent, and fold away once they've all passed
21    let targets: Vec<Step> = ["linux-x64", "linux-arm64", "macos-arm64"]
22        .into_iter()
23        .map(|t| build.step(t))
24        .collect();
25    let publish = live.step("Publish");
26
27    build.start();
28    // Step handles are cheap to clone and can move to other threads
29    scope(|s| {
30        for (i, target) in targets.iter().enumerate() {
31            s.spawn(move || {
32                target.start();
33                for stage in ["compiling", "linking", "stripping"] {
34                    target.detail(stage);
35                    sleep(Duration::from_millis(350 + 150 * i as u64));
36                }
37                if i == 2 {
38                    // a line worth keeping, printed above the display without disturbing it
39                    target.log("signed with Developer ID");
40                }
41                target.done();
42            });
43        }
44    });
45    build.done();
46
47    publish.start();
48    for i in 1..=30 {
49        let f = i as f32 / 30.0;
50        publish.progress(f, format!("{} of 3 archives", (f * 3.0).ceil()));
51        sleep(Duration::from_millis(40));
52    }
53    publish.done();
54
55    live.finish_with_card(Card::success("released").kv("tag", "v2.1.0"));
56}