From 45e2f72cf2099e15d51b19570f54b5b83b5bcadc Mon Sep 17 00:00:00 2001 From: Matei Radu Date: Mon, 10 Sep 2018 23:19:43 +0200 Subject: [PATCH 1/2] Fix regression in Android unlink command Commit 4dfdec9b28074b311ffd75084d9d2a3cbd41a800 introduced a regression in the `unlink` command for Android projects: native modules previously linked with the now-deprecated `compile` configuration will not be detected anymore and by extension will not be unlinked. `unlink` should be aware of both new and deprecated options to ensure functionality also in existing projects that previously linked modules with the old convention. This commit fixes the regression by: - first, `makeDeprecatedNativeModule` is added so to have the deprecated patch line that could be present in the Gradle build file. - updating the Android `isInstalled` to check, for a given package name, for both `compile` and `implementation` dependencies records in `build.gradle`. - Android `unregisterNativeModule` will proceed to revoke both possible patch lines from `build.gradle`. --- local-cli/link/android/isInstalled.js | 6 ++++- .../patches/makeDeprecatedBuildPatch.js | 23 +++++++++++++++++++ .../link/android/unregisterNativeModule.js | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 local-cli/link/android/patches/makeDeprecatedBuildPatch.js diff --git a/local-cli/link/android/isInstalled.js b/local-cli/link/android/isInstalled.js index 43a53396e9228d..f133619973fbff 100644 --- a/local-cli/link/android/isInstalled.js +++ b/local-cli/link/android/isInstalled.js @@ -9,8 +9,12 @@ const fs = require('fs'); const makeBuildPatch = require('./patches/makeBuildPatch'); +const makeDeprecatedBuildPatch = require('./patches/makeDeprecatedBuildPatch'); module.exports = function isInstalled(config, name) { const buildGradle = fs.readFileSync(config.buildGradlePath); - return makeBuildPatch(name).installPattern.test(buildGradle); + return ( + makeBuildPatch(name).installPattern.test(buildGradle) || + makeDeprecatedBuildPatch(name).installPattern.test(buildGradle) + ); }; diff --git a/local-cli/link/android/patches/makeDeprecatedBuildPatch.js b/local-cli/link/android/patches/makeDeprecatedBuildPatch.js new file mode 100644 index 00000000000000..c2dc93810430e8 --- /dev/null +++ b/local-cli/link/android/patches/makeDeprecatedBuildPatch.js @@ -0,0 +1,23 @@ +/** + * 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. + * + * @format + */ + +const normalizeProjectName = require('./normalizeProjectName'); + +module.exports = function makeDeprecatedBuildPatch(name) { + const normalizedProjectName = normalizeProjectName(name); + const installPattern = new RegExp( + `\\s{4}(compile)(\\(|\\s)(project)\\(\\\':${normalizedProjectName}\\\'\\)(\\)|\\s)`, + ); + + return { + installPattern, + pattern: /[^ \t]dependencies {(\r\n|\n)/, + patch: ` compile project(':${normalizedProjectName}')\n`, + }; +}; diff --git a/local-cli/link/android/unregisterNativeModule.js b/local-cli/link/android/unregisterNativeModule.js index 880070afa503c4..37dd7a368e17e8 100644 --- a/local-cli/link/android/unregisterNativeModule.js +++ b/local-cli/link/android/unregisterNativeModule.js @@ -13,6 +13,7 @@ const toCamelCase = require('lodash').camelCase; const revokePatch = require('./patches/revokePatch'); const makeSettingsPatch = require('./patches/makeSettingsPatch'); const makeBuildPatch = require('./patches/makeBuildPatch'); +const makeDeprecatedBuildPatch = require('./patches/makeDeprecatedBuildPatch'); const makeStringsPatch = require('./patches/makeStringsPatch'); const makeImportPatch = require('./patches/makeImportPatch'); const makePackagePatch = require('./patches/makePackagePatch'); @@ -23,6 +24,7 @@ module.exports = function unregisterNativeAndroidModule( projectConfig, ) { const buildPatch = makeBuildPatch(name); + const deprecatedBuildPatch = makeDeprecatedBuildPatch(name); const strings = fs.readFileSync(projectConfig.stringsPath, 'utf8'); var params = {}; @@ -39,6 +41,7 @@ module.exports = function unregisterNativeAndroidModule( ); revokePatch(projectConfig.buildGradlePath, buildPatch); + revokePatch(projectConfig.buildGradlePath, deprecatedBuildPatch); revokePatch(projectConfig.stringsPath, makeStringsPatch(params, name)); revokePatch( From 43a4a1f2eb1ee8ddb9191e456d5e2535eacd46ec Mon Sep 17 00:00:00 2001 From: Matei Radu Date: Tue, 11 Sep 2018 00:35:26 +0200 Subject: [PATCH 2/2] Add additional test for Android isInstalled --- local-cli/link/__fixtures__/android/patchedBuild.gradle | 1 + local-cli/link/__tests__/android/isInstalled.spec.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/local-cli/link/__fixtures__/android/patchedBuild.gradle b/local-cli/link/__fixtures__/android/patchedBuild.gradle index 425aaaf7f44427..d7b58b7c7a7d38 100644 --- a/local-cli/link/__fixtures__/android/patchedBuild.gradle +++ b/local-cli/link/__fixtures__/android/patchedBuild.gradle @@ -3,6 +3,7 @@ dependencies { implementation(project(':test2')) { exclude(group: 'org.unwanted', module: 'test10') } + compile project(':testDeprecated') implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "com.android.support:appcompat-v7:27.1.1" implementation "com.facebook.react:react-native:+" diff --git a/local-cli/link/__tests__/android/isInstalled.spec.js b/local-cli/link/__tests__/android/isInstalled.spec.js index a90649f0339f5f..1955b1dd011453 100644 --- a/local-cli/link/__tests__/android/isInstalled.spec.js +++ b/local-cli/link/__tests__/android/isInstalled.spec.js @@ -26,6 +26,10 @@ describe('android::isInstalled', () => { expect(isInstalled(projectConfig, 'test2')).toBeTruthy(); }); + it('should return true when project is already in build.gradle using compile', () => { + expect(isInstalled(projectConfig, 'testDeprecated')).toBeTruthy(); + }); + it('should return false when project is not in build.gradle', () => expect(isInstalled(projectConfig, 'test3')).toBeFalsy()); });