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
//! Tasks and context specific for building and running on iOS.

use std::collections::HashMap;
use std::path::PathBuf;

mod boot_ios_simulator;
mod build_runtime_library;
mod build_runtime_library_v2;
mod build_xcodeproj;
mod create_universal_binary;
mod generate_xcassets;
mod generate_xcodeproj;
mod install_on_ios_simulator;
mod open_ios_simulator;
mod run_on_ios_simulator;

pub use boot_ios_simulator::BootIOSSimulator;
pub use build_runtime_library::BuildRuntimeLibrary;
pub use build_runtime_library_v2::BuildRuntimeLibraryV2;
pub use build_xcodeproj::BuildXcodeproj;
pub use create_universal_binary::CreateUniversalBinary;
pub use generate_xcassets::GenerateXcassets;
pub use generate_xcodeproj::GenerateXcodeproj;
pub use install_on_ios_simulator::InstallOnIOSSimulator;
pub use open_ios_simulator::OpenIOSSimulator;
pub use run_on_ios_simulator::RunOnIOSSimulator;

pub use crate::core::tasks::InstallTarget;
pub use crate::core::tasks::{Dependency, DependencyCheck, InstallDependencies};

use crate::core::{Manager, Task};
use crate::Config;

/// Context that gets passed through each task.
#[derive(Debug)]
pub struct IOSContext {
    /// The platform-independent configuration that gets passed to each command.
    pub config: Config,

    /// A map between iOS architectures and the path to the product for each.
    pub products: HashMap<String, PathBuf>,

    /// This will contain a path to the universal binary which is basically a
    /// bundle of architecture-specific static libraries.
    pub universal_binary_path: Option<PathBuf>,
}

/// Represents one of the iOS-specific tasks.
pub enum IOSTask {
    /// This task boots an iOS Simulator (if necessary).
    BootIOSSimulator(BootIOSSimulator),

    /// This task builds the runtime library for the given target and with the
    /// given profile.
    BuildRuntimeLibrary(BuildRuntimeLibrary),

    /// This task builds the runtime library for the given target and with the
    /// given profile.
    BuildRuntimeLibraryV2(BuildRuntimeLibraryV2),

    /// This task builds an .xcodeproj with a given scheme, configuration and
    /// destination.
    BuildXcodeproj(BuildXcodeproj),

    /// This task creates a universal binary from one or multiple
    /// architecture-specific static libraries for iOS.
    CreateUniversalBinary(CreateUniversalBinary),

    /// This task generates an Xcode-compatible assets catalog for the assets of
    /// the Polyhorn project that is being compiled and its dependencies.
    GenerateXcassets(GenerateXcassets),

    /// This task generates an xcodeproj.
    GenerateXcodeproj(GenerateXcodeproj),

    /// This task checks if a given set of dependencies exist and if necessary,
    /// installs the dependencies that weren't found.
    InstallDependencies(InstallDependencies),

    /// This task installs an application on the iOS Simulator with a given
    /// identifier.
    InstallOnIOSSimulator(InstallOnIOSSimulator),

    /// This task installs a target with a given name using rustup, if
    /// necessary.
    InstallTarget(InstallTarget),

    /// This task opens the iOS Simulator GUI, which is not open by default when
    /// booting a (new) simulator.
    OpenIOSSimulator(OpenIOSSimulator),

    /// This task launches the application on an iOS Simulator with a given
    /// identifier.
    RunOnIOSSimulator(RunOnIOSSimulator),
}

/// Represents an error that is returned by one of the iOS-specific tasks.
#[derive(Debug)]
pub enum IOSError {
    /// Returned by tasks that have not yet been implemented for a specific host
    /// operating system.
    UnsupportedHostOS(&'static str),

    /// Returned by the `BuildRuntimeLibrary` task when Cargo fails to build the
    /// runtime library (most likely because of an error in user code, e.g.
    /// syntax error).
    CompilationFailure,

    /// Returned by tasks when an io error occurs.
    IO(std::io::Error),

    /// Returned by tasks that interact with `simctl`, an Apple-provided utility
    /// to programmatically control the iOS Simulator, in the event that it
    /// returns an error.
    Simctl(simctl::Error),
}

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

impl From<simctl::Error> for IOSError {
    fn from(error: simctl::Error) -> Self {
        IOSError::Simctl(error)
    }
}

impl Task for IOSTask {
    type Context = IOSContext;
    type Error = IOSError;

