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

Local cli/android/normalize project name #18275

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
18 changes: 18 additions & 0 deletions local-cli/link/__tests__/android/makeBuildPatch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
'use strict';

const makeBuildPatch = require('../../android/patches/makeBuildPatch');
const normalizeProjectName = require('../../android/patches/normalizeProjectName');

const name = 'test';
const scopedName = '@scoped/test';
const normalizedScopedName = normalizeProjectName('@scoped/test');

describe('makeBuildPatch', () => {
it('should build a patch function', () => {
Expand All @@ -31,3 +35,17 @@ describe('makeBuildPatch', () => {
expect(installPattern.toString()).toBe(match);
});
});


describe('makeBuildPatchWithScopedPackage', () => {
it('should make a correct patch', () => {
const {patch} = makeBuildPatch(scopedName);
expect(patch).toBe(` compile project(':${normalizedScopedName}')\n`);
});

it('should make a correct install check pattern', () => {
const {installPattern} = makeBuildPatch(scopedName);
const match = `/\\s{4}(compile)(\\(|\\s)(project)\\(\\':${normalizedScopedName}\\'\\)(\\)|\\s)/`;
expect(installPattern.toString()).toBe(match);
});
});
30 changes: 30 additions & 0 deletions local-cli/link/__tests__/android/makeSettingsPatch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@

const path = require('path');
const makeSettingsPatch = require('../../android/patches/makeSettingsPatch');
const normalizeProjectName = require('../../android/patches/normalizeProjectName');

const name = 'test';
const scopedName = '@scoped/test';
const normalizedScopedName = normalizeProjectName('@scoped/test');
const projectConfig = {
sourceDir: '/home/project/android/app',
settingsGradlePath: '/home/project/android/settings.gradle',
};
const dependencyConfig = {
sourceDir: `/home/project/node_modules/${name}/android`,
};
const scopedDependencyConfig = {
sourceDir: `/home/project/node_modules/${scopedName}/android`,
};

describe('makeSettingsPatch', () => {
it('should build a patch function', () => {
Expand All @@ -46,3 +52,27 @@ describe('makeSettingsPatch', () => {
);
});
});

describe('makeSettingsPatchWithScopedPackage', () => {
it('should build a patch function', () => {
expect(Object.prototype.toString(
makeSettingsPatch(scopedName, scopedDependencyConfig, projectConfig)
)).toBe('[object Object]');
});

it('should make a correct patch', () => {
const projectDir = path.relative(
path.dirname(projectConfig.settingsGradlePath),
scopedDependencyConfig.sourceDir
);

const {patch} = makeSettingsPatch(scopedName, scopedDependencyConfig, projectConfig);

expect(patch)
.toBe(
`include ':${normalizedScopedName}'\n` +
`project(':${normalizedScopedName}').projectDir = ` +
`new File(rootProject.projectDir, '${projectDir}')\n`
);
});
});
26 changes: 26 additions & 0 deletions local-cli/link/__tests__/android/normalizeProjectName.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* All rights reserved.
*
* @emails oncall+javascript_foundation
*/

'use strict';

const normalizeProjectName = require('../../android/patches/normalizeProjectName');

const name = 'test';
const scopedName = '@scoped/test';

describe('normalizeProjectName', () => {
it('should replace slashes with underscores', () => {
expect(normalizeProjectName(name))
.toBe('test');
expect(normalizeProjectName(scopedName))
.toBe('@scoped_test');
});
});
7 changes: 5 additions & 2 deletions local-cli/link/android/patches/makeBuildPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

const normalizeProjectName = require('./normalizeProjectName');

module.exports = function makeBuildPatch(name) {
const normalizedProjectName = normalizeProjectName(name);
const installPattern = new RegExp(
`\\s{4}(compile)(\\(|\\s)(project)\\(\\\':${name}\\\'\\)(\\)|\\s)`
`\\s{4}(compile)(\\(|\\s)(project)\\(\\\':${normalizedProjectName}\\\'\\)(\\)|\\s)`
);

return {
installPattern,
pattern: /[^ \t]dependencies {\n/,
patch: ` compile project(':${name}')\n`
patch: ` compile project(':${normalizedProjectName}')\n`
};
};
8 changes: 6 additions & 2 deletions local-cli/link/android/patches/makeSettingsPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
*/

const path = require('path');
const normalizeProjectName = require('./normalizeProjectName');

const isWin = process.platform === 'win32';

module.exports = function makeSettingsPatch(name, androidConfig, projectConfig) {
var projectDir = path.relative(
path.dirname(projectConfig.settingsGradlePath),
androidConfig.sourceDir
);
const normalizedProjectName = normalizeProjectName(name);


/*
* Fix for Windows
Expand All @@ -26,8 +30,8 @@ module.exports = function makeSettingsPatch(name, androidConfig, projectConfig)

return {
pattern: '\n',
patch: `include ':${name}'\n` +
`project(':${name}').projectDir = ` +
patch: `include ':${normalizedProjectName}'\n` +
`project(':${normalizedProjectName}').projectDir = ` +
`new File(rootProject.projectDir, '${projectDir}')\n`,
};
};
4 changes: 4 additions & 0 deletions local-cli/link/android/patches/normalizeProjectName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

module.exports = function normalizeProjectName(name) {
return name.replace(/\//g, '_');
};