forked from getsentry/sentry-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithSentryAndroid.ts
More file actions
51 lines (43 loc) · 1.96 KB
/
withSentryAndroid.ts
File metadata and controls
51 lines (43 loc) · 1.96 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
import type { ConfigPlugin } from 'expo/config-plugins';
import { withAppBuildGradle, withDangerousMod } from 'expo/config-plugins';
import * as path from 'path';
import { warnOnce, writeSentryPropertiesTo } from './utils';
export const withSentryAndroid: ConfigPlugin<string> = (config, sentryProperties: string) => {
const cfg = withAppBuildGradle(config, appBuildGradle => {
if (appBuildGradle.modResults.language === 'groovy') {
appBuildGradle.modResults.contents = modifyAppBuildGradle(appBuildGradle.modResults.contents);
} else {
throw new Error('Cannot configure Sentry in the app gradle because the build.gradle is not groovy');
}
return appBuildGradle;
});
return withDangerousMod(cfg, [
'android',
dangerousMod => {
writeSentryPropertiesTo(path.resolve(dangerousMod.modRequest.projectRoot, 'android'), sentryProperties);
return dangerousMod;
},
]);
};
const resolveSentryReactNativePackageJsonPath =
'["node", "--print", "require(\'path\').dirname(require.resolve(\'@sentry/react-native/package.json\'))"].execute().text.trim()';
/**
* Writes to projectDirectory/android/app/build.gradle,
* adding the relevant @sentry/react-native script.
*/
export function modifyAppBuildGradle(buildGradle: string): string {
if (buildGradle.includes('sentry.gradle')) {
return buildGradle;
}
// Use the same location that sentry-wizard uses
// See: https://github.com/getsentry/sentry-wizard/blob/e9b4522f27a852069c862bd458bdf9b07cab6e33/lib/Steps/Integrations/ReactNative.ts#L232
const pattern = /^android {/m;
if (!buildGradle.match(pattern)) {
warnOnce(
'Could not find `^android {` in `android/app/build.gradle`. Please open a bug report at https://github.com/getsentry/sentry-react-native.',
);
return buildGradle;
}
const applyFrom = `apply from: new File(${resolveSentryReactNativePackageJsonPath}, "sentry.gradle")`;
return buildGradle.replace(pattern, match => `${applyFrom}\n\n${match}`);
}