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..8ca0b7453 100644 --- a/packages/cli/src/commands/link/android/patches/makeBuildPatch.js +++ b/packages/cli/src/commands/link/android/patches/makeBuildPatch.js @@ -7,17 +7,45 @@ * @format */ +import fs from 'fs'; import normalizeProjectName from './normalizeProjectName'; -export default function makeBuildPatch(name) { +const depConfigs = ['compile', 'api', 'implementation']; + +export default function makeBuildPatch(name, buildGradlePath) { const normalizedProjectName = normalizeProjectName(name); const installPattern = new RegExp( - `(implementation|api|compile)\\w*\\s*\\(*project\\(['"]:${normalizedProjectName}['"]\\)`, + buildDepRegExp(normalizedProjectName, ...depConfigs), ); 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); + + for (const config of depConfigs) { + const depPattern = new RegExp( + buildDepRegExp(normalizedProjectName, config), + ); + if (depPattern.test(buildGradle)) { + return ` ${config} project(':${normalizedProjectName}')\n`; + } + } + + return defaultPatchString; +} + +function buildDepRegExp(normalizedProjectName, ...configs) { + const orConfigs = configs.join('|'); + return `(${orConfigs})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`; +} 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 = {};