mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-05-22 15:55:57 +08:00
add postlink hooks
This commit is contained in:
54
scripts/postlink/android/postlink.js
Normal file
54
scripts/postlink/android/postlink.js
Normal file
@@ -0,0 +1,54 @@
|
||||
var fs = require("fs");
|
||||
var glob = require("glob");
|
||||
var path = require("path");
|
||||
|
||||
var ignoreNodeModules = { ignore: "node_modules/**" };
|
||||
var mainApplicationPath = glob.sync("**/MainApplication.java", ignoreNodeModules)[0];
|
||||
var mainActivityPath = glob.sync("**/MainActivity.java", ignoreNodeModules)[0];
|
||||
var buildGradlePath = path.join("android", "app", "build.gradle");
|
||||
|
||||
// 1. Add the getJSBundleFile override
|
||||
var getJSBundleFileOverride = `
|
||||
@Override
|
||||
protected String getJSBundleFile() {
|
||||
return CodePush.getJSBundleFile();
|
||||
}
|
||||
`;
|
||||
|
||||
function isAlreadyOverridden(codeContents) {
|
||||
return /@Override\s*\n\s*protected String getJSBundleFile\(\)\s*\{[\s\S]*?\}/.test(codeContents);
|
||||
}
|
||||
|
||||
if (mainApplicationPath) {
|
||||
var mainApplicationContents = fs.readFileSync(mainApplicationPath, "utf8");
|
||||
if (isAlreadyOverridden(mainApplicationContents)) {
|
||||
console.log(`"getJSBundleFile" is already overridden`);
|
||||
} else {
|
||||
var reactNativeHostInstantiation = "new ReactNativeHost(this) {";
|
||||
mainApplicationContents = mainApplicationContents.replace(reactNativeHostInstantiation,
|
||||
`${reactNativeHostInstantiation}\n${getJSBundleFileOverride}`);
|
||||
fs.writeFileSync(mainApplicationPath, mainApplicationContents);
|
||||
}
|
||||
} else {
|
||||
var mainActivityContents = fs.readFileSync(mainActivityPath, "utf8");
|
||||
if (isAlreadyOverridden(mainActivityContents)) {
|
||||
console.log(`"getJSBundleFile" is already overridden`);
|
||||
} else {
|
||||
var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {";
|
||||
mainActivityContents = mainActivityContents.replace(mainActivityClassDeclaration,
|
||||
`${mainActivityClassDeclaration}\n${getJSBundleFileOverride}`);
|
||||
fs.writeFileSync(mainActivityPath, mainActivityContents);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Add the codepush.gradle build task definitions
|
||||
var buildGradleContents = fs.readFileSync(buildGradlePath, "utf8");
|
||||
var reactGradleLink = buildGradleContents.match(/\napply from: ".*?react\.gradle"/)[0];
|
||||
var codePushGradleLink = `apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"`;
|
||||
if (~buildGradleContents.indexOf(codePushGradleLink)) {
|
||||
console.log(`"codepush.gradle" is already linked in the build definition`);
|
||||
} else {
|
||||
buildGradleContents = buildGradleContents.replace(reactGradleLink,
|
||||
`${reactGradleLink}\n${codePushGradleLink}`);
|
||||
fs.writeFileSync(buildGradlePath, buildGradleContents);
|
||||
}
|
||||
62
scripts/postlink/ios/postlink.js
Normal file
62
scripts/postlink/ios/postlink.js
Normal file
@@ -0,0 +1,62 @@
|
||||
var fs = require("fs");
|
||||
var glob = require("glob");
|
||||
var inquirer = require('inquirer');
|
||||
var path = require("path");
|
||||
var plist = require("plist");
|
||||
|
||||
var ignoreNodeModules = { ignore: "node_modules/**" };
|
||||
var appDelegatePath = glob.sync("**/AppDelegate.m", ignoreNodeModules)[0];
|
||||
// Glob only allows foward slashes in patterns: https://www.npmjs.com/package/glob#windows
|
||||
var plistPath = glob.sync(path.join(path.dirname(appDelegatePath), "*Info.plist").replace(/\\/g, "/"), ignoreNodeModules)[0];
|
||||
|
||||
var appDelegateContents = fs.readFileSync(appDelegatePath, "utf8");
|
||||
var plistContents = fs.readFileSync(plistPath, "utf8");
|
||||
|
||||
// 1. Add the header import statement
|
||||
var codePushHeaderImportStatement = `#import "CodePush.h"`;
|
||||
if (~appDelegateContents.indexOf(codePushHeaderImportStatement)) {
|
||||
console.log(`"CodePush.h" header already imported.`);
|
||||
} else {
|
||||
var appDelegateHeaderImportStatement = `#import "AppDelegate.h"`;
|
||||
appDelegateContents = appDelegateContents.replace(appDelegateHeaderImportStatement,
|
||||
`${appDelegateHeaderImportStatement}\n${codePushHeaderImportStatement}`);
|
||||
}
|
||||
|
||||
// 2. Modify jsCodeLocation value assignment
|
||||
var oldJsCodeLocationAssignmentStatement = appDelegateContents.match(/(jsCodeLocation = .*)\n/)[1];
|
||||
var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];";
|
||||
if (~appDelegateContents.indexOf(newJsCodeLocationAssignmentStatement)) {
|
||||
console.log(`"jsCodeLocation" already pointing to "[CodePush bundleURL]".`);
|
||||
} else {
|
||||
var jsCodeLocationPatch = `
|
||||
#ifdef DEBUG
|
||||
${oldJsCodeLocationAssignmentStatement}
|
||||
#else
|
||||
${newJsCodeLocationAssignmentStatement}
|
||||
#endif`;
|
||||
appDelegateContents = appDelegateContents.replace(oldJsCodeLocationAssignmentStatement,
|
||||
jsCodeLocationPatch);
|
||||
}
|
||||
|
||||
// 3. Add CodePushDeploymentKey to plist file
|
||||
var parsedInfoPlist = plist.parse(plistContents);
|
||||
if (parsedInfoPlist.CodePushDeploymentKey) {
|
||||
console.log(`"CodePushDeploymentKey" already specified in the plist file.`);
|
||||
writePatches();
|
||||
} else {
|
||||
inquirer.prompt({
|
||||
"type": "input",
|
||||
"name": "iosDeploymentKey",
|
||||
"message": "What is your CodePush deployment key for iOS (hit <ENTER> to ignore)"
|
||||
}).then(function(answer) {
|
||||
parsedInfoPlist.CodePushDeploymentKey = answer.iosDeploymentKey || "deployment-key-here";
|
||||
plistContents = plist.build(parsedInfoPlist);
|
||||
|
||||
writePatches();
|
||||
});
|
||||
}
|
||||
|
||||
function writePatches() {
|
||||
fs.writeFileSync(appDelegatePath, appDelegateContents);
|
||||
fs.writeFileSync(plistPath, plistContents);
|
||||
}
|
||||
2
scripts/postlink/run.js
Normal file
2
scripts/postlink/run.js
Normal file
@@ -0,0 +1,2 @@
|
||||
require("./ios/postlink");
|
||||
require("./android/postlink");
|
||||
Reference in New Issue
Block a user