Skip to content

Commit 64772a0

Browse files
author
Kureev Alexey
committed
New patch system for Android
1 parent 9744d0c commit 64772a0

File tree

10 files changed

+101
-113
lines changed

10 files changed

+101
-113
lines changed

src/android/isInstalled.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const compose = require('lodash').flowRight;
2-
const readFile = require('./fs').readFile;
1+
const fs = require('fs');
2+
const makeBuildPatch = require(`./patches/makeBuildPatch`);
33

4-
module.exports = function isInstalled(projectConfig, name) {
5-
return compose(
6-
(content) => content.indexOf(`:${name}`) >= 0,
7-
readFile(projectConfig.buildGradlePath)
8-
)();
4+
module.exports = function isInstalled(config, name) {
5+
return fs
6+
.readFileSync(config.buildGradlePath)
7+
.indexOf(makeBuildPatch(name).patch) > -1;
98
};

src/android/patches/applyPatch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs');
2+
3+
module.exports = function applyPatch(file, patch) {
4+
fs.writeFileSync(file, fs
5+
.readFileSync(file, 'utf8')
6+
.replace(patch.pattern, `${patch.pattern}${patch.patch}`)
7+
);
8+
};

src/android/patches/makeBuildPatch.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
module.exports = function makeBuildPatch(name) {
2-
/**
3-
* Replace pattern by patch in the passed content
4-
* @param {String} content Content of the build.gradle file
5-
* @return {String} Patched content of build.gradle
6-
*/
7-
return function applyBuildPatch(content) {
8-
const pattern = `dependencies {`;
9-
const patch = ` compile project(':${name}')`;
10-
11-
return content.replace(pattern, `${pattern}\n${patch}`);
2+
return {
3+
pattern: 'dependencies {\n',
4+
patch: ` compile project(':${name}')\n`,
125
};
136
};
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
const path = require('path');
2+
const isWin = process.platform === 'win32';
23

3-
module.exports = function makeSettingsPatch(name, androidConfig, params, projectConfig) {
4-
const relativeSourceDir = path.relative(
4+
module.exports = function makeSettingsPatch(name, androidConfig, projectConfig) {
5+
var projectDir = path.relative(
56
path.dirname(projectConfig.settingsGradlePath),
67
androidConfig.sourceDir
78
);
89

910
/*
1011
* Fix for Windows
11-
* Backslashes is the escape character and will result in an invalid path in settings.gradle
12+
* Backslashes is the escape character and will result in
13+
* an invalid path in settings.gradle
1214
* https://github.com/rnpm/rnpm/issues/113
1315
*/
14-
const projectDir = process.platform === 'win32'
15-
? relativeSourceDir.replace(/\\/g, '/')
16-
: relativeSourceDir;
16+
if (isWin) {
17+
projectDir = projectDir.replace(/\\/g, '/');
18+
}
1719

18-
/**
19-
* Replace pattern by patch in the passed content
20-
* @param {String} content Content of the Settings.gradle file
21-
* @return {String} Patched content of Settings.gradle
22-
*/
23-
return function applySettingsPatch(content) {
24-
const patch = `include ':${name}'\n` +
20+
return {
21+
pattern: 'include \':app\'\n',
22+
patch: `include ':${name}'\n` +
2523
`project(':${name}').projectDir = ` +
26-
`new File(rootProject.projectDir, '${projectDir}')`;
27-
28-
return `${content.trim()}\n${patch}\n`;
24+
`new File(rootProject.projectDir, '${projectDir}')`,
2925
};
3026
};

src/android/patches/revokePatch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs');
2+
3+
module.exports = function revokePatch(file, patch) {
4+
fs.writeFileSync(file, fs
5+
.readFileSync(file, 'utf8')
6+
.replace(patch.patch, '')
7+
);
8+
};

src/android/registerNativeModule.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
const readFile = require('./fs').readFile;
2-
const writeFile = require('./fs').writeFile;
3-
const compose = require('lodash').flowRight;
1+
const fs = require('fs');
42
const getReactVersion = require('../getReactNativeVersion');
53
const getPrefix = require('./getPrefix');
4+
const isInstalled = require('./isInstalled');
65

7-
const applyPatch = (filePath, patch) =>
8-
compose(writeFile(filePath), patch, readFile(filePath));
6+
const applyPatch = require('./patches/applyPatch');
7+
const makeSettingsPatch = require(`./patches/makeSettingsPatch`);
8+
const makeBuildPatch = require(`./patches/makeBuildPatch`);
9+
10+
module.exports = function registerNativeAndroidModule(
11+
name,
12+
androidConfig,
13+
params,
14+
projectConfig
15+
) {
16+
const buildPatch = makeBuildPatch(name);
17+
const isInstalled = fs
18+
.readFileSync(projectConfig.buildGradlePath)
19+
.indexOf(buildPatch.patch) > -1;
20+
21+
if (isInstalled(projectConfig, name)) {
22+
return false;
23+
}
924

10-
module.exports = function registerNativeAndroidModule(name, androidConfig, params, projectConfig) {
1125
const prefix = getPrefix(getReactVersion(projectConfig.folder));
12-
const makeSettingsPatch = require(`./patches/makeSettingsPatch`);
13-
const makeBuildPatch = require(`./patches/makeBuildPatch`);
1426
const makeMainActivityPatch = require(`./${prefix}/makeMainActivityPatch`);
1527

16-
const performSettingsGradlePatch = applyPatch(
28+
applyPatch(
1729
projectConfig.settingsGradlePath,
18-
makeSettingsPatch.apply(null, arguments)
30+
makeSettingsPatch(name, androidConfig, projectConfig)
1931
);
2032

21-
const performBuildGradlePatch = applyPatch(
22-
projectConfig.buildGradlePath,
23-
makeBuildPatch(name)
24-
);
33+
applyPatch(projectConfig.buildGradlePath, buildPatch);
2534

26-
const performMainActivityPatch = applyPatch(
35+
applyPatch(
2736
projectConfig.mainActivityPath,
2837
makeMainActivityPatch(androidConfig, params)
2938
);
3039

31-
compose(
32-
performSettingsGradlePatch,
33-
performBuildGradlePatch,
34-
performMainActivityPatch
35-
)();
40+
return true;
3641
};

src/android/unregisterNativeModule.js

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
const readFile = require('./fs').readFile;
22
const writeFile = require('./fs').writeFile;
3-
const path = require('path');
43
const compose = require('lodash').flowRight;
54
const getReactVersion = require('../getReactNativeVersion');
65
const getPrefix = require('./getPrefix');
76
const isInstalled = require('./isInstalled');
87

9-
const cut = (scope, pattern) =>
10-
scope.replace(pattern, '');
8+
const revokePatch = require('./patches/revokePatch');
9+
const makeSettingsPatch = require('./patches/makeSettingsPatch');
10+
const makeBuildPatch = require(`./patches/makeBuildPatch`);
1111

12-
module.exports = function unregisterNativeAndroidModule(name, dependencyConfig, projectConfig) {
12+
module.exports = function unregisterNativeAndroidModule(
13+
name,
14+
androidConfig,
15+
projectConfig
16+
) {
1317
const prefix = getPrefix(getReactVersion(projectConfig.folder));
1418

15-
/**
16-
* @param {String} content Content of the Settings.gradle file
17-
* @return {String} Patched content of Settings.gradle
18-
*/
19-
const cutModuleFromSettings = (name) => (content) =>
20-
cut(content, `include ':${name}'\n` +
21-
`project(':${name}').projectDir = ` +
22-
`new File(rootProject.projectDir, '../node_modules/${name}/android')\n`
23-
);
19+
const buildPatch = makeBuildPatch(name);
2420

25-
/**
26-
* Cut module compilation from the project build
27-
* @param {String} content Content of the Build.gradle file
28-
* @return {String} Patched content of Build.gradle
29-
*/
30-
const cutModuleFromBuild = (name) => (content) =>
31-
cut(content, ` compile project(':${name}')\n`);
21+
if (!isInstalled(projectConfig, name)) {
22+
return false;
23+
}
3224

3325
const getAddPackagePatch = require(`./${prefix}/addPackagePatch`);
3426

@@ -39,20 +31,18 @@ module.exports = function unregisterNativeAndroidModule(name, dependencyConfig,
3931
* @return {Function} Patcher function
4032
*/
4133
const makeMainActivityPatcher = (content) => {
42-
const patched = cut(content, dependencyConfig.packageImportPath + '\n');
43-
return cut(patched, getAddPackagePatch(dependencyConfig));
34+
const patched = cut(content, androidConfig.packageImportPath + '\n');
35+
return cut(patched, getAddPackagePatch(androidConfig));
4436
};
4537

46-
const applySettingsGradlePatch = compose(
47-
writeFile(projectConfig.settingsGradlePath),
48-
cutModuleFromSettings(name),
49-
readFile(projectConfig.settingsGradlePath)
38+
revokePatch(
39+
projectConfig.settingsGradlePath,
40+
makeSettingsPatch(name, androidConfig, projectConfig)
5041
);
5142

52-
const applyBuildGradlePatch = compose(
53-
writeFile(projectConfig.buildGradlePath),
54-
cutModuleFromBuild(name),
55-
readFile(projectConfig.buildGradlePath)
43+
revokePatch(
44+
projectConfig.buildGradlePath,
45+
makeBuildPatch(name)
5646
);
5747

5848
const applyMainActivityPatch = compose(
@@ -61,13 +51,7 @@ module.exports = function unregisterNativeAndroidModule(name, dependencyConfig,
6151
readFile(projectConfig.mainActivityPath)
6252
);
6353

64-
if (!isInstalled(projectConfig, name)) {
65-
return false;
66-
}
67-
6854
compose(
69-
applySettingsGradlePatch,
70-
applyBuildGradlePatch,
7155
applyMainActivityPatch
7256
)();
7357

src/unlink.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ log.heading = 'rnpm-link';
1919
* If optional argument [packageName] is provided, it's the only one that's checked
2020
*/
2121
module.exports = function unlink(config, args) {
22-
2322
try {
2423
const project = config.getProjectConfig();
2524
} catch (err) {

test/android/patches/makeBuildPatch.spec.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@ const path = require('path');
33
const chai = require('chai');
44
const expect = chai.expect;
55
const makeBuildPatch = require('../../../src/android/patches/makeBuildPatch');
6+
const applyPatch = require('../../../src/android/patches/applyPatch');
67

78
const name = 'test';
8-
const buildGradle = fs.readFileSync(
9-
path.join(process.cwd(), 'test/fixtures/android/build.gradle'),
10-
'utf-8'
11-
);
12-
const patchedBuildGradle = fs.readFileSync(
13-
path.join(process.cwd(), 'test/fixtures/android/patchedBuild.gradle'),
14-
'utf-8'
15-
);
169

1710
describe('makeBuildPatch', () => {
1811
it('should build a patch function', () => {
19-
expect(makeBuildPatch(name)).to.be.a('function');
12+
expect(makeBuildPatch(name)).to.be.an('object');
2013
});
2114

2215
it('should make a correct patch', () => {
23-
const patch = makeBuildPatch('test');
24-
expect(patch(buildGradle)).to.be.equal(patchedBuildGradle);
16+
expect(makeBuildPatch(name).patch)
17+
.to.be.equal(` compile project(':${name}')\n`);
2518
});
2619
});

test/android/patches/makeSettingsPatch.spec.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,28 @@ const projectConfig = {
99
sourceDir: '/home/project/android/app',
1010
settingsGradlePath: '/home/project/android/settings.gradle',
1111
};
12-
const dependencyConfig = { sourceDir: `/home/project/node_modules/${name}/android` };
13-
const settingsGradle = fs.readFileSync(
14-
path.join(process.cwd(), 'test/fixtures/android/settings.gradle'),
15-
'utf-8'
16-
);
17-
const patchedSettingsGradle = fs.readFileSync(
18-
path.join(process.cwd(), 'test/fixtures/android/patchedSettings.gradle'),
19-
'utf-8'
20-
);
12+
const dependencyConfig = {
13+
sourceDir: `/home/project/node_modules/${name}/android`,
14+
};
2115

2216
describe('makeSettingsPatch', () => {
2317
it('should build a patch function', () => {
2418
expect(
2519
makeSettingsPatch(name, dependencyConfig, {}, projectConfig)
26-
).to.be.a('function');
20+
).to.be.an('object');
2721
});
2822

2923
it('should make a correct patch', () => {
30-
const patch = makeSettingsPatch(name, dependencyConfig, {}, projectConfig);
31-
expect(patch(settingsGradle)).to.be.equal(patchedSettingsGradle);
24+
const projectDir = path.relative(
25+
path.dirname(projectConfig.settingsGradlePath),
26+
dependencyConfig.sourceDir
27+
);
28+
29+
expect(makeSettingsPatch(name, dependencyConfig, projectConfig).patch)
30+
.to.be.equal(
31+
`include ':${name}'\n` +
32+
`project(':${name}').projectDir = ` +
33+
`new File(rootProject.projectDir, '${projectDir}')`
34+
);
3235
});
3336
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy