Skip to content

Commit e4ab21e

Browse files
matei-radugrabbou
authored andcommitted
fix: android unlink regression (#221)
* Fix Android unlink regression * Update regex to accept extra whitespaces * Extract dependency regex string to own function With this approach we have one source of truth for both the regex and the dependency types. * Rename dependency types to dependency configs The Gradle documentation calls "configurations" what we have been referring to as "types".
1 parent 0691633 commit e4ab21e

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

packages/cli/src/commands/link/__tests__/android/makeBuildPatch-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
import makeBuildPatch from '../../android/patches/makeBuildPatch';
1212
import normalizeProjectName from '../../android/patches/normalizeProjectName';
13+
import path from 'path';
14+
15+
const projectConfig = {
16+
buildGradlePath: path.join(
17+
__dirname,
18+
'../../__fixtures__/android/patchedBuild.gradle',
19+
),
20+
};
1321

1422
const name = 'test';
1523
const scopedName = '@scoped/test';
@@ -31,6 +39,23 @@ describe('makeBuildPatch', () => {
3139
const {installPattern} = makeBuildPatch(name);
3240
expect(installPattern.toString()).toEqual(expect.stringContaining(name));
3341
});
42+
43+
test.each([
44+
['test-impl', " implementation project(':test-impl')\n"],
45+
['test-compile', " compile project(':test-compile')\n"],
46+
['test-api', " api project(':test-api')\n"],
47+
[
48+
'test-not-there-yet',
49+
" implementation project(':test-not-there-yet')\n",
50+
],
51+
])(
52+
'properly detects the patch string of project %p in build.gradle',
53+
(project, projectPatchString) => {
54+
expect(makeBuildPatch(project, projectConfig.buildGradlePath).patch).toBe(
55+
projectPatchString,
56+
);
57+
},
58+
);
3459
});
3560

3661
describe('makeBuildPatchWithScopedPackage', () => {

packages/cli/src/commands/link/android/patches/makeBuildPatch.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,45 @@
77
* @format
88
*/
99

10+
import fs from 'fs';
1011
import normalizeProjectName from './normalizeProjectName';
1112

12-
export default function makeBuildPatch(name) {
13+
const depConfigs = ['compile', 'api', 'implementation'];
14+
15+
export default function makeBuildPatch(name, buildGradlePath) {
1316
const normalizedProjectName = normalizeProjectName(name);
1417
const installPattern = new RegExp(
15-
`(implementation|api|compile)\\w*\\s*\\(*project\\(['"]:${normalizedProjectName}['"]\\)`,
18+
buildDepRegExp(normalizedProjectName, ...depConfigs),
1619
);
1720

1821
return {
1922
installPattern,
2023
pattern: /[^ \t]dependencies {(\r\n|\n)/,
21-
patch: ` implementation project(':${normalizedProjectName}')\n`,
24+
patch: makePatchString(normalizedProjectName, buildGradlePath),
2225
};
2326
}
27+
28+
function makePatchString(normalizedProjectName, buildGradlePath) {
29+
const defaultPatchString = ` implementation project(':${normalizedProjectName}')\n`;
30+
if (!buildGradlePath) {
31+
return defaultPatchString;
32+
}
33+
34+
const buildGradle = fs.readFileSync(buildGradlePath);
35+
36+
for (const config of depConfigs) {
37+
const depPattern = new RegExp(
38+
buildDepRegExp(normalizedProjectName, config),
39+
);
40+
if (depPattern.test(buildGradle)) {
41+
return ` ${config} project(':${normalizedProjectName}')\n`;
42+
}
43+
}
44+
45+
return defaultPatchString;
46+
}
47+
48+
function buildDepRegExp(normalizedProjectName, ...configs) {
49+
const orConfigs = configs.join('|');
50+
return `(${orConfigs})\\w*\\s*\\(*project\\s*\\(['"]:${normalizedProjectName}['"]\\)`;
51+
}

packages/cli/src/commands/link/android/unregisterNativeModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function unregisterNativeAndroidModule(
2222
androidConfig,
2323
projectConfig,
2424
) {
25-
const buildPatch = makeBuildPatch(name);
25+
const buildPatch = makeBuildPatch(name, projectConfig.buildGradlePath);
2626
const strings = fs.readFileSync(projectConfig.stringsPath, 'utf8');
2727
const params = {};
2828

0 commit comments

Comments
 (0)