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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Rust-wrapper around the `simctl` utility that is shipped with Xcode and that
//! can be used to install apps onto one of the iOS simulator and subsequently
//! launch them.

use std::path::Path;
use std::process::{Command, Output, Stdio};
use std::str::FromStr;

/// Represents an error that occurs while communicating with the `simctl`
/// utility.
#[derive(Debug)]
pub enum Error {
    /// Contains an error that occurred while invoking the `simctl` utility.
    IO(std::io::Error),

    /// Contains an error that occurred while parsing the output of `simctl` as
    /// utf-8.
    Utf8(std::str::Utf8Error),

    /// Contains an error that occurred while parsing the output and
    /// encountering an unexpected sequence of tokens.
    UnexpectedOutput,
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Error::IO(value)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(value: std::str::Utf8Error) -> Self {
        Error::Utf8(value)
    }
}

/// This is the state of a device as provided by `simctl`. Currently, only two
/// states are supported ([DeviceState::Shutdown] and [DeviceState::Booted]). All
/// other states are not recognized and mapped to [DeviceState::Unknown] instead.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DeviceState {
    /// Represents a simulator that is not currently running.
    Shutdown,

    /// Represents a simulator that is currently running. Note that simulators
    /// may even be running while the Simulator.app is closed or the window
    /// corresponding to a specific simulator is closed.
    Booted,

    /// Represents any status that could not be parsed.
    Unknown,
}

impl FromStr for DeviceState {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "Shutdown" => DeviceState::Shutdown,
            "Booted" => DeviceState::Booted,
            _ => DeviceState::Unknown,
        })
    }
}

/// This is a device as provided by `simctl --list`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Device<'a> {
    operating_system: &'a str,
    name: &'a str,
    identifier: &'a str,
    state: DeviceState,
}

impl<'a> Device<'a> {
    /// This is the operating system of this device, including its version
    /// number. For example: "iOS 13.6" or "watchOS 6.2".
    pub fn operating_system(&self) -> &str {
        self.operating_system
    }

    /// This is the name of this device. For example "iPhone 11 Pro" or "Apple
    /// Watch Series 5 - 44mm".
    pub fn name(&self) -> &str {
        self.name
    }

    /// This is the UUID of this device (including dashes).
    pub fn identifier(&self) -> &str {
        self.identifier
    }

    /// This is the state of this device.
    pub fn state(&self) -> DeviceState {
        self.state
    }
}

/// Wrapper around the `simctl` utility.
#[derive(Debug)]
pub struct Simctl {
    list_output: Option<Output>,
}

impl Simctl {
    /// Returns a new instance of the Rust wrapper around the `simctl` utility.
    pub fn new() -> Simctl {
        Simctl { list_output: None }
    }

    /// Lists all simulator devices that are configured.
    pub fn list(&mut self) -> Result<DeviceManager, Error> {
        let output = Command::new("xcrun")
            .arg("simctl")
            .arg("list")
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()?
            .wait_with_output()?;

        self.list_output.replace(output);

        Ok(DeviceManager::new(std::str::from_utf8(
            self.list_output
                .as_ref()
                .ok_or(Error::UnexpectedOutput)?
                .stdout
                .as_slice(),
        )?))
    }

    /// Boots a simulator with the given identifier.
    pub fn boot(&mut self, identifier: &str) -> Result<(), Error> {
        Command::new("xcrun")
            .arg("simctl")
            .arg("boot")
            .arg(identifier)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()?
            .wait()?;
        Ok(())
    }

    /// Installs an application from the given path to the simulator with the
    /// given identifier.
    pub fn install(&mut self, identifier: &str, path: impl AsRef<Path>) -> Result<(), Error> {
        Command::new("xcrun")
            .arg("simctl")
            .arg("install")
            .arg(identifier)
            .arg(path.as_ref())
            .stdout(Stdio::null())
            .spawn()?
            .wait()?;
        Ok(())
    }

