Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore commented lines when finding libraryName #2542

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -73,6 +138,9 @@ describe('android::findLibraryName', () => {
'build.gradle': buildGradleWithSingleQuotes,
},
},
withcomments: {
'build.gradle': buildGradleValidWithComments,
},
},
});
});
Expand All @@ -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(
Expand All @@ -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();
});
});
10 changes: 8 additions & 2 deletions packages/cli-platform-android/src/config/findLibraryName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading