@@ -69,7 +69,7 @@ public String getStatusFilePath() {
69
69
70
70
public WritableMap getCurrentPackageInfo () {
71
71
String statusFilePath = getStatusFilePath ();
72
- if (!CodePushUtils .fileAtPathExists (statusFilePath )) {
72
+ if (!FileUtils .fileAtPathExists (statusFilePath )) {
73
73
return new WritableNativeMap ();
74
74
}
75
75
@@ -166,6 +166,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
166
166
File downloadFile = null ;
167
167
boolean isZip = false ;
168
168
169
+ // Download the file while checking if it is a zip and notifying client of progress.
169
170
try {
170
171
downloadUrl = new URL (downloadUrlString );
171
172
connection = (HttpURLConnection ) (downloadUrl .openConnection ());
@@ -216,27 +217,34 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
216
217
}
217
218
218
219
if (isZip ) {
220
+ // Unzip the downloaded file and then delete the zip
219
221
String unzippedFolderPath = getUnzippedFolderPath ();
220
- CodePushUtils .unzipFile (downloadFile , unzippedFolderPath );
221
- CodePushUtils .deleteFileSilently (downloadFile );
222
+ FileUtils .unzipFile (downloadFile , unzippedFolderPath );
223
+ FileUtils .deleteFileSilently (downloadFile );
224
+
225
+ // Merge contents with current update based on the manifest
222
226
String diffManifestFilePath = CodePushUtils .appendPathComponent (unzippedFolderPath ,
223
227
DIFF_MANIFEST_FILE_NAME );
224
228
File diffManifestFile = new File (unzippedFolderPath , DIFF_MANIFEST_FILE_NAME );
225
229
if (diffManifestFile .exists ()) {
226
230
String currentPackageFolderPath = getCurrentPackageFolderPath ();
227
- CodePushUtils . mergeEntriesInFolder (currentPackageFolderPath , newPackageFolderPath );
231
+ FileUtils . copyDirectoryContents (currentPackageFolderPath , newPackageFolderPath );
228
232
WritableMap diffManifest = CodePushUtils .getWritableMapFromFile (diffManifestFilePath );
229
233
ReadableArray deletedFiles = diffManifest .getArray ("deletedFiles" );
230
234
for (int i = 0 ; i < deletedFiles .size (); i ++) {
231
235
String fileNameToDelete = deletedFiles .getString (i );
232
236
File fileToDelete = new File (newPackageFolderPath , fileNameToDelete );
233
- CodePushUtils .deleteFileSilently (fileToDelete );
237
+ FileUtils .deleteFileSilently (fileToDelete );
234
238
}
235
239
}
236
240
237
- CodePushUtils .mergeEntriesInFolder (unzippedFolderPath , newPackageFolderPath );
238
- CodePushUtils .deleteFileAtPathSilently (unzippedFolderPath );
239
- String relativeBundlePath = findMainBundleInFolder (newPackageFolderPath );
241
+ // Move merged update contents to a folder with the packageHash as its name
242
+ FileUtils .copyDirectoryContents (unzippedFolderPath , newPackageFolderPath );
243
+ FileUtils .deleteFileAtPathSilently (unzippedFolderPath );
244
+
245
+ // For zip updates, we need to find the relative path to the jsBundle and save it in the
246
+ // metadata so that we can find and run it easily the next time.
247
+ String relativeBundlePath = CodePushUtils .findJSBundleInUpdateContents (newPackageFolderPath );
240
248
241
249
if (relativeBundlePath == null ) {
242
250
throw new CodePushInvalidPackageException ();
@@ -252,46 +260,22 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
252
260
updatePackage = CodePushUtils .convertJsonObjectToWriteable (updatePackageJSON );
253
261
}
254
262
} else {
255
- // File is not a zip.
263
+ // File is a jsBundle, move it to a folder with the packageHash as its name
256
264
File updateBundleFile = new File (newPackageFolderPath , UPDATE_BUNDLE_FILE_NAME );
257
265
downloadFile .renameTo (updateBundleFile );
258
266
}
259
267
268
+ // Save metadata to the folder.
260
269
String bundlePath = CodePushUtils .appendPathComponent (newPackageFolderPath , PACKAGE_FILE_NAME );
261
270
CodePushUtils .writeReadableMapToFile (updatePackage , bundlePath );
262
271
}
263
272
264
- public String findMainBundleInFolder (String folderPath ) {
265
- File folder = new File (folderPath );
266
- File [] folderFiles = folder .listFiles ();
267
- for (File file : folderFiles ) {
268
- String fullFilePath = CodePushUtils .appendPathComponent (folderPath , file .getName ());
269
- if (file .isDirectory ()) {
270
- String mainBundlePathInSubFolder = findMainBundleInFolder (fullFilePath );
271
- if (mainBundlePathInSubFolder != null ) {
272
- return CodePushUtils .appendPathComponent (file .getName (), mainBundlePathInSubFolder );
273
- }
274
- } else {
275
- String fileName = file .getName ();
276
- int dotIndex = fileName .lastIndexOf ("." );
277
- if (dotIndex >= 0 ) {
278
- String fileExtension = fileName .substring (dotIndex + 1 );
279
- if (fileExtension .equals ("bundle" ) || fileExtension .equals ("js" ) || fileExtension .equals ("jsbundle" )) {
280
- return fileName ;
281
- }
282
- }
283
- }
284
- }
285
-
286
- return null ;
287
- }
288
-
289
273
public void installPackage (ReadableMap updatePackage ) throws IOException {
290
274
String packageHash = CodePushUtils .tryGetString (updatePackage , PACKAGE_HASH_KEY );
291
275
WritableMap info = getCurrentPackageInfo ();
292
276
String previousPackageHash = getPreviousPackageHash ();
293
277
if (previousPackageHash != null && !previousPackageHash .equals (packageHash )) {
294
- CodePushUtils .deleteDirectoryAtPath (getPackageFolderPath (previousPackageHash ));
278
+ FileUtils .deleteDirectoryAtPath (getPackageFolderPath (previousPackageHash ));
295
279
}
296
280
297
281
info .putString (PREVIOUS_PACKAGE_KEY , CodePushUtils .tryGetString (info , CURRENT_PACKAGE_KEY ));
@@ -302,7 +286,7 @@ public void installPackage(ReadableMap updatePackage) throws IOException {
302
286
public void rollbackPackage () {
303
287
WritableMap info = getCurrentPackageInfo ();
304
288
String currentPackageFolderPath = getCurrentPackageFolderPath ();
305
- CodePushUtils .deleteDirectoryAtPath (currentPackageFolderPath );
289
+ FileUtils .deleteDirectoryAtPath (currentPackageFolderPath );
306
290
info .putString (CURRENT_PACKAGE_KEY , CodePushUtils .tryGetString (info , PREVIOUS_PACKAGE_KEY ));
307
291
info .putNull (PREVIOUS_PACKAGE_KEY );
308
292
updateCurrentPackageInfo (info );
@@ -344,6 +328,6 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) throws IOExc
344
328
public void clearUpdates () {
345
329
File statusFile = new File (getStatusFilePath ());
346
330
statusFile .delete ();
347
- CodePushUtils .deleteDirectoryAtPath (getCodePushPath ());
331
+ FileUtils .deleteDirectoryAtPath (getCodePushPath ());
348
332
}
349
333
}
0 commit comments