chain_failure/
chain_failure.rs1use 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 #[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"]), LocalCommand::new("sh").args(["-c", "echo 'Third command' && sleep 1"]),
21 ]);
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"]), LocalCommand::new("powershell").args(["echo 'Third command' && sleep 1"]),
29 ]);
32
33 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 if chain_query.is_empty() {
59 println!("Chain commands done. Exiting the app.");
60 std::process::exit(0);
61 }
62}