-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathwithSentryIOS.ts
More file actions
144 lines (125 loc) · 5.77 KB
/
withSentryIOS.ts
File metadata and controls
144 lines (125 loc) · 5.77 KB
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
/* oxlint-disable typescript-eslint(no-unsafe-member-access) */
import type { ExpoConfig } from '@expo/config-types';
import type { ConfigPlugin, XcodeProject } from 'expo/config-plugins';
import { withAppDelegate, withDangerousMod, withXcodeProject } from 'expo/config-plugins';
import * as path from 'path';
import { warnOnce } from './logger';
import { writeSentryPropertiesTo } from './utils';
type BuildPhase = { shellScript: string };
const SENTRY_REACT_NATIVE_XCODE_PATH =
"`\"$NODE_BINARY\" --print \"require('path').dirname(require.resolve('@sentry/react-native/package.json')) + '/scripts/sentry-xcode.sh'\"`";
const SENTRY_REACT_NATIVE_XCODE_DEBUG_FILES_PATH =
"`${NODE_BINARY:-node} --print \"require('path').dirname(require.resolve('@sentry/react-native/package.json')) + '/scripts/sentry-xcode-debug-files.sh'\"`";
export const withSentryIOS: ConfigPlugin<{ sentryProperties: string; useNativeInit: boolean | undefined }> = (
config,
{ sentryProperties, useNativeInit = false },
) => {
const xcodeProjectCfg = withXcodeProject(config, config => {
const xcodeProject: XcodeProject = config.modResults;
const sentryBuildPhase = xcodeProject.pbxItemByComment(
'Upload Debug Symbols to Sentry',
'PBXShellScriptBuildPhase',
);
if (!sentryBuildPhase) {
xcodeProject.addBuildPhase([], 'PBXShellScriptBuildPhase', 'Upload Debug Symbols to Sentry', null, {
shellPath: '/bin/sh',
shellScript: `/bin/sh ${SENTRY_REACT_NATIVE_XCODE_DEBUG_FILES_PATH}`,
});
}
const bundleReactNativePhase = xcodeProject.pbxItemByComment(
'Bundle React Native code and images',
'PBXShellScriptBuildPhase',
);
modifyExistingXcodeBuildScript(bundleReactNativePhase);
return config;
});
const appDelegateCfc = useNativeInit ? modifyAppDelegate(xcodeProjectCfg) : xcodeProjectCfg;
return withDangerousMod(appDelegateCfc, [
'ios',
config => {
writeSentryPropertiesTo(path.resolve(config.modRequest.projectRoot, 'ios'), sentryProperties);
return config;
},
]);
};
export function modifyExistingXcodeBuildScript(script: BuildPhase): void {
if (!script.shellScript.match(/(packager|scripts)\/react-native-xcode\.sh\b/)) {
warnOnce(
`'react-native-xcode.sh' not found in 'Bundle React Native code and images'.
Please open a bug report at https://github.com/getsentry/sentry-react-native`,
);
return;
}
if (script.shellScript.includes('sentry-xcode.sh')) {
warnOnce("The latest 'sentry-xcode.sh' script already exists in 'Bundle React Native code and images'.");
return;
}
if (script.shellScript.includes('@sentry')) {
warnOnce(
`Outdated or custom Sentry script found in 'Bundle React Native code and images'.
Regenerate the native project to use the latest script.
Run npx expo prebuild --clean`,
);
return;
}
const code = JSON.parse(script.shellScript);
script.shellScript = JSON.stringify(addSentryWithBundledScriptsToBundleShellScript(code));
}
export function addSentryWithBundledScriptsToBundleShellScript(script: string): string {
return script.replace(
/^.*?(packager|scripts)\/react-native-xcode\.sh\s*(\\'\\\\")?/m,
(match: string) => `/bin/sh ${SENTRY_REACT_NATIVE_XCODE_PATH} ${match}`,
);
}
export function modifyAppDelegate(config: ExpoConfig): ExpoConfig {
return withAppDelegate(config, async config => {
if (!config.modResults?.path) {
warnOnce("Can't add 'RNSentrySDK.start()' to the iOS AppDelegate, because the file was not found.");
return config;
}
const fileName = path.basename(config.modResults.path);
if (config.modResults.language === 'swift') {
if (config.modResults.contents.includes('RNSentrySDK.start()')) {
warnOnce(`Your '${fileName}' already contains 'RNSentrySDK.start()'.`);
return config;
}
// Add RNSentrySDK.start() at the beginning of application method
const originalContents = config.modResults.contents;
config.modResults.contents = config.modResults.contents.replace(
/(func application\([^)]*\) -> Bool \{)\s*\n(\s*)/s,
'$1\n$2RNSentrySDK.start()\n$2',
);
if (config.modResults.contents === originalContents) {
warnOnce(`Failed to insert 'RNSentrySDK.start()' in '${fileName}'.`);
} else if (!config.modResults.contents.includes('import RNSentry')) {
// Insert import statement after the first import (works for both UIKit and Expo imports)
config.modResults.contents = config.modResults.contents.replace(/(import \S+\n)/, '$1import RNSentry\n');
}
} else if (['objcpp', 'objc'].includes(config.modResults.language)) {
if (config.modResults.contents.includes('[RNSentrySDK start]')) {
warnOnce(`Your '${fileName}' already contains '[RNSentrySDK start]'.`);
return config;
}
// Add [RNSentrySDK start] at the beginning of application:didFinishLaunchingWithOptions method
const originalContents = config.modResults.contents;
config.modResults.contents = config.modResults.contents.replace(
/(- \(BOOL\)application:[\s\S]*?didFinishLaunchingWithOptions:[\s\S]*?\{\n)(\s*)/s,
'$1$2[RNSentrySDK start];\n$2',
);
if (config.modResults.contents === originalContents) {
warnOnce(`Failed to insert '[RNSentrySDK start]' in '${fileName}.`);
} else if (!config.modResults.contents.includes('#import <RNSentry/RNSentry.h>')) {
// Add import after AppDelegate.h
config.modResults.contents = config.modResults.contents.replace(
/(#import "AppDelegate.h"\n)/,
'$1#import <RNSentry/RNSentry.h>\n',
);
}
} else {
warnOnce(
`Unsupported language '${config.modResults.language}' detected in '${fileName}', the native code won't be updated.`,
);
}
return config;
});
}