Skip to content

Commit

Permalink
feat(reactnative): Use dynamic node path resolution to reference RN S…
Browse files Browse the repository at this point in the history
…AGP in app/build.gradle
  • Loading branch information
krystofwoldrich committed Oct 4, 2023
1 parent a91ad1a commit d80d2d7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/react-native/gradle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as fs from 'fs';

const applyFrom =
'apply from: "../../node_modules/@sentry/react-native/sentry.gradle"';
`apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")`;

export function doesAppBuildGradleIncludeRNSentryGradlePlugin(
content: string,
): boolean {
return content.includes(applyFrom);
return content.includes('sentry.gradle');
}

export function addRNSentryGradlePlugin(content: string): string {
Expand All @@ -15,7 +15,7 @@ export function addRNSentryGradlePlugin(content: string): string {

export function removeRNSentryGradlePlugin(content: string): string {
return content.replace(
/^\s*apply from: ["']..\/..\/node_modules\/@sentry\/react-native\/sentry.gradle["'];?\s*?\r?\n/m,
/^\s*apply from:.*sentry\.gradle.*;?\s*?\r?\n/m,
'',
);
}
Expand Down
55 changes: 51 additions & 4 deletions test/react-native/gradle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ apply plugin: "io.sentry.android.gradle"
sentry {
}
apply from: "../../node_modules/@sentry/react-native/sentry.gradle"
apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")
android {
ndkVersion rootProject.ext.ndkVersion
Expand Down Expand Up @@ -115,7 +115,7 @@ android {
`;
const expectedOutput = `apply plugin: "com.android.application"
apply from: "../../node_modules/@sentry/react-native/sentry.gradle"
apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")
android {
ndkVersion rootProject.ext.ndkVersion
Expand Down Expand Up @@ -162,7 +162,7 @@ apply plugin: "io.sentry.android.gradle"
sentry {
}
apply from: "../../node_modules/@sentry/react-native/sentry.gradle"
apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")
android {
ndkVersion rootProject.ext.ndkVersion
Expand Down Expand Up @@ -213,7 +213,54 @@ android {
expect(removeRNSentryGradlePlugin(input)).toBe(input);
});

it('does remove RN SAGP', () => {
it('does remove RN SAGP referenced by node resolved path', () => {
const input = `apply plugin: "com.android.application"
apply plugin: "io.sentry.android.gradle"
sentry {
}
apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")
android {
ndkVersion rootProject.ext.ndkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
namespace "com.samplenewarchitecture"
defaultConfig {
applicationId "com.samplenewarchitecture"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
}
`;
const output = `apply plugin: "com.android.application"
apply plugin: "io.sentry.android.gradle"
sentry {
}
android {
ndkVersion rootProject.ext.ndkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
namespace "com.samplenewarchitecture"
defaultConfig {
applicationId "com.samplenewarchitecture"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
}
`;

expect(removeRNSentryGradlePlugin(input)).toBe(output);
});

it('does remove RN SAGP reference by relative path', () => {
const input = `apply plugin: "com.android.application"
apply plugin: "io.sentry.android.gradle"
Expand Down

0 comments on commit d80d2d7

Please sign in to comment.