From c927e9daebd79a9f49666b24f8debff3fb78d558 Mon Sep 17 00:00:00 2001 From: Hyungu Kang | Airen Date: Mon, 4 Nov 2024 10:35:11 +0900 Subject: [PATCH 1/2] fix: ignore commented lines when finding libraryName --- .../cli-platform-android/src/config/findLibraryName.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/cli-platform-android/src/config/findLibraryName.ts b/packages/cli-platform-android/src/config/findLibraryName.ts index 735d32764..4f8c32286 100644 --- a/packages/cli-platform-android/src/config/findLibraryName.ts +++ b/packages/cli-platform-android/src/config/findLibraryName.ts @@ -17,10 +17,16 @@ export function findLibraryName(root: string, sourceDir: string) { // If not, we check if the library specified it in the build.gradle file. let match: RegExpMatchArray | null = null; if (fs.existsSync(buildGradlePath)) { - const buildGradleContents = fs.readFileSync(buildGradlePath, 'utf-8'); + const buildGradleContents = fs + .readFileSync(buildGradlePath, 'utf-8') + .replace(/\/\/.*$/gm, '') // Remove single-line comments + .replace(/\/\*[\s\S]*?\*\//g, ''); // Remove multi-line comments match = buildGradleContents.match(/libraryName = ["'](.+)["']/); } else if (fs.existsSync(buildGradleKtsPath)) { - const buildGradleContents = fs.readFileSync(buildGradleKtsPath, 'utf-8'); + const buildGradleContents = fs + .readFileSync(buildGradleKtsPath, 'utf-8') + .replace(/\/\/.*$/gm, '') // Remove single-line comments + .replace(/\/\*[\s\S]*?\*\//g, ''); // Remove multi-line comments match = buildGradleContents.match(/libraryName\.set\(["'](.+)["']\)/); } else { return undefined; From 7bfbdefe7d8ed440aafd34ea4788e3a3a0c365cd Mon Sep 17 00:00:00 2001 From: Hyungu Kang | Airen Date: Mon, 4 Nov 2024 10:37:16 +0900 Subject: [PATCH 2/2] test: add test cases for handling commented lines --- .../config/__tests__/findLibraryName.test.ts | 90 ++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/packages/cli-platform-android/src/config/__tests__/findLibraryName.test.ts b/packages/cli-platform-android/src/config/__tests__/findLibraryName.test.ts index 96998d24e..71565e5a7 100644 --- a/packages/cli-platform-android/src/config/__tests__/findLibraryName.test.ts +++ b/packages/cli-platform-android/src/config/__tests__/findLibraryName.test.ts @@ -49,10 +49,75 @@ const packageJsonWithoutCodegenConfig = ` } `; +const buildGradleWithSingleLineComments = ` + apply plugin: "com.android.application" + apply plugin: "com.facebook.react" + + // react { + // libraryName = "justalibrary" + // } + `; + +const buildGradleWithMultiLineComments = ` + apply plugin: "com.android.application" + apply plugin: "com.facebook.react" + + /* + react { + libraryName = "justalibrary" + } + */ + `; + +const buildGradleValidWithComments = ` + apply plugin: "com.android.application" + apply plugin: "com.facebook.react" + + // react { + // libraryName = "justalibrary" + // } + react { + libraryName = "justalibrary2" + } + `; + +const buildGradleKtsWithSingleLineComments = ` + apply(id = "com.android.application") + apply(id = "com.facebook.react") + + // react { + // libraryName.set("justalibrary") + // } + `; + +const buildGradleKtsWithMultiLineComments = ` + apply(id = "com.android.application") + apply(id = "com.facebook.react") + + /* + react { + libraryName.set("justalibrary") + } + */ + `; + describe('android::findLibraryName', () => { beforeAll(() => { fs.__setMockFilesystem({ - empty: {}, + empty: { + singlelinecomments: { + 'build.gradle': buildGradleWithSingleLineComments, + }, + multilinecomments: { + 'build.gradle': buildGradleWithMultiLineComments, + }, + singllinecommentskts: { + 'build.gradle.kts': buildGradleKtsWithSingleLineComments, + }, + multilinecommentskts: { + 'build.gradle.kts': buildGradleKtsWithMultiLineComments, + }, + }, valid: { android: mocks.valid, singlequotes: { @@ -73,6 +138,9 @@ describe('android::findLibraryName', () => { 'build.gradle': buildGradleWithSingleQuotes, }, }, + withcomments: { + 'build.gradle': buildGradleValidWithComments, + }, }, }); }); @@ -98,6 +166,10 @@ describe('android::findLibraryName', () => { ).toBe('my-awesome-library'); }); + it('returns the library name if declared in the build.gradle file with comments', () => { + expect(findLibraryName('/', '/valid/withcomments')).toBe('justalibrary2'); + }); + it('falls back to reading from build.gradle when codegenConfig is not there', () => { expect( findLibraryName( @@ -110,4 +182,20 @@ describe('android::findLibraryName', () => { it('returns null if there is no build.gradle file', () => { expect(findLibraryName('/', '/empty')).toBeUndefined(); }); + + it('returns null if the library name is declared as a single line comment', () => { + expect(findLibraryName('/', '/empty/singlelinecomments')).toBeUndefined(); + }); + + it('returns null if the library name is declared as a multi line comment', () => { + expect(findLibraryName('/', '/empty/multilinecomments')).toBeUndefined(); + }); + + it('returns null if the library name is declared as a single line comment in build.gradle.kts', () => { + expect(findLibraryName('/', '/empty/singllinecommentskts')).toBeUndefined(); + }); + + it('returns null if the library name is declared as a multi line comment in build.gradle.kts', () => { + expect(findLibraryName('/', '/empty/multilinecommentskts')).toBeUndefined(); + }); });