-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmod.rs
206 lines (184 loc) · 5.76 KB
/
mod.rs
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
use super::{
config::Config,
deps::{GemCache, PackageSpec},
target::{ArchiveError, BuildError, ExportError, Target},
};
use crate::{
env::{Env, ExplicitEnv as _},
opts,
util::cli::{Report, Reportable},
DuctExpressionExt,
};
use std::{
collections::BTreeSet,
fmt::{self, Display},
path::PathBuf,
};
use thiserror::Error;
mod devicectl;
mod ios_deploy;
mod simctl;
pub use simctl::Device as Simulator;
#[derive(Debug, Error)]
pub enum RunError {
#[error(transparent)]
BuildFailed(BuildError),
#[error(transparent)]
ArchiveFailed(ArchiveError),
#[error(transparent)]
ExportFailed(ExportError),
#[error("IPA appears to be missing. Not found at either {old} or {new}")]
IpaMissing { old: PathBuf, new: PathBuf },
#[error("Failed to unzip archive: {0}")]
UnzipFailed(std::io::Error),
#[error("{0}")]
DeployFailed(String),
}
impl Reportable for RunError {
fn report(&self) -> Report {
match self {
Self::BuildFailed(err) => err.report(),
Self::ArchiveFailed(err) => err.report(),
Self::ExportFailed(err) => err.report(),
Self::IpaMissing { old, new } => Report::error(
"IPA appears to be missing",
format!("Not found at either {:?} or {:?}", old, new),
),
Self::UnzipFailed(err) => Report::error("Failed to unzip archive", err),
Self::DeployFailed(err) => Report::error("Failed to deploy app", err),
}
}
}
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum DeviceKind {
Simulator,
IosDeployDevice,
VisionOsDeployDevice,
DeviceCtlDevice,
}
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Device<'a> {
id: String,
name: String,
model: String,
target: &'a Target<'a>,
kind: DeviceKind,
paired: bool,
}
impl<'a> Display for Device<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name, self.model)
}
}
impl<'a> Device<'a> {
pub(super) fn new(
id: String,
name: String,
model: String,
target: &'a Target<'a>,
kind: DeviceKind,
) -> Self {
Self {
id,
name,
model,
target,
kind,
paired: true,
}
}
pub fn paired(mut self, paired: bool) -> Self {
self.paired = paired;
self
}
pub fn target(&self) -> &'a Target<'a> {
self.target
}
pub fn name(&self) -> &str {
&self.name
}
pub fn model(&self) -> &str {
&self.model
}
pub fn run(
&self,
config: &Config,
env: &Env,
noise_level: opts::NoiseLevel,
non_interactive: bool,
profile: opts::Profile,
) -> Result<duct::Handle, RunError> {
// TODO: These steps are run unconditionally, which is slooooooow
println!("Building app...");
self.target
.build(config, env, noise_level, profile)
.map_err(RunError::BuildFailed)?;
println!("Archiving app...");
self.target
.archive(config, env, noise_level, profile, None)
.map_err(RunError::ArchiveFailed)?;
match self.kind {
DeviceKind::Simulator => simctl::run(config, env, non_interactive, &self.id)
.map_err(|e| RunError::DeployFailed(e.to_string())),
DeviceKind::IosDeployDevice
| DeviceKind::VisionOsDeployDevice
| DeviceKind::DeviceCtlDevice => {
println!("Exporting app...");
self.target
.export(config, env, noise_level)
.map_err(RunError::ExportFailed)?;
println!("Extracting IPA...");
let ipa_path = config
.ipa_path()
.map_err(|(old, new)| RunError::IpaMissing { old, new })?;
let export_dir = config.export_dir();
let cmd = duct::cmd::<&str, [String; 0]>("unzip", [])
.vars(env.explicit_env())
.before_spawn(move |cmd| {
if noise_level.pedantic() {
cmd.arg("-q");
}
cmd.arg("-o").arg(&ipa_path).arg("-d").arg(&export_dir);
Ok(())
})
.dup_stdio();
cmd.run().map_err(RunError::UnzipFailed)?;
if self.kind == DeviceKind::IosDeployDevice {
ios_deploy::run_and_debug(config, env, non_interactive, &self.id)
.map_err(|e| RunError::DeployFailed(e.to_string()))
} else {
devicectl::run(config, env, non_interactive, &self.id, self.paired)
.map_err(|e| RunError::DeployFailed(e.to_string()))
}
}
}
}
}
pub fn list_devices<'a>(env: &Env) -> Result<BTreeSet<Device<'a>>, String> {
let mut devices = BTreeSet::default();
let mut error = None;
// devicectl
match devicectl::device_list(env) {
Ok(d) => {
devices.extend(d);
}
Err(e) => {
error = Some(e);
}
}
// if we could not find a device with devicectl, let's use ios-deploy
if devices.is_empty() {
PackageSpec::brew("ios-deploy")
.install(false, &mut GemCache::new())
.map_err(|e| e.to_string())?;
return ios_deploy::device_list(env).map_err(|e| e.to_string());
}
if let Some(err) = error {
Err(err.to_string())
} else {
Ok(devices)
}
}
pub fn list_simulators(env: &Env) -> Result<BTreeSet<Simulator>, String> {
simctl::device_list(env).map_err(|e| e.to_string())
}