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

Commit 746b767

Browse files
Nikita MatrosovVladimir Kotikov
authored andcommitted
Improve post-linking process (#639)
1 parent e39ade0 commit 746b767

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

scripts/postlink/android/postlink.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@ var fs = require("fs");
22
var glob = require("glob");
33
var path = require("path");
44

5-
var ignoreNodeModules = { ignore: "node_modules/**" };
6-
var mainApplicationPath = glob.sync("**/MainApplication.java", ignoreNodeModules)[0];
7-
var mainActivityPath = glob.sync("**/MainActivity.java", ignoreNodeModules)[0];
5+
var ignoreFolders = { ignore: ["node_modules/**", "**/build/**"] };
86
var buildGradlePath = path.join("android", "app", "build.gradle");
7+
var manifestPath = glob.sync("**/AndroidManifest.xml", ignoreFolders)[0];
8+
9+
function findMainApplication() {
10+
if (!manifestPath) {
11+
return null;
12+
}
13+
14+
var manifest = fs.readFileSync(manifestPath, "utf8");
15+
16+
// Android manifest must include single 'application' element
17+
var matchResult = manifest.match(/application\s+android:name\s*=\s*"(.*?)"/);
18+
if (matchResult) {
19+
var appName = matchResult[1];
20+
} else {
21+
return null;
22+
}
23+
24+
var nameParts = appName.split('.');
25+
var searchPath = glob.sync("**/" + nameParts[nameParts.length - 1] + ".java", ignoreFolders)[0];
26+
return searchPath;
27+
}
28+
29+
var mainApplicationPath = findMainApplication() || glob.sync("**/MainApplication.java", ignoreFolders)[0];
930

1031
// 1. Add the getJSBundleFile override
1132
var getJSBundleFileOverride = `
@@ -30,20 +51,34 @@ if (mainApplicationPath) {
3051
fs.writeFileSync(mainApplicationPath, mainApplicationContents);
3152
}
3253
} else {
33-
var mainActivityContents = fs.readFileSync(mainActivityPath, "utf8");
34-
if (isAlreadyOverridden(mainActivityContents)) {
35-
console.log(`"getJSBundleFile" is already overridden`);
54+
var mainActivityPath = glob.sync("**/MainActivity.java", ignoreFolders)[0];
55+
if (mainActivityPath) {
56+
var mainActivityContents = fs.readFileSync(mainActivityPath, "utf8");
57+
if (isAlreadyOverridden(mainActivityContents)) {
58+
console.log(`"getJSBundleFile" is already overridden`);
59+
} else {
60+
var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {";
61+
mainActivityContents = mainActivityContents.replace(mainActivityClassDeclaration,
62+
`${mainActivityClassDeclaration}\n${getJSBundleFileOverride}`);
63+
fs.writeFileSync(mainActivityPath, mainActivityContents);
64+
}
3665
} else {
37-
var mainActivityClassDeclaration = "public class MainActivity extends ReactActivity {";
38-
mainActivityContents = mainActivityContents.replace(mainActivityClassDeclaration,
39-
`${mainActivityClassDeclaration}\n${getJSBundleFileOverride}`);
40-
fs.writeFileSync(mainActivityPath, mainActivityContents);
66+
console.error(`Couldn't find Android application entry point. You might need to update it manually. \
67+
Please refer to plugin configuration section for Android at \
68+
https://github.com/microsoft/react-native-code-push#plugin-configuration-android for more details`);
4169
}
4270
}
4371

72+
if (!fs.existsSync(buildGradlePath)) {
73+
console.error(`Couldn't find build.gradle file. You might need to update it manually. \
74+
Please refer to plugin installation section for Android at \
75+
https://github.com/microsoft/react-native-code-push#plugin-installation-android---manual`);
76+
return;
77+
}
78+
4479
// 2. Add the codepush.gradle build task definitions
4580
var buildGradleContents = fs.readFileSync(buildGradlePath, "utf8");
46-
var reactGradleLink = buildGradleContents.match(/\napply from: ".*?react\.gradle"/)[0];
81+
var reactGradleLink = buildGradleContents.match(/\napply from: ["'].*?react\.gradle["']/)[0];
4782
var codePushGradleLink = `apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"`;
4883
if (~buildGradleContents.indexOf(codePushGradleLink)) {
4984
console.log(`"codepush.gradle" is already linked in the build definition`);

scripts/postlink/ios/postlink.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ var plist = require("plist");
66
var package = require('../../../../../package.json');
77

88
var ignoreNodeModules = { ignore: "node_modules/**" };
9-
var appDelegatePaths = glob.sync("**/AppDelegate.m", ignoreNodeModules);
9+
var appDelegatePaths = glob.sync("**/AppDelegate.+(mm|m)", ignoreNodeModules);
1010

1111
// Fix for https://github.com/Microsoft/react-native-code-push/issues/477
1212
// Typical location of AppDelegate.m for newer RN versions: $PROJECT_ROOT/ios/<project_name>/AppDelegate.m
1313
// Let's try to find that path by filtering the whole array for any path containing <project_name>
1414
// If we can't find it there, play dumb and pray it is the first path we find.
1515
var appDelegatePath = findFileByAppName(appDelegatePaths, package ? package.name : null) || appDelegatePaths[0];
1616

17-
// Glob only allows foward slashes in patterns: https://www.npmjs.com/package/glob#windows
18-
var plistPath = glob.sync(path.join(path.dirname(appDelegatePath), "*Info.plist").replace(/\\/g, "/"), ignoreNodeModules)[0];
17+
if (!appDelegatePath) {
18+
console.log(`Couldn't find AppDelegate. You might need to update it manually \
19+
Please refer to plugin configuration section for iOS at \
20+
https://github.com/microsoft/react-native-code-push#plugin-configuration-ios`);
21+
return;
22+
}
1923

2024
var appDelegateContents = fs.readFileSync(appDelegatePath, "utf8");
21-
var plistContents = fs.readFileSync(plistPath, "utf8");
2225

2326
// 1. Add the header import statement
2427
var codePushHeaderImportStatement = `#import "CodePush.h"`;
@@ -46,6 +49,14 @@ if (~appDelegateContents.indexOf(newJsCodeLocationAssignmentStatement)) {
4649
jsCodeLocationPatch);
4750
}
4851

52+
var plistPath = glob.sync(`**/${package.name}/*Info.plist`, ignoreNodeModules)[0];
53+
if (!plistPath) {
54+
console.log("Couldn't find .plist file");
55+
return;
56+
}
57+
58+
var plistContents = fs.readFileSync(plistPath, "utf8");
59+
4960
// 3. Add CodePushDeploymentKey to plist file
5061
var parsedInfoPlist = plist.parse(plistContents);
5162
if (parsedInfoPlist.CodePushDeploymentKey) {

0 commit comments

Comments
 (0)