Skip to content

Commit

Permalink
Local cli/android/normalize project name
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Titozzz authored and facebook-github-bot committed Mar 16, 2018
1 parent b02b167 commit dbd4759
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
18 changes: 18 additions & 0 deletions local-cli/link/__tests__/android/makeBuildPatch.spec.js
Expand Up @@ -10,7 +10,11 @@
'use strict'; 'use strict';


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

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


describe('makeBuildPatch', () => { describe('makeBuildPatch', () => {
it('should build a patch function', () => { it('should build a patch function', () => {
Expand All @@ -29,3 +33,17 @@ describe('makeBuildPatch', () => {
expect(installPattern.toString()).toBe(match); 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
Expand Up @@ -11,15 +11,21 @@


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


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


describe('makeSettingsPatch', () => { describe('makeSettingsPatch', () => {
it('should build a patch function', () => { it('should build a patch function', () => {
Expand All @@ -44,3 +50,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
@@ -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
Expand Up @@ -5,14 +5,17 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */


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

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


return { return {
installPattern, installPattern,
pattern: /[^ \t]dependencies {\n/, 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
Expand Up @@ -6,13 +6,17 @@
*/ */


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

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


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



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


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

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

0 comments on commit dbd4759

Please sign in to comment.