outsource more methods to CodePushUpdateUtils

This commit is contained in:
Geoffrey Goh
2016-01-29 14:34:42 -08:00
parent 44d6b8da1b
commit 0712451622
5 changed files with 77 additions and 64 deletions

View File

@@ -359,32 +359,34 @@ RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[CodePushPackage downloadPackage:updatePackage
// The download is progressing forward
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
// Notify the script-side about the progress
[self.bridge.eventDispatcher
sendDeviceEventWithName:@"CodePushDownloadProgress"
body:@{
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
}];
}
// The download completed
doneCallback:^{
NSError *err;
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
if (err) {
return reject(err);
dispatch_async(dispatch_get_main_queue(), ^{
[CodePushPackage downloadPackage:updatePackage
// The download is progressing forward
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
// Notify the script-side about the progress
[self.bridge.eventDispatcher
sendDeviceEventWithName:@"CodePushDownloadProgress"
body:@{
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
}];
}
resolve(newPackage);
}
// The download failed
failCallback:^(NSError *err) {
reject(err);
}];
// The download completed
doneCallback:^{
NSError *err;
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
if (err) {
return reject(err);
}
resolve(newPackage);
}
// The download failed
failCallback:^(NSError *err) {
reject(err);
}];
});
}
/*

View File

@@ -225,26 +225,17 @@ public class CodePushPackage {
// Merge contents with current update based on the manifest
String diffManifestFilePath = CodePushUtils.appendPathComponent(unzippedFolderPath,
DIFF_MANIFEST_FILE_NAME);
File diffManifestFile = new File(unzippedFolderPath, DIFF_MANIFEST_FILE_NAME);
if (diffManifestFile.exists()) {
if (FileUtils.fileAtPathExists(diffManifestFilePath)) {
String currentPackageFolderPath = getCurrentPackageFolderPath();
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath);
ReadableArray deletedFiles = diffManifest.getArray("deletedFiles");
for (int i = 0; i < deletedFiles.size(); i++) {
String fileNameToDelete = deletedFiles.getString(i);
File fileToDelete = new File(newPackageFolderPath, fileNameToDelete);
FileUtils.deleteFileSilently(fileToDelete);
}
CodePushUpdateUtils.copyNecessaryFilesFromCurrentPackage(diffManifestFilePath, currentPackageFolderPath, newPackageFolderPath);
}
// Move merged update contents to a folder with the packageHash as its name
FileUtils.copyDirectoryContents(unzippedFolderPath, newPackageFolderPath);
FileUtils.deleteFileAtPathSilently(unzippedFolderPath);
// For zip updates, we need to find the relative path to the jsBundle and save it in the
// metadata so that we can find and run it easily the next time.
String relativeBundlePath = CodePushUtils.findJSBundleInUpdateContents(newPackageFolderPath);
String relativeBundlePath = CodePushUpdateUtils.findJSBundleInUpdateContents(newPackageFolderPath);
if (relativeBundlePath == null) {
throw new CodePushInvalidUpdateException();
@@ -257,6 +248,7 @@ public class CodePushPackage {
RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath +
" in update package.", e);
}
updatePackage = CodePushUtils.convertJsonObjectToWriteable(updatePackageJSON);
}
} else {

View File

@@ -0,0 +1,46 @@
package com.microsoft.codepush.react;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import java.io.File;
import java.io.IOException;
public class CodePushUpdateUtils {
public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFilePath, String currentPackageFolderPath, String newPackageFolderPath) throws IOException{
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath);
ReadableArray deletedFiles = diffManifest.getArray("deletedFiles");
for (int i = 0; i < deletedFiles.size(); i++) {
String fileNameToDelete = deletedFiles.getString(i);
File fileToDelete = new File(newPackageFolderPath, fileNameToDelete);
FileUtils.deleteFileSilently(fileToDelete);
}
}
public static String findJSBundleInUpdateContents(String folderPath) {
File folder = new File(folderPath);
File[] folderFiles = folder.listFiles();
for (File file : folderFiles) {
String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName());
if (file.isDirectory()) {
String mainBundlePathInSubFolder = findJSBundleInUpdateContents(fullFilePath);
if (mainBundlePathInSubFolder != null) {
return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder);
}
} else {
String fileName = file.getName();
int dotIndex = fileName.lastIndexOf(".");
if (dotIndex >= 0) {
String fileExtension = fileName.substring(dotIndex + 1);
if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) {
return fileName;
}
}
}
}
return null;
}
}

View File

@@ -183,31 +183,6 @@ public class CodePushUtils {
return jsonObj;
}
public static String findJSBundleInUpdateContents(String folderPath) {
File folder = new File(folderPath);
File[] folderFiles = folder.listFiles();
for (File file : folderFiles) {
String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName());
if (file.isDirectory()) {
String mainBundlePathInSubFolder = findJSBundleInUpdateContents(fullFilePath);
if (mainBundlePathInSubFolder != null) {
return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder);
}
} else {
String fileName = file.getName();
int dotIndex = fileName.lastIndexOf(".");
if (dotIndex >= 0) {
String fileExtension = fileName.substring(dotIndex + 1);
if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) {
return fileName;
}
}
}
}
return null;
}
public static WritableMap getWritableMapFromFile(String filePath) throws IOException {
String content = FileUtils.readFileToString(filePath);

View File

@@ -1,7 +1,5 @@
package com.microsoft.codepush.react;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;