Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 22be520

Browse files
committed
add postlink hooks
1 parent c9dff4d commit 22be520

File tree

4 files changed

+136
-9
lines changed

4 files changed

+136
-9
lines changed

package.json

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"url": "https://github.com/Microsoft/react-native-code-push"
1818
},
1919
"dependencies": {
20-
"code-push": "1.8.0-beta"
20+
"code-push": "1.8.0-beta",
21+
"inquirer": "^1.1.2",
22+
"plist": "^1.2.0"
2123
},
2224
"devDependencies": {
2325
"archiver": "latest",
@@ -33,16 +35,23 @@
3335
"run-sequence": "latest"
3436
},
3537
"rnpm": {
36-
"android": {
37-
"packageInstance": "new CodePush(${androidDeploymentKey}, this, BuildConfig.DEBUG)"
38-
},
39-
"ios": {
40-
"sharedLibraries": ["libz"]
41-
},
42-
"params": [{
38+
"android": {
39+
"packageInstance": "new CodePush(${androidDeploymentKey}, this, BuildConfig.DEBUG)"
40+
},
41+
"ios": {
42+
"sharedLibraries": [
43+
"libz"
44+
]
45+
},
46+
"params": [
47+
{
4348
"type": "input",
4449
"name": "androidDeploymentKey",
4550
"message": "What is your CodePush deployment key for Android (hit <ENTER> to ignore)"
46-
}]
51+
}
52+
],
53+
"commands": {
54+
"postlink": "node node_modules/react-native-code-push/scripts/postlink/run"
55+
}
4756
}
4857
}

