diff --git a/local-cli/link/__tests__/android/makeBuildPatch.spec.js b/local-cli/link/__tests__/android/makeBuildPatch.spec.js index c37ecc8b8c20ea..6b55e11a731286 100644 --- a/local-cli/link/__tests__/android/makeBuildPatch.spec.js +++ b/local-cli/link/__tests__/android/makeBuildPatch.spec.js @@ -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', () => { @@ -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); + }); +}); diff --git a/local-cli/link/__tests__/android/makeSettingsPatch.spec.js b/local-cli/link/__tests__/android/makeSettingsPatch.spec.js index 223f0d6c405c14..336e2f2b72d346 100644 --- a/local-cli/link/__tests__/android/makeSettingsPatch.spec.js +++ b/local-cli/link/__tests__/android/makeSettingsPatch.spec.js @@ -13,8 +13,11 @@ 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', @@ -22,6 +25,9 @@ const projectConfig = { 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', () => { @@ -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` + ); + }); +}); diff --git a/local-cli/link/__tests__/android/normalizeProjectName.spec.js b/local-cli/link/__tests__/android/normalizeProjectName.spec.js new file mode 100644 index 00000000000000..e4e236aca42f45 --- /dev/null +++ b/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'); + }); +}); diff --git a/local-cli/link/android/patches/makeBuildPatch.js b/local-cli/link/android/patches/makeBuildPatch.js index 67f7d8aac3f1d2..3c4969af22d8db 100644 --- a/local-cli/link/android/patches/makeBuildPatch.js +++ b/local-cli/link/android/patches/makeBuildPatch.js @@ -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` }; }; diff --git a/local-cli/link/android/patches/makeSettingsPatch.js b/local-cli/link/android/patches/makeSettingsPatch.js index a8a6e148333f04..a39de2be17dc16 100644 --- a/local-cli/link/android/patches/makeSettingsPatch.js +++ b/local-cli/link/android/patches/makeSettingsPatch.js @@ -6,6 +6,8 @@ */ const path = require('path'); +const normalizeProjectName = require('./normalizeProjectName'); + const isWin = process.platform === 'win32'; module.exports = function makeSettingsPatch(name, androidConfig, projectConfig) { @@ -13,6 +15,8 @@ module.exports = function makeSettingsPatch(name, androidConfig, projectConfig) path.dirname(projectConfig.settingsGradlePath), androidConfig.sourceDir ); + const normalizedProjectName = normalizeProjectName(name); + /* * Fix for Windows @@ -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`, }; }; diff --git a/local-cli/link/android/patches/normalizeProjectName.js b/local-cli/link/android/patches/normalizeProjectName.js new file mode 100644 index 00000000000000..fd52d1bb108b84 --- /dev/null +++ b/local-cli/link/android/patches/normalizeProjectName.js @@ -0,0 +1,4 @@ + +module.exports = function normalizeProjectName(name) { + return name.replace(/\//g, '_'); +};