    /// Launches an installed application with the given bundle identifier on
    /// the simulator with the given identifier.
    pub fn launch(&mut self, identifier: &str, bundle_id: &str) -> Result<(), Error> {
        Command::new("xcrun")
            .arg("simctl")
            .arg("launch")
            .arg("--console-tty")
            .arg(identifier)
            .arg(bundle_id)
            .env("SIMCTL_CHILD_RUST_BACKTRACE", "full")
            .stdout(Stdio::null())
            .spawn()?
            .wait()?;
        Ok(())
    }
}

/// Wrapper around a set of devices. This is used to parse the output of a
/// `simctl list` command.
#[derive(Debug)]
pub struct DeviceManager<'a> {
    devices: Vec<Device<'a>>,
}

fn parse_operating_system(line: &str) -> Option<&str> {
    line.strip_prefix("-- ")?.strip_suffix(" --")
}

fn parse_device<'a>(line: &'a str, operating_system: &'a str) -> Option<Device<'a>> {
    let mut line = line.trim_start().trim_end().rsplitn(3, " (");

    let state = DeviceState::from_str(line.next()?.strip_suffix(")")?).ok()?;
    let id = line.next()?.strip_suffix(")")?;

    Some(Device {
        operating_system,
        name: line.next()?,
        identifier: id,
        state,
    })
}

impl<'a> DeviceManager<'a> {
    /// Parses a set of devices from the output of `simctl list` and returns the
    /// result.
    pub fn new(source: &'a str) -> DeviceManager<'a> {
        let mut lines = source.split("\n").into_iter();

        while let Some(line) = lines.next() {
            if line == "== Devices ==" {
                break;
            }
        }

        let mut devices = vec![];

        let mut operating_system = "";

        while let Some(line) = lines.next() {
            if line.starts_with("--") {
                if let Some(parsed) = parse_operating_system(line) {
                    operating_system = parsed;
                }
            } else if line.starts_with("==") {
                break;
            } else {
                if let Some(device) = parse_device(line, operating_system) {
                    devices.push(device);
                }
            }
        }

        DeviceManager { devices }
    }

    /// Starts a new query for this set of devices.
    pub fn query(&self) -> DeviceQuery<'_, 'a, std::slice::Iter<Device<'a>>> {
        DeviceQuery {
            iter: self.devices.iter(),
        }
    }
}

/// Represents a query of devices.
pub struct DeviceQuery<'a, 'b, T>
where
    T: Iterator<Item = &'a Device<'b>>,
    'b: 'a,
{
    iter: T,
}

impl<'a, 'b, T> DeviceQuery<'a, 'b, T>
where
    T: Iterator<Item = &'a Device<'b>>,
    'b: 'a,
{
    /// Queries for simulators that run on the given operating system.
    pub fn with_operating_system(
        self,
        operating_system: &'a str,
    ) -> DeviceQuery<'a, 'b, std::iter::Filter<T, impl FnMut(&&'a Device<'b>) -> bool>> {
        DeviceQuery {
            iter: self
                .iter
                .filter(move |device| device.operating_system == operating_system),
        }
    }

    /// Queries for simulators that have the given name.
    pub fn with_name(
        self,
        name: &'a str,
    ) -> DeviceQuery<'a, 'b, std::iter::Filter<T, impl FnMut(&&'a Device<'b>) -> bool>> {
        DeviceQuery {
            iter: self.iter.filter(move |device| device.name == name),
        }
    }

    /// Queries for simulators that are in the given state.
    pub fn with_state(
        self,
        state: DeviceState,
    ) -> DeviceQuery<'a, 'b, std::iter::Filter<T, impl FnMut(&&'a Device<'b>) -> bool>> {
        DeviceQuery {
            iter: self.iter.filter(move |device| device.state == state),
        }
    }
}

impl<'a, 'b, T> IntoIterator for DeviceQuery<'a, 'b, T>
where
    T: Iterator<Item = &'a Device<'b>>,
{
    type Item = &'a Device<'b>;
    type IntoIter = T;

    fn into_iter(self) -> Self::IntoIter {
        self.iter
    }
}