-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
xcode.rs
354 lines (323 loc) · 13.4 KB
/
xcode.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;
use anyhow::{bail, Result};
use chrono::Duration;
use clap::{Arg, ArgMatches, Command};
use if_chain::if_chain;
use log::info;
use serde::{Deserialize, Serialize};
use crate::api::{Api, NewRelease};
use crate::config::Config;
use crate::utils::args::{validate_distribution, ArgExt};
use crate::utils::file_search::ReleaseFileSearch;
use crate::utils::file_upload::UploadContext;
use crate::utils::fs::TempFile;
use crate::utils::sourcemaps::SourceMapProcessor;
use crate::utils::system::propagate_exit_status;
use crate::utils::xcode::{InfoPlist, MayDetach};
#[derive(Serialize, Deserialize, Default, Debug)]
struct SourceMapReport {
bundle_path: Option<PathBuf>,
sourcemap_path: Option<PathBuf>,
}
pub fn make_command(command: Command) -> Command {
command
.about("Upload react-native projects in a Xcode build step.")
.org_arg()
.project_arg(false)
.arg(Arg::new("force").long("force").short('f').help(
"Force the script to run, even in debug configuration.{n}This rarely \
does what you want because the default build script does not actually \
produce any information that the sentry build tool could pick up on.",
))
.arg(Arg::new("allow_fetch").long("allow-fetch").help(
"Enable sourcemap fetching from the packager.{n}If this is enabled \
the react native packager needs to run and sourcemaps are downloade \
from it if the simulator platform is detected.",
))
.arg(
Arg::new("fetch_from")
.long("fetch-from")
.value_name("URL")
.help(
"Set the URL to fetch sourcemaps from.{n}\
The default is http://127.0.0.1:8081/, where the react-native \
packager runs by default.",
),
)
.arg(Arg::new("force_foreground").long("force-foreground").help(
"Wait for the process to finish.{n}\
By default part of the build process will when triggered from Xcode \
detach and continue in the background. When an error happens, \
a dialog is shown. If this parameter is passed, Xcode will wait \
for the process to finish before the build finishes and output \
will be shown in the Xcode build output.",
))
.arg(Arg::new("build_script").value_name("BUILD_SCRIPT").help(
"Optional path to the build script.{n}\
This is the path to the `react-native-xcode.sh` script you want \
to use. By default the bundled build script is used.",
))
.arg(
Arg::new("dist")
.long("dist")
.value_name("DISTRIBUTION")
.multiple_occurrences(true)
.validator(validate_distribution)
.help("The names of the distributions to publish. Can be supplied multiple times."),
)
.arg(
Arg::new("args")
.value_name("ARGS")
.takes_value(true)
.multiple_values(true)
.last(true)
.help("Optional arguments to pass to the build script."),
)
.arg(
Arg::new("wait")
.long("wait")
.help("Wait for the server to fully process uploaded files."),
)
}
fn find_node() -> String {
if let Ok(path) = env::var("NODE_BINARY") {
if !path.is_empty() {
return path;
}
}
"node".into()
}
pub fn execute(matches: &ArgMatches) -> Result<()> {
let config = Config::current();
let (org, project) = config.get_org_and_project(matches)?;
let should_wrap = matches.is_present("force")
|| match env::var("CONFIGURATION") {
Ok(config) => !&config.contains("Debug"),
Err(_) => bail!("Need to run this from Xcode"),
};
let base = env::current_dir()?;
let script = if let Some(path) = matches.value_of("build_script") {
base.join(path)
} else {
base.join("../node_modules/react-native/scripts/react-native-xcode.sh")
}
.canonicalize()?;
info!(
"Issuing a command for Organization: {} Project: {}",
org, project
);
// if we allow fetching and we detect a simulator run, then we need to switch
// to simulator mode.
let fetch_url;
if_chain! {
if matches.is_present("allow_fetch");
if let Ok(val) = env::var("PLATFORM_NAME");
if val.ends_with("simulator");
then {
let url = matches.value_of("fetch_from").unwrap_or("http://127.0.0.1:8081/");
info!("Fetching sourcemaps from {}", url);
fetch_url = Some(url);
} else {
info!("Using react-native build script at {}", base.display());
fetch_url = None;
}
}
// in case we are in debug mode we directly dispatch to the script
// and exit out early.
if !should_wrap && fetch_url.is_none() {
info!("Running in debug mode, skipping script wrapping.");
let rv = process::Command::new(&script).spawn()?.wait()?;
propagate_exit_status(rv);
return Ok(());
}
info!("Parsing Info.plist");
let plist = match InfoPlist::discover_from_env()? {
Some(plist) => plist,
None => bail!("Could not find info.plist"),
};
info!("Parse result from Info.plist: {:?}", &plist);
let report_file = TempFile::create()?;
let node = find_node();
info!("Using node interpreter '{}'", &node);
MayDetach::wrap("React native symbol handling", |md| {
let bundle_path;
let sourcemap_path;
let bundle_url;
let sourcemap_url;
let bundle_file;
let sourcemap_file;
// If we have a fetch URL we need to fetch them from there now. In that
// case we do indeed fetch it right from the running packager and then
// store it in temporary files for later consumption.
if let Some(url) = fetch_url {
if !matches.is_present("force_foreground") {
md.may_detach()?;
}
let api = Api::current();
let url = url.trim_end_matches('/');
bundle_file = TempFile::create()?;
bundle_path = bundle_file.path().to_path_buf();
bundle_url = "~/index.ios.bundle".to_string();
sourcemap_file = TempFile::create()?;
sourcemap_path = sourcemap_file.path().to_path_buf();
sourcemap_url = "~/index.ios.map".to_string();
// wait up to 10 seconds for the server to be up.
if !api.wait_until_available(url, Duration::seconds(10))? {
bail!("Error: react-native packager did not respond in time");
}
api.download(
&format!("{}/index.ios.bundle?platform=ios&dev=true", url),
&mut bundle_file.open()?,
)?;
api.download(
&format!("{}/index.ios.map?platform=ios&dev=true", url),
&mut sourcemap_file.open()?,
)?;
// This is the case where we need to hook into the release process to
// collect sourcemaps when they are generated.
//
// this invokes via an indirection of sentry-cli our wrap_call() below.
// What is happening behind the scenes is that we switch out NODE_BINARY
// for ourselves which is what the react-native build script normally
// invokes. Because we export __SENTRY_RN_WRAP_XCODE_CALL=1, the main
// sentry-cli script will invoke our wrap_call() function below.
//
// That will then attempt to figure out that a react-native bundle is
// happening to the build script, parse out the arguments, add additional
// arguments if needed and then report the parsed arguments to a temporary
// JSON file we load back below.
//
// With that we we then have all the information we need to invoke the
// upload process.
} else {
let rv = process::Command::new(&script)
.env("NODE_BINARY", env::current_exe()?.to_str().unwrap())
.env("SENTRY_RN_REAL_NODE_BINARY", &node)
.env(
"SENTRY_RN_SOURCEMAP_REPORT",
report_file.path().to_str().unwrap(),
)
.env("__SENTRY_RN_WRAP_XCODE_CALL", "1")
.spawn()?
.wait()?;
propagate_exit_status(rv);
if !matches.is_present("force_foreground") {
md.may_detach()?;
}
let mut f = fs::File::open(report_file.path())?;
let report: SourceMapReport = serde_json::from_reader(&mut f).unwrap_or_else(|_| {
let format_err = format!(
"File {} doesn't contain a valid JSON data.",
report_file.path().display()
);
panic!("{}", format_err);
});
if report.bundle_path.is_none() || report.sourcemap_path.is_none() {
println!("Warning: build produced no sourcemaps.");
return Ok(());
}
bundle_path = report.bundle_path.unwrap();
bundle_url = format!("~/{}", bundle_path.file_name().unwrap().to_string_lossy());
sourcemap_path = report.sourcemap_path.unwrap();
sourcemap_url = format!(
"~/{}",
sourcemap_path.file_name().unwrap().to_string_lossy()
);
}
// now that we have all the data, we can now process and upload the
// sourcemaps.
println!("Processing react-native sourcemaps for Sentry upload.");
info!(" bundle path: {}", bundle_path.display());
info!(" sourcemap path: {}", sourcemap_path.display());
let mut processor = SourceMapProcessor::new();
processor.add(&bundle_url, ReleaseFileSearch::collect_file(bundle_path)?)?;
processor.add(
&sourcemap_url,
ReleaseFileSearch::collect_file(sourcemap_path)?,
)?;
processor.rewrite(&[base.parent().unwrap().to_str().unwrap()])?;
processor.add_sourcemap_references()?;
let dist = env::var("SENTRY_DIST").unwrap_or_else(|_| plist.build().to_string());
let release_name = env::var("SENTRY_RELEASE").unwrap_or(format!(
"{}@{}+{}",
plist.bundle_id(),
plist.version(),
dist
));
let api = Api::current();
let release = api.new_release(
&org,
&NewRelease {
version: release_name,
projects: vec![project.to_string()],
..Default::default()
},
)?;
match matches.values_of("dist") {
None => {
processor.upload(&UploadContext {
org: &org,
project: Some(&project),
release: &release.version,
dist: Some(&dist),
wait: matches.is_present("wait"),
})?;
}
Some(dists) => {
for dist in dists {
processor.upload(&UploadContext {
org: &org,
project: Some(&project),
release: &release.version,
dist: Some(dist),
wait: matches.is_present("wait"),
})?;
}
}
}
Ok(())
})
}
pub fn wrap_call() -> Result<()> {
let mut args: Vec<_> = env::args().skip(1).collect();
let mut bundle_path = None;
let mut sourcemap_path = None;
if args.len() > 1 && (args[1] == "bundle" || args[1] == "ram-bundle") {
let mut iter = args.iter().fuse();
while let Some(item) = iter.next() {
if item == "--sourcemap-output" {
sourcemap_path = iter.next().cloned();
} else if let Some(rest) = item.strip_prefix("--sourcemap-output=") {
sourcemap_path = Some(rest.to_string());
} else if item == "--bundle-output" {
bundle_path = iter.next().cloned();
} else if let Some(rest) = item.strip_prefix("--bundle-output=") {
bundle_path = Some(rest.to_string());
}
}
}
let mut sourcemap_report = SourceMapReport::default();
if sourcemap_path.is_none() && bundle_path.is_some() {
let mut path = env::temp_dir();
let mut map_path = PathBuf::from(bundle_path.clone().unwrap());
map_path.set_extension("jsbundle.map");
path.push(map_path.file_name().unwrap());
sourcemap_report.sourcemap_path = Some(PathBuf::from(&path));
args.push("--sourcemap-output".into());
args.push(path.into_os_string().into_string().unwrap());
} else if let Some(path) = sourcemap_path {
sourcemap_report.sourcemap_path = Some(PathBuf::from(path));
}
sourcemap_report.bundle_path = bundle_path.map(PathBuf::from);
let rv = process::Command::new(env::var("SENTRY_RN_REAL_NODE_BINARY").unwrap())
.args(args)
.spawn()?
.wait()?;
propagate_exit_status(rv);
let mut f = fs::File::create(env::var("SENTRY_RN_SOURCEMAP_REPORT").unwrap())?;
serde_json::to_writer(&mut f, &sourcemap_report)?;
Ok(())
}