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

[android] Refine and test the findLibraryName function #1635

Merged
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 @@ -15,9 +15,13 @@ const manifest = fs.readFileSync(
const mainJavaClass = fs.readFileSync(
path.join(__dirname, './files/Main.java'),
);
const buildGradle = fs.readFileSync(
path.join(__dirname, './files/build.gradle'),
);

function generateValidFileStructure(classFileName: string) {
return {
'build.gradle': buildGradle,
src: {
'AndroidManifest.xml': manifest,
main: {
Expand Down
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"
}
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();
});
});
34 changes: 27 additions & 7 deletions packages/platform-android/src/config/findLibraryName.ts
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 {
Copy link
Member

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 return

Copy link
Member Author

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?

return undefined;
}
}
3 changes: 2 additions & 1 deletion packages/platform-android/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export function dependencyConfig(

const buildTypes = userConfig.buildTypes || [];
const dependencyConfiguration = userConfig.dependencyConfiguration;
const libraryName = userConfig.libraryName || findLibraryName(sourceDir);
const libraryName =
userConfig.libraryName || findLibraryName(root, sourceDir);
const componentDescriptors =
userConfig.componentDescriptors || findComponentDescriptors(root);
const androidMkPath = userConfig.androidMkPath
Expand Down