From 876ca29757a8530842e5b6358daa0fa1b041af73 Mon Sep 17 00:00:00 2001 From: Matei Radu Date: Mon, 11 Mar 2019 20:01:26 +0100 Subject: [PATCH 1/4] Fix Android unlink regression --- .../__tests__/android/makeBuildPatch-test.js | 25 +++++++++++++++++ .../link/android/patches/makeBuildPatch.js | 27 +++++++++++++++++-- .../link/android/unregisterNativeModule.js | 2 +- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/link/__tests__/android/makeBuildPatch-test.js b/packages/cli/src/commands/link/__tests__/android/makeBuildPatch-test.js index 44b5ac3fe..2e4fb76a1 100644 --- a/packages/cli/src/commands/link/__tests__/android/makeBuildPatch-test.js +++ b/packages/cli/src/commands/link/__tests__/android/makeBuildPatch-test.js @@ -10,6 +10,14 @@ import makeBuildPatch from '../../android/patches/makeBuildPatch'; import normalizeProjectName from '../../android/patches/normalizeProjectName'; +import path from 'path'; + +const projectConfig = { + buildGradlePath: path.join( + __dirname, + '../../__fixtures__/android/patchedBuild.gradle', + ), +}; const name = 'test'; const scopedName = '@scoped/test'; @@ -31,6 +39,23 @@ describe('makeBuildPatch', () => { const {installPattern} = makeBuildPatch(name); expect(installPattern.toString()).toEqual(expect.stringContaining(name)); }); + + test.each([ + ['test-impl', " implementation project(':test-impl')\n"], + ['test-compile', " compile project(':test-compile')\n"], + ['test-api', " api project(':test-api')\n"], + [ + 'test-not-there-yet', + " implementation project(':test-not-there-yet')\n", + ], + ])( + 'properly detects the patch string of project %p in build.gradle', + (project, projectPatchString) => { + expect(makeBuildPatch(project, projectConfig.buildGradlePath).patch).toBe( + projectPatchString, + ); + }, + ); }); describe('makeBuildPatchWithScopedPackage', () => { diff --git a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js index 9966d0705..98ed55b99 100644 --- a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js +++ b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js @@ -7,9 +7,10 @@ * @format */ +import fs from 'fs'; import normalizeProjectName from './normalizeProjectName'; -export default function makeBuildPatch(name) { +export default function makeBuildPatch(name, buildGradlePath) { const normalizedProjectName = normalizeProjectName(name); const installPattern = new RegExp( `(implementation|api|compile)\\w*\\s*\\(*project\\(['"]:${normalizedProjectName}['"]\\)`, @@ -18,6 +19,28 @@ export default function makeBuildPatch(name) { return { installPattern, pattern: /[^ \t]dependencies {(\r\n|\n)/, - patch: ` implementation project(':${normalizedProjectName}')\n`, + patch: makePatchString(normalizedProjectName, buildGradlePath), }; } + +function makePatchString(normalizedProjectName, buildGradlePath) { + const defaultPatchString = ` implementation project(':${normalizedProjectName}')\n`; + if (!buildGradlePath) { + return defaultPatchString; + } + + const buildGradle = fs.readFileSync(buildGradlePath); + + const depTypes = ['compile', 'api', 'implementation']; + for (const type of depTypes) { + const depPattern = new RegExp( + `(${type})\\w*\\s*\\(*project\\(['"]:${normalizedProjectName}['"]\\)`, + ); + + if (depPattern.test(buildGradle)) { + return ` ${type} project(':${normalizedProjectName}')\n`; + } + } + + return defaultPatchString; +} diff --git a/packages/cli/src/commands/link/android/unregisterNativeModule.js b/packages/cli/src/commands/link/android/unregisterNativeModule.js index 29680eaae..74098a936 100644 --- a/packages/cli/src/commands/link/android/unregisterNativeModule.js +++ b/packages/cli/src/commands/link/android/unregisterNativeModule.js @@ -22,7 +22,7 @@ export default function unregisterNativeAndroidModule( androidConfig, projectConfig, ) { - const buildPatch = makeBuildPatch(name); + const buildPatch = makeBuildPatch(name, projectConfig.buildGradlePath); const strings = fs.readFileSync(projectConfig.stringsPath, 'utf8'); const params = {}; From 3166cd8b47481559b19242643d9d8c128c37812c Mon Sep 17 00:00:00 2001 From: Matei Radu Date: Mon, 11 Mar 2019 23:57:48 +0100 Subject: [PATCH 2/4] Update regex to accept extra whitespaces --- .../cli/src/commands/link/android/patches/makeBuildPatch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js index 98ed55b99..55b993f4b 100644 --- a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js +++ b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js @@ -13,7 +13,7 @@ import normalizeProjectName from './normalizeProjectName'; export default function makeBuildPatch(name, buildGradlePath) { const normalizedProjectName = normalizeProjectName(name); const installPattern = new RegExp( - `(implementation|api|compile)\\w*\\s*\\(*project\\(['"]:${normalizedProjectName}['"]\\)`, + `(implementation|api|compile)\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`, ); return { @@ -34,7 +34,7 @@ function makePatchString(normalizedProjectName, buildGradlePath) { const depTypes = ['compile', 'api', 'implementation']; for (const type of depTypes) { const depPattern = new RegExp( - `(${type})\\w*\\s*\\(*project\\(['"]:${normalizedProjectName}['"]\\)`, + `(${type})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`, ); if (depPattern.test(buildGradle)) { From 95230e082e96d428b1af81bfda835336387dcd70 Mon Sep 17 00:00:00 2001 From: Matei Radu Date: Tue, 12 Mar 2019 10:38:55 +0100 Subject: [PATCH 3/4] Extract dependency regex string to own function With this approach we have one source of truth for both the regex and the dependency types. --- .../link/android/patches/makeBuildPatch.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js index 55b993f4b..c69503c42 100644 --- a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js +++ b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js @@ -10,10 +10,12 @@ import fs from 'fs'; import normalizeProjectName from './normalizeProjectName'; +const depTypes = ['compile', 'api', 'implementation']; + export default function makeBuildPatch(name, buildGradlePath) { const normalizedProjectName = normalizeProjectName(name); const installPattern = new RegExp( - `(implementation|api|compile)\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`, + buildDepRegExp(normalizedProjectName, ...depTypes), ); return { @@ -31,12 +33,8 @@ function makePatchString(normalizedProjectName, buildGradlePath) { const buildGradle = fs.readFileSync(buildGradlePath); - const depTypes = ['compile', 'api', 'implementation']; for (const type of depTypes) { - const depPattern = new RegExp( - `(${type})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`, - ); - + const depPattern = new RegExp(buildDepRegExp(normalizedProjectName, type)); if (depPattern.test(buildGradle)) { return ` ${type} project(':${normalizedProjectName}')\n`; } @@ -44,3 +42,8 @@ function makePatchString(normalizedProjectName, buildGradlePath) { return defaultPatchString; } + +function buildDepRegExp(normalizedProjectName, ...types) { + const orTypes = types.join('|'); + return `(${orTypes})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`; +} From 19de47196ddbe191d60b0384f96702d0ffa4f271 Mon Sep 17 00:00:00 2001 From: Matei Radu Date: Tue, 12 Mar 2019 10:44:23 +0100 Subject: [PATCH 4/4] Rename dependency types to dependency configs The Gradle documentation calls "configurations" what we have been referring to as "types". --- .../link/android/patches/makeBuildPatch.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js index c69503c42..8ca0b7453 100644 --- a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js +++ b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js @@ -10,12 +10,12 @@ import fs from 'fs'; import normalizeProjectName from './normalizeProjectName'; -const depTypes = ['compile', 'api', 'implementation']; +const depConfigs = ['compile', 'api', 'implementation']; export default function makeBuildPatch(name, buildGradlePath) { const normalizedProjectName = normalizeProjectName(name); const installPattern = new RegExp( - buildDepRegExp(normalizedProjectName, ...depTypes), + buildDepRegExp(normalizedProjectName, ...depConfigs), ); return { @@ -33,17 +33,19 @@ function makePatchString(normalizedProjectName, buildGradlePath) { const buildGradle = fs.readFileSync(buildGradlePath); - for (const type of depTypes) { - const depPattern = new RegExp(buildDepRegExp(normalizedProjectName, type)); + for (const config of depConfigs) { + const depPattern = new RegExp( + buildDepRegExp(normalizedProjectName, config), + ); if (depPattern.test(buildGradle)) { - return ` ${type} project(':${normalizedProjectName}')\n`; + return ` ${config} project(':${normalizedProjectName}')\n`; } } return defaultPatchString; } -function buildDepRegExp(normalizedProjectName, ...types) { - const orTypes = types.join('|'); - return `(${orTypes})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`; +function buildDepRegExp(normalizedProjectName, ...configs) { + const orConfigs = configs.join('|'); + return `(${orConfigs})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`; }