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
//! Wrapper around Cargo that is currently still used by the iOS implementation.

use cargo_metadata::{Message, Metadata, MetadataCommand};
use std::io::{BufReader, Error};
use std::path::PathBuf;
use std::process::{Command, Stdio};

/// Reads the metadata stored in the `Cargo.toml` file of the current working
/// directory. Note that this does not support `--manifest-path`.
pub fn metadata() -> Result<Metadata, Error> {
    let command = MetadataCommand::new();
    Ok(command.exec().unwrap())
}

/// Build function that is currently still used by the iOS implementation. Note
/// that this does not support `--manifest-path`.
pub fn build() -> Result<PathBuf, Error> {
    let metadata = metadata()?;
    let root_id = metadata.resolve.unwrap().root.unwrap();

    let mut command = Command::new("cargo")
        .arg("build")
        .arg("--target")
        .arg("x86_64-apple-ios")
        .arg("--message-format=json")
        .stdout(Stdio::piped())
        .spawn()?;

    let reader = BufReader::new(command.stdout.take().unwrap());

    let mut path = None;

    for message in Message::parse_stream(reader) {
        match message.unwrap() {
            Message::CompilerArtifact(mut artifact) if artifact.package_id == root_id => {
                path = artifact.filenames.pop();
            }
            _ => {}
        }
    }

    let output = command.wait().unwrap();

    assert!(output.success());

    Ok(path.unwrap())
}