-
Notifications
You must be signed in to change notification settings - Fork 907
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
[android] Refine and test the findLibraryName function #1635
Merged
thymikee
merged 1 commit into
react-native-community:main
from
cortinico:nc/fix-library-name-assumptions
Jun 30, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/platform-android/src/config/__fixtures__/files/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apply package: "com.android.application" | ||
apply package: "com.facebook.react" | ||
|
||
react { | ||
libraryName = "justalibrary" | ||
} |
91 changes: 91 additions & 0 deletions
91
packages/platform-android/src/config/__tests__/findLibraryName.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {findLibraryName} from '../findLibraryName'; | ||
import * as mocks from '../__fixtures__/android'; | ||
|
||
jest.mock('path'); | ||
jest.mock('fs'); | ||
|
||
const fs = require('fs'); | ||
|
||
const buildGradleWithSingleQuotes = ` | ||
apply plugin: "com.android.application" | ||
apply plugin: "com.facebook.react" | ||
|
||
react { | ||
libraryName = 'justalibrary' | ||
} | ||
`; | ||
|
||
const buildGradleKts = ` | ||
apply(id = "com.android.application") | ||
apply(id = "com.facebook.react") | ||
|
||
react { | ||
libraryName.set("justalibrary") | ||
} | ||
`; | ||
|
||
const packageJsonWithCodegenConfig = ` | ||
{ | ||
"name": "my-awesome-library", | ||
"version": "0.0.1", | ||
"codegenConfig": { | ||
"name": "my-awesome-library" | ||
} | ||
} | ||
`; | ||
|
||
describe('android::findLibraryName', () => { | ||
beforeAll(() => { | ||
fs.__setMockFilesystem({ | ||
empty: {}, | ||
valid: { | ||
android: mocks.valid, | ||
singlequotes: { | ||
'build.gradle': buildGradleWithSingleQuotes, | ||
}, | ||
gradlekts: { | ||
'build.gradle.kts': buildGradleKts, | ||
}, | ||
withcodegenconfig: { | ||
'package.json': packageJsonWithCodegenConfig, | ||
android: { | ||
'build.gradle': buildGradleWithSingleQuotes, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns the library name if declared in the build.gradle file', () => { | ||
expect(findLibraryName('/', '/valid/android')).toBe('justalibrary'); | ||
}); | ||
|
||
it('returns the library name if declared with single quotes in the build.gradle file', () => { | ||
expect(findLibraryName('/', '/valid/singlequotes')).toBe('justalibrary'); | ||
}); | ||
|
||
it('returns the library name if declared with inside a build.gradle.kts file', () => { | ||
expect(findLibraryName('/', '/valid/singlequotes')).toBe('justalibrary'); | ||
}); | ||
|
||
it('returns the library name if defined inside codegenConfig', () => { | ||
expect( | ||
findLibraryName( | ||
'/valid/withcodegenconfig', | ||
'/valid/withcodegenconfig/android', | ||
), | ||
).toBe('my-awesome-library'); | ||
}); | ||
|
||
it('returns null if there is no build.gradle file', () => { | ||
expect(findLibraryName('/', '/empty')).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
export function findLibraryName(sourceDir: string) { | ||
export function findLibraryName(root: string, sourceDir: string) { | ||
const packageJsonPath = path.join(root, 'package.json'); | ||
const buildGradlePath = path.join(sourceDir, 'build.gradle'); | ||
if (fs.existsSync(buildGradlePath)) { | ||
const buildGradleContents = fs.readFileSync(buildGradlePath, 'utf-8'); | ||
const match = buildGradleContents.match(/libraryName = "(.+)"/); | ||
if (match) { | ||
return match[1]; | ||
const buildGradleKtsPath = path.join(sourceDir, 'build.gradle.kts'); | ||
|
||
// We first check if there is a codegenConfig.name inside the package.json file. | ||
if (fs.existsSync(packageJsonPath)) { | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); | ||
if (packageJson.codegenConfig.name) { | ||
return packageJson.codegenConfig.name; | ||
} | ||
} | ||
return undefined; | ||
|
||
// If not, we check if the library specified it in the build.gradle file. | ||
let buildGradleContents = ''; | ||
if (fs.existsSync(buildGradlePath)) { | ||
buildGradleContents = fs.readFileSync(buildGradlePath, 'utf-8'); | ||
} else if (fs.existsSync(buildGradleKtsPath)) { | ||
buildGradleContents = fs.readFileSync(buildGradleKtsPath, 'utf-8'); | ||
} else { | ||
return undefined; | ||
} | ||
|
||
const match = buildGradleContents.match(/libraryName = ["'](.+)["']/); | ||
|
||
if (match) { | ||
return match[1]; | ||
} else { | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for
else
when we returnThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I follow up on this in a separate PR?