Skip to content

Commit

Permalink
fix: android unlink regression (#221)
Browse files Browse the repository at this point in the history
* 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".
  • Loading branch information
matei-radu authored and grabbou committed Mar 12, 2019
1 parent 0691633 commit e4ab21e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
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

0 comments on commit e4ab21e

Please sign in to comment.