-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsimulator.rs
More file actions
116 lines (101 loc) · 3.05 KB
/
simulator.rs
File metadata and controls
116 lines (101 loc) · 3.05 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::*;
use crate::*;
use process_stream::Process;
use std::path::PathBuf;
use tap::Pipe;
use tokio::process::Command;
use xclog::XCBuildSettings;
/// Simulator Device runner
pub struct SimulatorRunner {
pub device: Device,
pub app_id: String,
pub output_dir: PathBuf,
}
#[async_trait::async_trait]
impl Runner for SimulatorRunner {
async fn run<'a>(&self, task: &Task) -> Result<Process> {
self.boot(task).await?;
self.install(task).await?;
let process = self.launch(task).await;
process
}
}
impl SimulatorRunner {
pub fn new(device: Device, info: &XCBuildSettings) -> Self {
Self {
device,
app_id: info.product_bundle_identifier.clone(),
output_dir: info.metal_library_output_dir.clone(),
}
}
pub async fn boot<'a>(&self, task: &Task) -> Result<()> {
match pid::get_pid_by_name("Simulator") {
Err(Error::Lookup(_, _)) => {
task.info(format!("[Simulator] Launching"));
Command::new("open")
.args(&["-a", "Simulator"])
.spawn()?
.wait()
.await?;
task.info("[Simulator] Connected");
}
Err(err) => {
task.error(err.to_string());
}
_ => {}
}
task.info(self.booting_msg());
if let Err(e) = self.device.boot() {
let err: Error = e.into();
let err_msg = err.to_string();
if !err_msg.contains("current state Booted") {
// task.log_error(err_msg);
}
}
Ok(())
}
pub async fn install<'a>(&self, task: &Task) -> Result<()> {
task.info(self.installing_msg());
self.device
.install(&self.output_dir)
.pipe(|res| self.ok_or_abort(res, task))
.await?;
Ok(())
}
pub async fn launch<'a>(&self, task: &Task) -> Result<Process> {
task.info(self.launching_msg());
let mut process = Process::new("xcrun");
let args = &[
"simctl",
"launch",
"--terminate-running-process",
"--console-pty",
&self.device.udid,
&self.app_id,
];
process.args(args);
task.info(self.connected_msg());
Ok(process)
}
async fn ok_or_abort<'a, T>(&self, res: simctl::Result<T>, task: &Task) -> Result<()> {
if let Err(e) = res {
let error: Error = e.into();
task.error(error.to_string());
Err(error)
} else {
Ok(())
}
}
fn booting_msg(&self) -> String {
format!("[{}] Booting", self.device.name)
}
fn installing_msg(&self) -> String {
format!("[{}] Installing {}", self.device.name, self.app_id)
}
fn launching_msg(&self) -> String {
format!("[{}] Launching {}", self.device.name, self.app_id)
}
fn connected_msg(&self) -> String {
format!("[{}]", self.device.name)
}
}