1use bevy::prelude::*;
2use bevy_local_commands::{
3 BevyLocalCommandsPlugin, LocalCommand, Process, ProcessCompleted, ProcessOutput,
4};
5
6fn main() {
7 App::new()
8 .add_plugins((MinimalPlugins, BevyLocalCommandsPlugin))
9 .add_systems(Startup, startup)
10 .add_systems(Update, update)
11 .run();
12}
13
14fn startup(mut commands: Commands) {
15 #[cfg(not(windows))]
17 let cmd =
18 LocalCommand::new("sh").args(["-c", "echo 'Enter Name:' && read NAME && echo Hello $NAME"]);
19 #[cfg(windows)]
20 let cmd = LocalCommand::new("powershell")
21 .args(["$name = Read-Host 'Enter Name'; echo \"Name Entered: $name\""]);
22 let id = commands.spawn(cmd).id();
23 println!("Spawned the command as entity {id:?}");
24}
25
26fn update(
27 mut process_output_event: MessageReader<ProcessOutput>,
28 mut process_completed_event: MessageReader<ProcessCompleted>,
29 mut active_processes: Query<&mut Process>,
30) {
31 for process_output in process_output_event.read() {
32 for line in process_output.lines() {
33 println!("{line}");
34 }
35 }
36 if let Ok(mut process) = active_processes.single_mut() {
37 process.println("Bevy").unwrap_or_default();
38 }
39 if let Some(process_completed) = process_completed_event.read().last() {
40 println!(
41 "Command {:?} completed (Success - {})",
42 process_completed.entity,
43 process_completed.exit_status.success()
44 );
45 std::process::exit(0);
47 }
48}