Skip to content

Commit

Permalink
Support additional dependency declaration format
Browse files Browse the repository at this point in the history
Previously, `isInstalled` was somewhat naively checking for the presence
of a string in the `build.gradle` file to determine whether or not that
dependency was already linked. I.e.:

`    compile project(':${name}')\n`

…where `name` is replaced with the name of the dependency being checked.

This was inflexible as it only supported that particular format of
`compile` definition. Another, valid `compile` definition follows:

`    compile(project(':example') { … }`

However, this failed the check because it didn't exactly match the
format for which the check was searching the `build.gradle` contents. As
a result, running `react-native link` would incorrectly duplicate the
dependency definition and thus cause a crash upon launching the app.

This change adds an `installPattern` to the object returned from
`makeBuildPatch`, which includes the particular dependency name and is
valid for both `compile` definition formats.
  • Loading branch information
gilesvangruisen committed Jun 12, 2017
1 parent 40b5486 commit dda1374
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions local-cli/link/__fixtures__/android/patchedBuild.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
dependencies {
compile project(':test')
compile(project(':test2')) {
exclude(group: 'org.unwanted', module: 'test10')
}
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:0.18.+"
Expand Down
7 changes: 4 additions & 3 deletions local-cli/link/__tests__/android/isInstalled.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ const projectConfig = {
};

describe('android::isInstalled', () => {
it('should return true when project is already in build.gradle', () =>
it('should return true when project is already in build.gradle', () => {
expect(isInstalled(projectConfig, 'test')).toBeTruthy()
);
expect(isInstalled(projectConfig, 'test2')).toBeTruthy()
});

it('should return false when project is not in build.gradle', () =>
expect(isInstalled(projectConfig, 'test2')).toBeFalsy()
expect(isInstalled(projectConfig, 'test3')).toBeFalsy()
);
});
6 changes: 6 additions & 0 deletions local-cli/link/__tests__/android/makeBuildPatch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ describe('makeBuildPatch', () => {
const {patch} = makeBuildPatch(name);
expect(patch).toBe(` compile project(':${name}')\n`);
});

it('should make a correct install check pattern', () => {
const {installPattern} = makeBuildPatch(name);
const match = `/\\s{4}(compile)(\\(|\\s)(project)\\(\\':${name}\\'\\)(\\)|\\s)/`;
expect(installPattern.toString()).toBe(match);
});
});
5 changes: 2 additions & 3 deletions local-cli/link/android/isInstalled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require('fs');
const makeBuildPatch = require('./patches/makeBuildPatch');

module.exports = function isInstalled(config, name) {
return fs
.readFileSync(config.buildGradlePath)
.indexOf(makeBuildPatch(name).patch) > -1;
const buildGradle = fs.readFileSync(config.buildGradlePath);
return makeBuildPatch(name).installPattern.test(buildGradle);
};
7 changes: 6 additions & 1 deletion local-cli/link/android/patches/makeBuildPatch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module.exports = function makeBuildPatch(name) {
const installPattern = new RegExp(
`\\s{4}(compile)(\\(|\\s)(project)\\(\\\':${name}\\\'\\)(\\)|\\s)`
)

return {
installPattern,
pattern: /[^ \t]dependencies {\n/,
patch: ` compile project(':${name}')\n`,
patch: ` compile project(':${name}')\n`
};
};

0 comments on commit dda1374

Please sign in to comment.