Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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', () => {
Expand Down
34 changes: 31 additions & 3 deletions packages/cli/src/commands/link/android/patches/makeBuildPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}['"]\\)`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down