chain_failure/
chain_failure.rs

1use bevy::prelude::*;
2use bevy_local_commands::{
3    BevyLocalCommandsPlugin, Chain, LocalCommand, ProcessCompleted, ProcessOutput,
4};
5
6fn main() {
7    App::new()
8        .add_plugins((MinimalPlugins, BevyLocalCommandsPlugin))
9        .add_systems(Startup, setup)
10        .add_systems(Update, update)
11        .run();
12}
13
14fn setup(mut commands: Commands) {
15    // Create a chain of commands
16    #[cfg(not(windows))]
17    let chain = Chain::new(vec![
18        LocalCommand::new("sh").args(["-c", "echo 'First command' && sleep 1"]),
19        LocalCommand::new("commanddoesnotexist").args(["this should fail"]), // Failure
20        LocalCommand::new("sh").args(["-c", "echo 'Third command' && sleep 1"]),
21        // Same result with a failed running command
22        //LocalCommand::new("sh").args(["-c", "exit 1"]), // Failure
23    ]);
24    #[cfg(windows)]
25    let chain = Chain::new(vec![
26        LocalCommand::new("powershell").args(["echo 'First command' && sleep 1"]),
27        LocalCommand::new("commanddoesnotexist").args(["this should fail"]), // Failure
28        LocalCommand::new("powershell").args(["echo 'Third command' && sleep 1"]),
29        // Same result with a failed running command
30        //LocalCommand::new("powershell").args(["exit 1"]), // Failure
31    ]);
32
33    // Spawn an entity with the Chain component
34    let id = commands.spawn(chain).id();
35    println!("Spawned the chain as entity {id:?}");
36}
37
38fn update(
39    mut process_output_event: MessageReader<ProcessOutput>,
40    mut process_completed_event: MessageReader<ProcessCompleted>,
41    chain_query: Query<(), With<Chain>>,
42) {
43    for process_output in process_output_event.read() {
44        for line in process_output.lines() {
45            println!("Output Line ({:?}): {line}", process_output.entity);
46        }
47    }
48
49    for process_completed in process_completed_event.read() {
50        println!(
51            "Command {:?} completed (Success - {})",
52            process_completed.entity,
53            process_completed.exit_status.success()
54        );
55    }
56
57    // Check if there are no more Chain components (all chains completed)
58    if chain_query.is_empty() {
59        println!("Chain commands done. Exiting the app.");
60        std::process::exit(0);
61    }
62}