scripts/postlink/android/postlink.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var fs = require("fs");
2+
var glob = require("glob");
3+
var path = require("path");
4+
5+
var ignoreNodeModules = { ignore: "node_modules/**" };
6+
var mainApplicationPath = glob.sync("**/MainApplication.java", ignoreNodeModules)[0];
7+
var mainActivityPath = glob.sync("**/MainActivity.java", ignoreNodeModules)[0];
8+
var buildGradlePath = path.join("android", "app", "build.gradle");
9+
10+
// 1. Add the getJSBundleFile override
11+
var getJSBundleFileOverride = `
12+
@Override
13+
protected String getJSBundleFile() {
14+
return CodePush.getJSBundleFile();
15+
}
16+
`;
17+
18+
function isAlreadyOverridden(codeContents) {
19+
return /@Override\s*\n\s*protected String getJSBundleFile\(\)\s*\{[\s\S]*?\}/.test(codeContents);
20+
}
21+
22+
if (mainApplicationPath) {
23+
var mainApplicationContents = fs.readFileSync(mainApplicationPath, "utf8");
24+
if (isAlreadyOverridden(mainApplicationContents)) {
25+
console.log(`"getJSBundleFile" is already overridden`);
26+
} else {
27+
var reactNativeHostInstantiation = "new ReactNativeHost(this) {";
28+
mainApplicationContents = mainApplicationContents.replace(reactNativeHostInstantiation,
29+
`${reactNativeHostInstantiation}\n${getJSBundleFileOverride}`);
30+
fs.writeFileSync(mainApplicationPath, mainApplicationContents);
31+
}
32+
} else {
33+
var mainActivityContents = fs.readFileSync(mainActivityPath, "utf8");
34+
if (isAlreadyOverridden(mainActivityContents)) {
35+
console.log(`"getJSBundleFile" is already overridden`);
36+
} else {
37+
var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {";
38+
mainActivityContents = mainActivityContents.replace(mainActivityClassDeclaration,
39+
`${mainActivityClassDeclaration}\n${getJSBundleFileOverride}`);
40+
fs.writeFileSync(mainActivityPath, mainActivityContents);
41+
}
42+
}
43+
44+
// 2. Add the codepush.gradle build task definitions
45+
var buildGradleContents = fs.readFileSync(buildGradlePath, "utf8");
46+
var reactGradleLink = buildGradleContents.match(/\napply from: ".*?react\.gradle"/)[0];
47+
var codePushGradleLink = `apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"`;
48+
if (~buildGradleContents.indexOf(codePushGradleLink)) {
49+
console.log(`"codepush.gradle" is already linked in the build definition`);
50+
} else {
51+
buildGradleContents = buildGradleContents.replace(reactGradleLink,
52+
`${reactGradleLink}\n${codePushGradleLink}`);
53+
fs.writeFileSync(buildGradlePath, buildGradleContents);
54+
}

scripts/postlink/ios/postlink.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var fs = require("fs");
2+
var glob = require("glob");
3+
var inquirer = require('inquirer');
4+
var path = require("path");
5+
var plist = require("plist");
6+
7+
var ignoreNodeModules = { ignore: "node_modules/**" };
8+
var appDelegatePath = glob.sync("**/AppDelegate.m", ignoreNodeModules)[0];
9+
// Glob only allows foward slashes in patterns: https://www.npmjs.com/package/glob#windows
10+
var plistPath = glob.sync(path.join(path.dirname(appDelegatePath), "*Info.plist").replace(/\\/g, "/"), ignoreNodeModules)[0];
11+
12+
var appDelegateContents = fs.readFileSync(appDelegatePath, "utf8");
13+
var plistContents = fs.readFileSync(plistPath, "utf8");
14+
15+
// 1. Add the header import statement
16+
var codePushHeaderImportStatement = `#import "CodePush.h"`;
17+
if (~appDelegateContents.indexOf(codePushHeaderImportStatement)) {
18+
console.log(`"CodePush.h" header already imported.`);
19+
} else {
20+
var appDelegateHeaderImportStatement = `#import "AppDelegate.h"`;
21+
appDelegateContents = appDelegateContents.replace(appDelegateHeaderImportStatement,
22+
`${appDelegateHeaderImportStatement}\n${codePushHeaderImportStatement}`);
23+
}
24+
25+
// 2. Modify jsCodeLocation value assignment
26+
var oldJsCodeLocationAssignmentStatement = appDelegateContents.match(/(jsCodeLocation = .*)\n/)[1];
27+
var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];";
28+
if (~appDelegateContents.indexOf(newJsCodeLocationAssignmentStatement)) {
29+
console.log(`"jsCodeLocation" already pointing to "[CodePush bundleURL]".`);
30+
} else {
31+
var jsCodeLocationPatch = `
32+
#ifdef DEBUG
33+
${oldJsCodeLocationAssignmentStatement}
34+
#else
35+
${newJsCodeLocationAssignmentStatement}
36+
#endif`;
37+
appDelegateContents = appDelegateContents.replace(oldJsCodeLocationAssignmentStatement,
38+
jsCodeLocationPatch);
39+
}
40+
41+
// 3. Add CodePushDeploymentKey to plist file
42+
var parsedInfoPlist = plist.parse(plistContents);
43+
if (parsedInfoPlist.CodePushDeploymentKey) {
44+
console.log(`"CodePushDeploymentKey" already specified in the plist file.`);
45+
writePatches();
46+
} else {
47+
inquirer.prompt({
48+
"type": "input",
49+
"name": "iosDeploymentKey",
50+
"message": "What is your CodePush deployment key for iOS (hit <ENTER> to ignore)"
51+
}).then(function(answer) {
52+
parsedInfoPlist.CodePushDeploymentKey = answer.iosDeploymentKey || "deployment-key-here";
53+
plistContents = plist.build(parsedInfoPlist);
54+
55+
writePatches();
56+
});
57+
}
58+
59+
function writePatches() {
60+
fs.writeFileSync(appDelegatePath, appDelegateContents);
61+
fs.writeFileSync(plistPath, plistContents);
62+
}

scripts/postlink/run.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("./ios/postlink");
2+
require("./android/postlink");

0 commit comments

Comments
 (0)