Skip to content

Commit dbd4759

Browse files
Titozzzfacebook-github-bot
authored andcommitted
Local cli/android/normalize project name
Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> Scoped packages are starting to be the new thing, and gradle does not work properly with '/' in the project name, so this PR links them and replaces '/' by '_' . This only affects android. I added tests in the 2 impacted functions + a test file for the normalizer function <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** --> [CLI] [BUGFIX] [local-cli/link/link.js] - On android, Scoped packages will now get the '/' replaced with '_' to ensure gradle works nicely. ⚠️ However if you previously linked scoped packages, they will get linked again. ⚠️ Closes #18275 Differential Revision: D7305227 Pulled By: hramos fbshipit-source-id: 1c95563e884175529692948b29407a7733c44353
1 parent b02b167 commit dbd4759

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

local-cli/link/__tests__/android/makeBuildPatch.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
'use strict';
1111

1212
const makeBuildPatch = require('../../android/patches/makeBuildPatch');
13+
const normalizeProjectName = require('../../android/patches/normalizeProjectName');
14+
1315
const name = 'test';
16+
const scopedName = '@scoped/test';
17+
const normalizedScopedName = normalizeProjectName('@scoped/test');
1418

1519
describe('makeBuildPatch', () => {
1620
it('should build a patch function', () => {
@@ -29,3 +33,17 @@ describe('makeBuildPatch', () => {
2933
expect(installPattern.toString()).toBe(match);
3034
});
3135
});
36+
37+
38+
describe('makeBuildPatchWithScopedPackage', () => {
39+
it('should make a correct patch', () => {
40+
const {patch} = makeBuildPatch(scopedName);
41+
expect(patch).toBe(` compile project(':${normalizedScopedName}')\n`);
42+
});
43+
44+
it('should make a correct install check pattern', () => {
45+
const {installPattern} = makeBuildPatch(scopedName);
46+
const match = `/\\s{4}(compile)(\\(|\\s)(project)\\(\\':${normalizedScopedName}\\'\\)(\\)|\\s)/`;
47+
expect(installPattern.toString()).toBe(match);
48+
});
49+
});

local-cli/link/__tests__/android/makeSettingsPatch.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@
1111

1212
const path = require('path');
1313
const makeSettingsPatch = require('../../android/patches/makeSettingsPatch');
14+
const normalizeProjectName = require('../../android/patches/normalizeProjectName');
1415

1516
const name = 'test';
17+
const scopedName = '@scoped/test';
18+
const normalizedScopedName = normalizeProjectName('@scoped/test');
1619
const projectConfig = {
1720
sourceDir: '/home/project/android/app',
1821
settingsGradlePath: '/home/project/android/settings.gradle',
1922
};
2023
const dependencyConfig = {
2124
sourceDir: `/home/project/node_modules/${name}/android`,
2225
};
26+
const scopedDependencyConfig = {
27+
sourceDir: `/home/project/node_modules/${scopedName}/android`,
28+
};
2329

2430
describe('makeSettingsPatch', () => {
2531
it('should build a patch function', () => {
@@ -44,3 +50,27 @@ describe('makeSettingsPatch', () => {
4450
);
4551
});
4652
});
53+
54+
describe('makeSettingsPatchWithScopedPackage', () => {
55+
it('should build a patch function', () => {
56+
expect(Object.prototype.toString(
57+
makeSettingsPatch(scopedName, scopedDependencyConfig, projectConfig)
58+
)).toBe('[object Object]');
59+
});
60+
61+
it('should make a correct patch', () => {
62+
const projectDir = path.relative(
63+
path.dirname(projectConfig.settingsGradlePath),
64+
scopedDependencyConfig.sourceDir
65+
);
66+
67+
const {patch} = makeSettingsPatch(scopedName, scopedDependencyConfig, projectConfig);
68+
69+
expect(patch)
70+
.toBe(
71+
`include ':${normalizedScopedName}'\n` +
72+
`project(':${normalizedScopedName}').projectDir = ` +
73+
`new File(rootProject.projectDir, '${projectDir}')\n`
74+
);
75+
});
76+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* All rights reserved.
8+
*
9+
* @emails oncall+javascript_foundation
10+
*/
11+
12+
'use strict';
13+
14+
const normalizeProjectName = require('../../android/patches/normalizeProjectName');
15+
16+
const name = 'test';
17+
const scopedName = '@scoped/test';
18+
19+
describe('normalizeProjectName', () => {
20+
it('should replace slashes with underscores', () => {
21+
expect(normalizeProjectName(name))
22+
.toBe('test');
23+
expect(normalizeProjectName(scopedName))
24+
.toBe('@scoped_test');
25+
});
26+
});

local-cli/link/android/patches/makeBuildPatch.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
const normalizeProjectName = require('./normalizeProjectName');
9+
810
module.exports = function makeBuildPatch(name) {
11+
const normalizedProjectName = normalizeProjectName(name);
912
const installPattern = new RegExp(
10-
`\\s{4}(compile)(\\(|\\s)(project)\\(\\\':${name}\\\'\\)(\\)|\\s)`
13+
`\\s{4}(compile)(\\(|\\s)(project)\\(\\\':${normalizedProjectName}\\\'\\)(\\)|\\s)`
1114
);
1215

1316
return {
1417
installPattern,
1518
pattern: /[^ \t]dependencies {\n/,
16-
patch: ` compile project(':${name}')\n`
19+
patch: ` compile project(':${normalizedProjectName}')\n`
1720
};
1821
};

local-cli/link/android/patches/makeSettingsPatch.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
*/
77

88
const path = require('path');
9+
const normalizeProjectName = require('./normalizeProjectName');
10+
911
const isWin = process.platform === 'win32';
1012

1113
module.exports = function makeSettingsPatch(name, androidConfig, projectConfig) {
1214
var projectDir = path.relative(
1315
path.dirname(projectConfig.settingsGradlePath),
1416
androidConfig.sourceDir
1517
);
18+
const normalizedProjectName = normalizeProjectName(name);
19+
1620

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

2731
return {
2832
pattern: '\n',
29-
patch: `include ':${name}'\n` +
30-
`project(':${name}').projectDir = ` +
33+
patch: `include ':${normalizedProjectName}'\n` +
34+
`project(':${normalizedProjectName}').projectDir = ` +
3135
`new File(rootProject.projectDir, '${projectDir}')\n`,
3236
};
3337
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
module.exports = function normalizeProjectName(name) {
3+
return name.replace(/\//g, '_');
4+
};

0 commit comments

Comments
 (0)