@@ -69,7 +69,7 @@ public String getStatusFilePath() {
6969
7070 public WritableMap getCurrentPackageInfo () {
7171 String statusFilePath = getStatusFilePath ();
72- if (!CodePushUtils .fileAtPathExists (statusFilePath )) {
72+ if (!FileUtils .fileAtPathExists (statusFilePath )) {
7373 return new WritableNativeMap ();
7474 }
7575
@@ -166,6 +166,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
166166 File downloadFile = null ;
167167 boolean isZip = false ;
168168
169+ // Download the file while checking if it is a zip and notifying client of progress.
169170 try {
170171 downloadUrl = new URL (downloadUrlString );
171172 connection = (HttpURLConnection ) (downloadUrl .openConnection ());
@@ -216,27 +217,34 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
216217 }
217218
218219 if (isZip ) {
220+ // Unzip the downloaded file and then delete the zip
219221 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
222226 String diffManifestFilePath = CodePushUtils .appendPathComponent (unzippedFolderPath ,
223227 DIFF_MANIFEST_FILE_NAME );
224228 File diffManifestFile = new File (unzippedFolderPath , DIFF_MANIFEST_FILE_NAME );
225229 if (diffManifestFile .exists ()) {
226230 String currentPackageFolderPath = getCurrentPackageFolderPath ();
227- CodePushUtils . mergeEntriesInFolder (currentPackageFolderPath , newPackageFolderPath );
231+ FileUtils . copyDirectoryContents (currentPackageFolderPath , newPackageFolderPath );
228232 WritableMap diffManifest = CodePushUtils .getWritableMapFromFile (diffManifestFilePath );
229233 ReadableArray deletedFiles = diffManifest .getArray ("deletedFiles" );
230234 for (int i = 0 ; i < deletedFiles .size (); i ++) {
231235 String fileNameToDelete = deletedFiles .getString (i );
232236 File fileToDelete = new File (newPackageFolderPath , fileNameToDelete );
233- CodePushUtils .deleteFileSilently (fileToDelete );
237+ FileUtils .deleteFileSilently (fileToDelete );
234238 }
235239 }
236240
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 );
240248
241249 if (relativeBundlePath == null ) {
242250 throw new CodePushInvalidPackageException ();
@@ -252,46 +260,22 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
252260 updatePackage = CodePushUtils .convertJsonObjectToWriteable (updatePackageJSON );
253261 }
254262 } else {
255- // File is not a zip.
263+ // File is a jsBundle, move it to a folder with the packageHash as its name
256264 File updateBundleFile = new File (newPackageFolderPath , UPDATE_BUNDLE_FILE_NAME );
257265 downloadFile .renameTo (updateBundleFile );
258266 }
259267
268+ // Save metadata to the folder.
260269 String bundlePath = CodePushUtils .appendPathComponent (newPackageFolderPath , PACKAGE_FILE_NAME );
261270 CodePushUtils .writeReadableMapToFile (updatePackage , bundlePath );
262271 }
263272
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-
289273 public void installPackage (ReadableMap updatePackage ) throws IOException {
290274 String packageHash = CodePushUtils .tryGetString (updatePackage , PACKAGE_HASH_KEY );
291275 WritableMap info = getCurrentPackageInfo ();
292276 String previousPackageHash = getPreviousPackageHash ();
293277 if (previousPackageHash != null && !previousPackageHash .equals (packageHash )) {
294- CodePushUtils .deleteDirectoryAtPath (getPackageFolderPath (previousPackageHash ));
278+ FileUtils .deleteDirectoryAtPath (getPackageFolderPath (previousPackageHash ));
295279 }
296280
297281 info .putString (PREVIOUS_PACKAGE_KEY , CodePushUtils .tryGetString (info , CURRENT_PACKAGE_KEY ));
@@ -302,7 +286,7 @@ public void installPackage(ReadableMap updatePackage) throws IOException {
302286 public void rollbackPackage () {
303287 WritableMap info = getCurrentPackageInfo ();
304288 String currentPackageFolderPath = getCurrentPackageFolderPath ();
305- CodePushUtils .deleteDirectoryAtPath (currentPackageFolderPath );
289+ FileUtils .deleteDirectoryAtPath (currentPackageFolderPath );
306290 info .putString (CURRENT_PACKAGE_KEY , CodePushUtils .tryGetString (info , PREVIOUS_PACKAGE_KEY ));
307291 info .putNull (PREVIOUS_PACKAGE_KEY );
308292 updateCurrentPackageInfo (info );
@@ -344,6 +328,6 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) throws IOExc
344328 public void clearUpdates () {
345329 File statusFile = new File (getStatusFilePath ());
346330 statusFile .delete ();
347- CodePushUtils .deleteDirectoryAtPath (getCodePushPath ());
331+ FileUtils .deleteDirectoryAtPath (getCodePushPath ());
348332 }
349333}
0 commit comments