1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use crate::prelude::*;

use super::XcRunSimctlInstance;

#[cfg(feature = "cli")]
#[derive(Args, Debug)]
pub struct CliLaunchArgs {
	#[clap(long, default_value_t = true)]
	console: bool,

	#[clap(long)]
	bundle_id: String,

	/// If true, pipes the spawned process's console to the current process's console.
	/// This is the default behavior.
	#[clap(long, default_value_t = true)]
	piped: bool,
}

#[cfg(feature = "cli")]
impl CliLaunchArgs {
	pub fn resolve(self) -> Result<(bool, LaunchConfig)> {
		let config = LaunchConfig {
			console: self.console,
			bundle_id: self.bundle_id,
		};
		Ok((self.piped, config))
	}
}

#[derive(Debug)]
pub struct LaunchConfig {
	pub console: bool,
	pub bundle_id: String,
}

impl LaunchConfig {
	pub fn console(&self) -> bool {
		self.console
	}

	pub fn bundle_id(&self) -> &str {
		&self.bundle_id
	}
}

pub use output::LaunchOutput;
mod output;

impl XcRunSimctlInstance<'_> {
	#[instrument(ret, skip(self,))]
	pub fn launch(
		&self,
		config: &LaunchConfig,
		simulator_name: DeviceName,
	) -> error::Result<LaunchOutput> {
		let mut cmd = self.bossy_command().with_arg("launch");
		if config.console() {
			cmd.add_arg("--console");
		}
		cmd.add_arg(simulator_name.to_string());
		cmd.add_arg(config.bundle_id());

		LaunchOutput::from_bossy_result(cmd.run_and_wait_for_output())
	}

	/// Like [Self::launch], but pipes the console of the launched
	/// process to the current process's console.
	///
	/// The upshot of this is you can see your programs logs in the console!
	///
	/// Not setting [LaunchConfig::console] to true will result in a warning,
	/// since presumably you are calling this function over [Self::launch] to
	/// see logs in the console.
	#[instrument(ret, skip(self,))]
	pub fn launch_piped(
		&self,
		config: &LaunchConfig,
		simulator_name: DeviceName,
	) -> Result<ExitStatus> {
		let mut cmd = self.bossy_command().with_arg("launch");
		if config.console() {
			cmd.add_arg("--console");
		} else {
			warn!(
				?config,
				?simulator_name,
				"Why are you calling launch_piped without console set to true?"
			);
		}
		cmd.add_arg(simulator_name.to_string());
		cmd.add_arg(config.bundle_id());

		Ok(cmd.run_and_wait()?)
	}
}