Skip to content

Commit 8450d97

Browse files
author
Kureev Alexey
committed
Rework Android patches [0.20]
1 parent 64772a0 commit 8450d97

File tree

7 files changed

+62
-71
lines changed

7 files changed

+62
-71
lines changed

src/android/patches/0.20/addPackagePatch.js

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function makeImportPatch(packageImportPath) {
2+
return {
3+
pattern: 'import com.facebook.react.ReactActivity;',
4+
patch: '\n' + packageImportPath,
5+
};
6+
};

src/android/patches/0.20/makeMainActivityPatch.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function makePackagePatch(packageInstance, params) {
2+
const processedInstance = packageInstance.replace(
3+
/\$\{(\w+)\}/g,
4+
(pattern, paramName) => params[paramName]
5+
? `this.getResources().getString(R.strings.${paramName})`
6+
: null
7+
);
8+
9+
return {
10+
pattern: 'new MainReactPackage()',
11+
patch: ',\n ' + processedInstance,
12+
};
13+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function makeStringsPatch(params) {
2+
const patch = Object.keys(params).map(key =>
3+
` <string moduleConfig="true" name="${key}">${params[key]}</string>`
4+
).join('\n') + '\n';
5+
6+
return {
7+
pattern: '<resources>\n',
8+
patch: patch,
9+
};
10+
};

src/android/registerNativeModule.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const getPrefix = require('./getPrefix');
44
const isInstalled = require('./isInstalled');
55

66
const applyPatch = require('./patches/applyPatch');
7+
const makeStringsPatch = require('./patches/makeStringsPatch');
78
const makeSettingsPatch = require(`./patches/makeSettingsPatch`);
89
const makeBuildPatch = require(`./patches/makeBuildPatch`);
910

@@ -13,28 +14,32 @@ module.exports = function registerNativeAndroidModule(
1314
params,
1415
projectConfig
1516
) {
16-
const buildPatch = makeBuildPatch(name);
17-
const isInstalled = fs
18-
.readFileSync(projectConfig.buildGradlePath)
19-
.indexOf(buildPatch.patch) > -1;
2017

2118
if (isInstalled(projectConfig, name)) {
2219
return false;
2320
}
2421

22+
const buildPatch = makeBuildPatch(name);
2523
const prefix = getPrefix(getReactVersion(projectConfig.folder));
26-
const makeMainActivityPatch = require(`./${prefix}/makeMainActivityPatch`);
24+
const makeImportPatch = require(`./${prefix}/makeImportPatch`);
25+
const makePackagePatch = require(`./${prefix}/makePackagePatch`);
2726

2827
applyPatch(
2928
projectConfig.settingsGradlePath,
3029
makeSettingsPatch(name, androidConfig, projectConfig)
3130
);
3231

3332
applyPatch(projectConfig.buildGradlePath, buildPatch);
33+
applyPatch(projectConfig.stringsPath, makeStringsPatch(params));
34+
35+
applyPatch(
36+
projectConfig.mainActivityPath,
37+
makePackagePatch(androidConfig.packageInstance, params)
38+
);
3439

3540
applyPatch(
3641
projectConfig.mainActivityPath,
37-
makeMainActivityPatch(androidConfig, params)
42+
makeImportPatch(androidConfig.packageImportPath)
3843
);
3944

4045
return true;

src/android/unregisterNativeModule.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,52 @@
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');
64
const isInstalled = require('./isInstalled');
75

86
const revokePatch = require('./patches/revokePatch');
97
const makeSettingsPatch = require('./patches/makeSettingsPatch');
10-
const makeBuildPatch = require(`./patches/makeBuildPatch`);
8+
const makeBuildPatch = require('./patches/makeBuildPatch');
9+
const makeStringsPatch = require('./patches/makeStringsPatch');
1110

1211
module.exports = function unregisterNativeAndroidModule(
1312
name,
1413
androidConfig,
1514
projectConfig
1615
) {
17-
const prefix = getPrefix(getReactVersion(projectConfig.folder));
18-
19-
const buildPatch = makeBuildPatch(name);
2016

2117
if (!isInstalled(projectConfig, name)) {
2218
return false;
2319
}
2420

25-
const getAddPackagePatch = require(`./${prefix}/addPackagePatch`);
26-
27-
/**
28-
* Make a MainActivity.java program patcher
29-
* @param {String} importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
30-
* @param {String} instance Code to instance a package, e.g. new VectorIconsPackage();
31-
* @return {Function} Patcher function
32-
*/
33-
const makeMainActivityPatcher = (content) => {
34-
const patched = cut(content, androidConfig.packageImportPath + '\n');
35-
return cut(patched, getAddPackagePatch(androidConfig));
36-
};
21+
const buildPatch = makeBuildPatch(name);
22+
const prefix = getPrefix(getReactVersion(projectConfig.folder));
23+
const makeImportPatch = require(`./${prefix}/makeImportPatch`);
24+
const makePackagePatch = require(`./${prefix}/makePackagePatch`);
25+
const strings = fs.readFileSync(projectConfig.stringsPath, 'utf8');
26+
var params = {};
27+
28+
strings.replace(
29+
/moduleConfig="true" name="(\w+)">(\w+)</g,
30+
(_, name, value) => params[name] = value
31+
);
3732

3833
revokePatch(
3934
projectConfig.settingsGradlePath,
4035
makeSettingsPatch(name, androidConfig, projectConfig)
4136
);
4237

38+
revokePatch(projectConfig.buildGradlePath, buildPatch);
39+
revokePatch(projectConfig.stringsPath, makeStringsPatch(params));
40+
4341
revokePatch(
44-
projectConfig.buildGradlePath,
45-
makeBuildPatch(name)
42+
projectConfig.mainActivityPath,
43+
makePackagePatch(androidConfig.packageInstance, params)
4644
);
4745

48-
const applyMainActivityPatch = compose(
49-
writeFile(projectConfig.mainActivityPath),
50-
makeMainActivityPatcher,
51-
readFile(projectConfig.mainActivityPath)
46+
revokePatch(
47+
projectConfig.mainActivityPath,
48+
makeImportPatch(androidConfig.packageImportPath)
5249
);
5350

54-
compose(
55-
applyMainActivityPatch
56-
)();
57-
5851
return true;
5952
};

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