    fn verb(&self) -> &str {
        match self {
            IOSTask::BootIOSSimulator(task) => task.verb(),
            IOSTask::BuildRuntimeLibrary(task) => task.verb(),
            IOSTask::BuildRuntimeLibraryV2(task) => task.verb(),
            IOSTask::BuildXcodeproj(task) => task.verb(),
            IOSTask::CreateUniversalBinary(task) => task.verb(),
            IOSTask::GenerateXcassets(task) => task.verb(),
            IOSTask::GenerateXcodeproj(task) => task.verb(),
            IOSTask::InstallDependencies(task) => task.verb(),
            IOSTask::InstallOnIOSSimulator(task) => task.verb(),
            IOSTask::InstallTarget(task) => task.verb(),
            IOSTask::OpenIOSSimulator(task) => task.verb(),
            IOSTask::RunOnIOSSimulator(task) => task.verb(),
        }
    }

    fn message(&self) -> &str {
        match self {
            IOSTask::BootIOSSimulator(task) => task.message(),
            IOSTask::BuildRuntimeLibrary(task) => task.message(),
            IOSTask::BuildRuntimeLibraryV2(task) => task.message(),
            IOSTask::BuildXcodeproj(task) => task.message(),
            IOSTask::CreateUniversalBinary(task) => task.message(),
            IOSTask::GenerateXcassets(task) => task.message(),
            IOSTask::GenerateXcodeproj(task) => task.message(),
            IOSTask::InstallDependencies(task) => task.message(),
            IOSTask::InstallOnIOSSimulator(task) => task.message(),
            IOSTask::InstallTarget(task) => task.message(),
            IOSTask::OpenIOSSimulator(task) => task.message(),
            IOSTask::RunOnIOSSimulator(task) => task.message(),
        }
    }

    fn detail(&self) -> &str {
        match self {
            IOSTask::BootIOSSimulator(task) => task.detail(),
            IOSTask::BuildRuntimeLibrary(task) => task.detail(),
            IOSTask::BuildRuntimeLibraryV2(task) => task.detail(),
            IOSTask::BuildXcodeproj(task) => task.detail(),
            IOSTask::CreateUniversalBinary(task) => task.detail(),
            IOSTask::GenerateXcassets(task) => task.detail(),
            IOSTask::GenerateXcodeproj(task) => task.detail(),
            IOSTask::InstallDependencies(task) => task.detail(),
            IOSTask::InstallOnIOSSimulator(task) => task.detail(),
            IOSTask::InstallTarget(task) => task.detail(),
            IOSTask::OpenIOSSimulator(task) => task.detail(),
            IOSTask::RunOnIOSSimulator(task) => task.detail(),
        }
    }

    fn run(
        &self,
        context: Self::Context,
        manager: &mut Manager,
    ) -> Result<Self::Context, Self::Error> {
        match self {
            IOSTask::BootIOSSimulator(task) => task.run(context, manager),
            IOSTask::BuildRuntimeLibrary(task) => task.run(context, manager),
            IOSTask::BuildRuntimeLibraryV2(task) => task.run(context, manager),
            IOSTask::BuildXcodeproj(task) => task.run(context, manager),
            IOSTask::CreateUniversalBinary(task) => task.run(context, manager),
            IOSTask::GenerateXcassets(task) => task.run(context, manager),
            IOSTask::GenerateXcodeproj(task) => task.run(context, manager),
            IOSTask::InstallDependencies(task) => task
                .run((), manager)
                .map_err(|error| IOSError::IO(error))
                .map(|_| context),
            IOSTask::InstallOnIOSSimulator(task) => task.run(context, manager),
            IOSTask::InstallTarget(task) => task
                .run((), manager)
                .map_err(|error| IOSError::IO(error))
                .map(|_| context),
            IOSTask::OpenIOSSimulator(task) => task.run(context, manager),
            IOSTask::RunOnIOSSimulator(task) => task.run(context, manager),
        }
    }
}