Skip to content

Commit 100ac61

Browse files
author
Alexey
committed
Merge pull request #22 from rnpm/feature/support-0.18
Support react-native 0.18+
2 parents 68aafa7 + 5dd07e8 commit 100ac61

20 files changed

Lines changed: 528 additions & 56 deletions

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"mime": "^1.3.4",
4141
"npmlog": "^2.0.0",
4242
"plist": "^1.2.0",
43+
"semver": "^5.1.0",
4344
"xcode": "^0.8.2"
4445
},
4546
"devDependencies": {

src/android/getPrefix.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require('path');
2+
const semver = require('semver');
3+
4+
module.exports = function getPrefix(config) {
5+
const rnpkg = require(
6+
path.join(config.folder, 'node_modules', 'react-native', 'package.json')
7+
);
8+
9+
var prefix = 'patches/0.18';
10+
11+
if (semver.lt(rnpkg.version, '0.18.0')) {
12+
prefix = 'patches/0.17';
13+
}
14+
15+
return prefix;
16+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const append = (scope, pattern, patch) =>
2+
scope.replace(pattern, `${pattern}\n${patch}`);
3+
4+
module.exports = function makeMainActivityPatch(config) {
5+
const importPattern = `import android.app.Activity;`;
6+
const packagePattern = `.addPackage(new MainReactPackage())`;
7+
8+
/**
9+
* Make a MainActivity.java program patcher
10+
* @param {String} importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
11+
* @param {String} instance Code to instance a package, e.g. new VectorIconsPackage();
12+
* @return {Function} Patcher function
13+
*/
14+
return function applyMainActivityPatch(content) {
15+
const patched = append(
16+
content,
17+
importPattern,
18+
config.packageImportPath
19+
);
20+
21+
return append(
22+
patched,
23+
packagePattern,
24+
` .addPackage(${config.packageInstance})`
25+
);
26+
};
27+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const append = (scope, pattern, patch) =>
2+
scope.replace(pattern, `${pattern}${patch}`);
3+
4+
module.exports = function makeMainActivityPatch(config) {
5+
const importPattern = 'import com.facebook.react.ReactActivity;';
6+
const packagePattern = 'new MainReactPackage()';
7+
8+
/**
9+
* Make a MainActivity.java program patcher
10+
* @param {String} importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
11+
* @param {String} instance Code to instance a package, e.g. new VectorIconsPackage();
12+
* @return {Function} Patcher function
13+
*/
14+
return function applyMainActivityPatch(content) {
15+
const patched = append(
16+
content,
17+
importPattern,
18+
'\n' + config.packageImportPath
19+
);
20+
21+
return append(
22+
patched,
23+
packagePattern,
24+
',\n ' + config.packageInstance
25+
);
26+
};
27+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
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}`);
12+
};
13+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path');
2+
3+
module.exports = function makeSettingsPatch(name, dependencyConfig, projectConfig) {
4+
const relative = path.relative(
5+
projectConfig.sourceDir,
6+
dependencyConfig.sourceDir
7+
);
8+
9+
/**
10+
* Replace pattern by patch in the passed content
11+
* @param {String} content Content of the Settings.gradle file
12+
* @return {String} Patched content of Settings.gradle
13+
*/
14+
return function applySettingsPatch(content) {
15+
const pattern = `include ':app'`;
16+
const patch = `include ':${name}'\n` +
17+
`project(':${name}').projectDir = ` +
18+
`new File(rootProject.projectDir, '${relative}')`;
19+
20+
return content.replace(pattern, `${pattern}\n${patch}`);
21+
};
22+
};
Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
const fs = require('fs-extra');
2-
const path = require('path');
32
const compose = require('lodash.flowright');
4-
5-
const SETTINGS_PATCH_PATTERN = `include ':app'`;
6-
const BUILD_PATCH_PATTERN = `dependencies {`;
7-
const MAIN_ACTIVITY_IMPORT_PATTERN = `import android.app.Activity;`;
8-
const MAIN_ACTIVITY_PACKAGE_PATTERN = `.addPackage(new MainReactPackage())`;
3+
const getPrefix = require('./getPrefix');
94

105
const readFile = (file) =>
116
() => fs.readFileSync(file, 'utf8');
@@ -14,81 +9,49 @@ const writeFile = (file, content) => content ?
149
fs.writeFileSync(file, content, 'utf8') :
1510
(c) => fs.writeFileSync(file, c, 'utf8');
1611

17-
const replace = (scope, pattern, patch) =>
18-
scope.replace(pattern, `${pattern}\n${patch}`);
19-
2012
module.exports = function registerNativeAndroidModule(name, dependencyConfig, projectConfig) {
21-
const BUILD_PATCH = ` compile project(':${name}')`;
22-
const SETTINGS_PATCH = `include ':${name}'\n` +
23-
`project(':${name}').projectDir = ` +
24-
`new File(rootProject.projectDir, '../node_modules/${name}/${dependencyConfig.sourceDir}')`;
25-
26-
/**
27-
* Replace SETTINGS_PATCH_PATTERN by patch in the passed content
28-
* @param {String} content Content of the Settings.gradle file
29-
* @return {String} Patched content of Settings.gradle
30-
*/
31-
const patchProjectSettings = (content) =>
32-
replace(content, SETTINGS_PATCH_PATTERN, SETTINGS_PATCH);
33-
34-
/**
35-
* Replace BUILD_PATCH_PATTERN by patch in the passed content
36-
* @param {String} content Content of the Build.gradle file
37-
* @return {String} Patched content of Build.gradle
38-
*/
39-
const patchProjectBuild = (content) =>
40-
replace(content, BUILD_PATCH_PATTERN, BUILD_PATCH);
41-
42-
const getMainActivityPatch = () =>
43-
` .addPackage(${dependencyConfig.packageInstance})`;
13+
const prefix = getPrefix(projectConfig);
4414

45-
/**
46-
* Make a MainActivity.java program patcher
47-
* @param {String} importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
48-
* @param {String} instance Code to instance a package, e.g. new VectorIconsPackage();
49-
* @return {Function} Patcher function
50-
*/
51-
const makeMainActivityPatcher = (content) => {
52-
const patched = replace(
53-
content, MAIN_ACTIVITY_IMPORT_PATTERN, dependencyConfig.packageImportPath
54-
);
15+
const makeSettingsPatch = require(`./patches/makeSettingsPatch`);
16+
const makeBuildPatch = require(`./patches/makeBuildPatch`);
17+
const makeMainActivityPatch = require(`./${prefix}/makeMainActivityPatch`);
5518

56-
return replace(
57-
patched, MAIN_ACTIVITY_PACKAGE_PATTERN, getMainActivityPatch()
58-
);
59-
};
19+
const applySettingsPatch = makeSettingsPatch.apply(null, arguments);
20+
const applyBuildPath = makeBuildPatch(name);
21+
const applyMainActivityPatch = makeMainActivityPatch(dependencyConfig);
6022

61-
const applySettingsGradlePatch = compose(
23+
const performSettingsGradlePatch = compose(
6224
writeFile(projectConfig.settingsGradlePath),
63-
patchProjectSettings,
25+
applySettingsPatch,
6426
readFile(projectConfig.settingsGradlePath)
6527
);
6628

67-
const applyBuildGradlePatch = compose(
29+
const performBuildGradlePatch = compose(
6830
writeFile(projectConfig.buildGradlePath),
69-
patchProjectBuild,
31+
applyBuildPath,
7032
readFile(projectConfig.buildGradlePath)
7133
);
7234

73-
const applyMainActivityPatch = compose(
35+
const performMainActivityPatch = compose(
7436
writeFile(projectConfig.mainActivityPath),
75-
makeMainActivityPatcher,
37+
applyMainActivityPatch,
7638
readFile(projectConfig.mainActivityPath)
7739
);
7840

7941
/**
8042
* Check if module has been installed already
8143
*/
8244
const isInstalled = compose(
83-
(content) => ~content.indexOf(getMainActivityPatch()),
45+
(content) => ~content.indexOf(dependencyConfig.packageInstance),
8446
readFile(projectConfig.mainActivityPath)
8547
);
8648

8749
if (!isInstalled(name)) {
8850
compose(
89-
applySettingsGradlePatch,
90-
applyBuildGradlePatch,
91-
applyMainActivityPatch
51+
performSettingsGradlePatch,
52+
performBuildGradlePatch,
53+
performMainActivityPatch
9254
)();
9355
}
9456
};
57+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const chai = require('chai');
4+
const expect = chai.expect;
5+
const makeMainActivityPatch = require(
6+
'../../../../src/android/patches/0.17/makeMainActivityPatch'
7+
);
8+
9+
const config = {
10+
packageInstance: 'new VectorIconsPackage()',
11+
packageImportPath: 'import com.oblador.vectoricons.VectorIconsPackage;',
12+
};
13+
const mainActivityPatch = fs.readFileSync(
14+
path.join(process.cwd(), 'test/fixtures/android/0.17/MainActivity.java'),
15+
'utf-8'
16+
);
17+
const patchedMainActivity = fs.readFileSync(
18+
path.join(process.cwd(), 'test/fixtures/android/0.17/patchedMainActivity.java'),
19+
'utf-8'
20+
);
21+
22+
describe('makeMainActivityPatch@0.17', () => {
23+
it('should build a patch function', () => {
24+
expect(makeMainActivityPatch(config)).to.be.a('function');
25+
});
26+
27+
it('should make a correct patch', () => {
28+
const patch = makeMainActivityPatch(config);
29+
expect(patch(mainActivityPatch)).to.be.equal(patchedMainActivity);
30+
});
31+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const chai = require('chai');
4+
const expect = chai.expect;
5+
const makeMainActivityPatch = require(
6+
'../../../../src/android/patches/0.18/makeMainActivityPatch'
7+
);
8+
9+
const config = {
10+
packageInstance: 'new VectorIconsPackage()',
11+
packageImportPath: 'import com.oblador.vectoricons.VectorIconsPackage;',
12+
};
13+
const mainActivityPatch = fs.readFileSync(
14+
path.join(process.cwd(), 'test/fixtures/android/0.18/MainActivity.java'),
15+
'utf-8'
16+
);
17+
const patchedMainActivity = fs.readFileSync(
18+
path.join(process.cwd(), 'test/fixtures/android/0.18/patchedMainActivity.java'),
19+
'utf-8'
20+
);
21+
22+
describe('makeMainActivityPatch@0.18', () => {
23+
it('should build a patch function', () => {
24+
expect(makeMainActivityPatch(config)).to.be.a('function');
25+
});
26+
27+
it('should make a correct patch', () => {
28+
const patch = makeMainActivityPatch(config);
29+
expect(patch(mainActivityPatch)).to.be.equal(patchedMainActivity);
30+
});
31+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const chai = require('chai');
4+
const expect = chai.expect;
5+
const makeBuildPatch = require('../../../src/android/patches/makeBuildPatch');
6+
7+
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+
);
16+
17+
describe('makeBuildPatch', () => {
18+
it('should build a patch function', () => {
19+
expect(makeBuildPatch(name)).to.be.a('function');
20+
});
21+
22+
it('should make a correct patch', () => {
23+
const patch = makeBuildPatch('test');
24+
expect(patch(buildGradle)).to.be.equal(patchedBuildGradle);
25+
});
26+
});

0 commit comments

Comments
 (0)