simple_chain/
simple_chain.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("sh").args(["-c", "echo 'Second command' && sleep 1"]),
20        LocalCommand::new("sh").args(["-c", "echo 'Third command' && sleep 1"]),
21    ]);
22    #[cfg(windows)]
23    let chain = Chain::new(vec![
24        LocalCommand::new("powershell").args(["echo 'First command' && sleep 1"]),
25        LocalCommand::new("powershell").args(["echo 'Second command' && sleep 1"]),
26        LocalCommand::new("powershell").args(["echo 'Third command' && sleep 1"]),
27    ]);
28
29    // Spawn an entity with the Chain component
30    let id = commands.spawn(chain).id();
31    println!("Spawned the chain as entity {id:?}");
32}
33
34fn update(
35    mut process_output_event: MessageReader<ProcessOutput>,
36    mut process_completed_event: MessageReader<ProcessCompleted>,
37    chain_query: Query<(), With<Chain>>,
38) {
39    for process_output in process_output_event.read() {
40        for line in process_output.lines() {
41            println!("Output Line ({:?}): {line}", process_output.entity);
42        }
43    }
44
45    for process_completed in process_completed_event.read() {
46        println!(
47            "Command {:?} completed (Success - {})",
48            process_completed.entity,
49            process_completed.exit_status.success()
50        );
51    }
52
53    // Check if there are no more Chain components (all chains completed)
54    if chain_query.is_empty() {
55        println!("All chain commands completed. Exiting the app.");
56        std::process::exit(0);
57    }
58}