forked from CodePushNext/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpo.js
More file actions
324 lines (297 loc) · 14.1 KB
/
expo.js
File metadata and controls
324 lines (297 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const {
withAppDelegate,
withInfoPlist,
withStringsXml,
withAppBuildGradle,
withMainApplication, // Using the safer, higher-level helper
WarningAggregator,
} = require('@expo/config-plugins');
const fs = require('fs');
const path = require('path');
//================ iOS (Unchanged) ================
function addImportIOS(content) {
const lines = content.split('\n');
const importIndices = lines
.map((line, idx) => (line.trim().startsWith('import ') || line.trim().startsWith('#import ') ? idx : -1))
.filter(idx => idx !== -1);
if (content.includes('import CodePush') || content.includes('#import <CodePush/CodePush.h>')) {
return content;
}
const codePushImport = 'import CodePush';
if (importIndices.length > 0) {
const lastImportIdx = importIndices[importIndices.length - 1];
lines.splice(lastImportIdx + 1, 0, codePushImport);
return lines.join('\n');
} else {
const swiftClassRegex = /class\s+AppDelegate\s*:\s*RCTAppDelegate\s*\{/m;
if (swiftClassRegex.test(content)) {
return content.replace(swiftClassRegex, `$&\n${codePushImport}`);
}
return codePushImport + '\n' + content;
}
}
function ensureBundleURLMethodIOS(content) {
const methodRegexOld = /-\s*\(NSURL\s*\*\s*\)\s*bundleURL\s*\{[^}]*\}/s;
const methodRegexSwift = /override func bundleURL\(\) -> URL\? \{[^}]*\}/s;
const newMethodBodyObjC = `- (NSURL *)bundleURL
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@".expo/.virtual-metro-entry"];
#else
return [CodePush bundleURL];
#endif
}`;
const newMethodBodySwift = `override func bundleURL() -> URL? {
#if DEBUG
return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: ".expo/.virtual-metro-entry")
#else
return CodePush.bundleURL()
#endif
}`;
if (methodRegexSwift.test(content)) {
return content.replace(methodRegexSwift, newMethodBodySwift);
} else if (methodRegexOld.test(content)) {
return content.replace(methodRegexOld, newMethodBodyObjC);
} else {
const appDelegateEndRegexObjC = /\@end/m;
const appDelegateEndRegexSwift = /\}\s*$/m;
if (appDelegateEndRegexObjC.test(content)) {
return content.replace(appDelegateEndRegexObjC, `\n${newMethodBodyObjC}\n\n@end`);
} else if (appDelegateEndRegexSwift.test(content)) {
const lastBraceIndex = content.lastIndexOf('}');
if (lastBraceIndex !== -1) {
return content.substring(0, lastBraceIndex) + `\n ${newMethodBodySwift}\n` + content.substring(lastBraceIndex);
}
}
}
WarningAggregator.addWarningIOS('codepush-plugin', 'Could not find a suitable place to insert bundleURL() method in AppDelegate.');
return content;
}
const withCodePushAppDelegate = (config) => {
return withAppDelegate(config, (config) => {
let content = config.modResults.contents;
content = addImportIOS(content);
content = ensureBundleURLMethodIOS(content);
config.modResults.contents = content;
return config;
});
};
const withCodePushInfoPlist = (config, options = {}) => {
return withInfoPlist(config, (config) => {
if (options.ios && options.ios.CodePushDeploymentKey) {
config.modResults.CodePushDeploymentKey = options.ios.CodePushDeploymentKey;
}
if (options.ios && options.ios.CodePushServerURL) {
config.modResults.CodePushServerURL = options.ios.CodePushServerURL;
}
return config;
});
};
//================ Android (Refactored to use withMainApplication) ================
const withAndroidMainApplication = (config) => {
return withMainApplication(config, (modConfig) => {
// Check for Kotlin
if (modConfig.modResults.language !== 'kt') {
WarningAggregator.addWarningAndroid(
'codepush-plugin',
`The CodePush plugin is skipping modifications to 'MainApplication' because it is not a Kotlin file. Your project must be configured to use Kotlin for Android.`
);
return modConfig;
}
let content = modConfig.modResults.contents;
const packageName = config.android.package;
// --- 1. Add Imports ---
const requiredImports = [
'import com.microsoft.codepush.react.CodePush',
`import ${packageName}.R`,
'import android.util.Log',
];
// Simple import adder
const lines = content.split('\n');
let lastImportIndex = -1;
const existingImports = new Set();
lines.forEach((line, index) => {
if (line.trim().startsWith('import ')) {
lastImportIndex = index;
existingImports.add(line.trim());
}
});
const importsToAdd = requiredImports.filter(imp => !existingImports.has(imp));
if (importsToAdd.length > 0) {
lines.splice(lastImportIndex + 1, 0, ...importsToAdd);
}
content = lines.join('\n');
// --- 2. Modify onCreate method ---
const onCreateRegex = /(override fun onCreate\(\)\s*\{)([\s\S]*?)(\n\s*\})/m;
const onCreateInjection = `
// CodePush: Initialize instance on app startup.
try {
Log.d("CodePushDebug", "Attempting to pre-initialize CodePush in onCreate...");
val deploymentKey = getString(R.string.CodePushDeploymentKey);
val isDebugMode = BuildConfig.DEBUG;
CodePush.getInstance(deploymentKey, this, isDebugMode);
Log.d("CodePushDebug", "CodePush.getInstance() called in onCreate()");
} catch (e: Exception) {
Log.e("CodePushDebug", "Error pre-initializing CodePush in onCreate: " + e.message, e);
}
`;
if (onCreateRegex.test(content) && !content.includes('CodePush.getInstance(deploymentKey, this, isDebugMode)')) {
content = content.replace(onCreateRegex, (match, onCreateStart, onCreateContent, onCreateEnd) => {
const soLoaderInitLineRegex = /^\s*SoLoader\.init\(this,.*\)/m;
const superOnCreateLineRegex = /^\s*super\.onCreate\(\)/m;
if (soLoaderInitLineRegex.test(onCreateContent)) {
// Found SoLoader.init(), insert after it
const modifiedContent = onCreateContent.replace(soLoaderInitLineRegex, `$&${onCreateInjection}`);
return `${onCreateStart}${modifiedContent}${onCreateEnd}`;
} else if (superOnCreateLineRegex.test(onCreateContent)) {
// SoLoader.init() not found, insert after super.onCreate()
const modifiedContent = onCreateContent.replace(superOnCreateLineRegex, `$&${onCreateInjection}`);
WarningAggregator.addWarningAndroid('codepush-plugin', 'SoLoader.init() not found in onCreate(). Placing CodePush initialization after super.onCreate().');
return `${onCreateStart}${modifiedContent}${onCreateEnd}`;
} else {
// Neither found, inject at the start of the method
WarningAggregator.addWarningAndroid('codepush-plugin', 'Could not find super.onCreate() or SoLoader.init() in onCreate(). CodePush initialization may be misplaced.');
return `${onCreateStart}${onCreateInjection}${onCreateContent}${onCreateEnd}`;
}
});
}
// --- 3. Modify getPackages method ---
const getPackagesRegex = /override fun getPackages\(\): List<ReactPackage>\s*\{/m;
const originalPackagesLineRegex = /val\s+packages\s*=\s*PackageList\(this\)\.packages/m;
const mutablePackagesLine = "val packages: MutableList<ReactPackage> = PackageList(this).packages.toMutableList()";
const packagesInjection = `
try {
val codePushInstance = CodePush.getInstance(getString(R.string.CodePushDeploymentKey), this@MainApplication, BuildConfig.DEBUG);
if (!packages.contains(codePushInstance)) {
packages.add(codePushInstance);
Log.d("CodePushDebug", "CodePush instance added to packages.");
} else {
Log.d("CodePushDebug", "CodePush instance was already present in packages list; not adding again.");
}
} catch (e: Exception) {
Log.e("CodePushDebug", "Error adding CodePush to packages list: " + e.message, e);
}
`;
if (getPackagesRegex.test(content) && !content.includes('CodePush.getInstance(getString(R.string.CodePushDeploymentKey)')) {
if (originalPackagesLineRegex.test(content)) {
content = content.replace(originalPackagesLineRegex, mutablePackagesLine);
content = content.replace(/(\n\s*return\s+packages)/m, `\n${packagesInjection}$1`);
} else {
WarningAggregator.addWarningAndroid('codepush-plugin', 'Could not find standard packages list initialization in getPackages(). CodePush package not registered.');
}
}
// --- 4. Add getJSBundleFile method ---
const getJSBundleFileMethodString = `
override fun getJSBundleFile(): String {
return CodePush.getJSBundleFile()
}`;
const hermesEnabledAnchor = /(override\s+val\s+isHermesEnabled:\s*Boolean\s*=\s*BuildConfig\.IS_HERMES_ENABLED)\s*\n/m;
if (!content.includes("override fun getJSBundleFile(): String")) {
if (hermesEnabledAnchor.test(content)) {
content = content.replace(hermesEnabledAnchor, `$1\n${getJSBundleFileMethodString}\n`);
} else {
WarningAggregator.addWarningAndroid('codepush-plugin', 'Could not find `isHermesEnabled` property to anchor `getJSBundleFile()` insertion. Please review `MainApplication.kt`.');
}
}
modConfig.modResults.contents = content;
return modConfig;
});
};
const withAndroidGradle = (config) => {
return withAppBuildGradle(config, (modConfig) => {
if (modConfig.modResults.language === 'groovy') {
let content = modConfig.modResults.contents;
// This part adds the codepush.gradle apply line and is correct.
const codePushApplyLine = 'apply from: "../../node_modules/@code-push-next/react-native-code-push/android/codepush.gradle"';
if (!content.includes(codePushApplyLine)) {
content += `\n${codePushApplyLine}\n`;
}
const debugConfigFieldLine = 'buildConfigField "boolean", "DEBUG", "true"';
const releaseConfigFieldLine = 'buildConfigField "boolean", "DEBUG", "false"';
const indent = ' ';
/**
* A robust function to add a configuration field to a specific build type.
* It operates on the entire gradle content to ensure context is never lost.
* @param {string} gradleContent The entire content of the build.gradle file.
* @param {string} buildType The name of the build type (e.g., "debug").
* @param {string} field The configuration line to add.
* @returns {string} The modified gradle content.
*/
const addFieldToBuildType = (gradleContent, buildType, field) => {
// Regex to find a specific buildType block that is inside the buildTypes block
const buildTypeRegex = new RegExp(`(buildTypes\\s*\\{[\\s\\S]*?${buildType}\\s*\\{)([\\s\\S]*?)(\\n\\s*\\})`, 'm');
if (buildTypeRegex.test(gradleContent)) {
// The build type block (e.g., debug {}) already exists.
return gradleContent.replace(buildTypeRegex, (match, startBlock, innerContent, endBlock) => {
if (innerContent.includes(field.trim())) {
return match; // Field already exists, no changes needed.
}
// Add the field to the existing block.
const newInnerContent = innerContent.trim() ? `${innerContent.trim()}\n${indent}${field.trim()}` : `\n${indent}${field.trim()}`;
return `${startBlock}${newInnerContent}\n ${endBlock}`;
});
} else {
// The build type block does not exist, so we need to add it.
const buildTypesRegex = /(buildTypes\s*\{)([\s\S]*?)(\n\s*\})/m;
if (buildTypesRegex.test(gradleContent)) {
// The buildTypes block exists, so add the new build type to it.
return gradleContent.replace(buildTypesRegex, (match, startBlock, innerContent, endBlock) => {
const newBlock = `\n ${buildType} {\n${indent}${field.trim()}\n }`;
return `${startBlock}${innerContent.trim()}${newBlock}${endBlock}`;
});
} else {
// The buildTypes block itself doesn't exist, a rare edge case.
// Expo projects should always have a buildTypes block.
WarningAggregator.addWarningAndroid('codepush-plugin', `Could not find buildTypes { ... } block in app/build.gradle to add the '${buildType}' configuration.`);
return gradleContent;
}
}
};
// Apply the modifications for debug and release builds sequentially.
// Each function call operates on the full, updated content, preventing nesting errors.
content = addFieldToBuildType(content, 'debug', debugConfigFieldLine);
content = addFieldToBuildType(content, 'release', releaseConfigFieldLine);
modConfig.modResults.contents = content;
} else {
WarningAggregator.addWarningAndroid('codepush-plugin', 'Cannot apply build.gradle modifications because it is not a groovy file.');
}
return modConfig;
});
};
const withAndroidStrings = (config, options) => {
return withStringsXml(config, (config) => {
if (!config.modResults.resources) config.modResults.resources = {};
if (!config.modResults.resources.string) config.modResults.resources.string = [];
const strings = config.modResults.resources.string;
const setString = (name, value) => {
const existing = strings.find(s => s.$.name === name);
if (existing) {
existing._ = value;
} else {
strings.push({$: { name, translatable: 'false' }, _: value });
}
};
if (options.android?.CodePushDeploymentKey) {
setString('CodePushDeploymentKey', options.android.CodePushDeploymentKey);
}
if (options.android?.CodePushServerURL) {
setString('CodePushServerUrl', options.android.CodePushServerURL);
}
return config;
});
};
// --- CORRECTED EXPORT BLOCK ---
module.exports = (config, options = {}) => {
if (!options) options = {};
if (options.ios) {
config = withCodePushAppDelegate(config, options);
config = withCodePushInfoPlist(config, options);
}
if (options.android) {
config = withAndroidStrings(config, options);
// Correctly call the wrapper functions we defined
config = withAndroidMainApplication(config);
config = withAndroidGradle(config);
}
return config;
};