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

Commit 0712451

Browse files
committed
outsource more methods to CodePushUpdateUtils
1 parent 44d6b8d commit 0712451

File tree

5 files changed

+77
-64
lines changed

5 files changed

+77
-64
lines changed

CodePush.m

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -359,32 +359,34 @@ - (void)savePendingUpdate:(NSString *)packageHash
359359
resolver:(RCTPromiseResolveBlock)resolve
360360
rejecter:(RCTPromiseRejectBlock)reject)
361361
{
362-
[CodePushPackage downloadPackage:updatePackage
363-
// The download is progressing forward
364-
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
365-
// Notify the script-side about the progress
366-
[self.bridge.eventDispatcher
367-
sendDeviceEventWithName:@"CodePushDownloadProgress"
368-
body:@{
369-
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
370-
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
371-
}];
372-
}
373-
// The download completed
374-
doneCallback:^{
375-
NSError *err;
376-
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
377-
378-
if (err) {
379-
return reject(err);
362+
dispatch_async(dispatch_get_main_queue(), ^{
363+
[CodePushPackage downloadPackage:updatePackage
364+
// The download is progressing forward
365+
progressCallback:^(long long expectedContentLength, long long receivedContentLength) {
366+
// Notify the script-side about the progress
367+
[self.bridge.eventDispatcher
368+
sendDeviceEventWithName:@"CodePushDownloadProgress"
369+
body:@{
370+
@"totalBytes":[NSNumber numberWithLongLong:expectedContentLength],
371+
@"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength]
372+
}];
380373
}
381-
382-
resolve(newPackage);
383-
}
384-
// The download failed
385-
failCallback:^(NSError *err) {
386-
reject(err);
387-
}];
374+
// The download completed
375+
doneCallback:^{
376+
NSError *err;
377+
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
378+
379+
if (err) {
380+
return reject(err);
381+
}
382+
383+
resolve(newPackage);
384+
}
385+
// The download failed
386+
failCallback:^(NSError *err) {
387+
reject(err);
388+
}];
389+
});
388390
}
389391

390392
/*

android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,17 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
225225
// Merge contents with current update based on the manifest
226226
String diffManifestFilePath = CodePushUtils.appendPathComponent(unzippedFolderPath,
227227
DIFF_MANIFEST_FILE_NAME);
228-
File diffManifestFile = new File(unzippedFolderPath, DIFF_MANIFEST_FILE_NAME);
229-
if (diffManifestFile.exists()) {
228+
if (FileUtils.fileAtPathExists(diffManifestFilePath)) {
230229
String currentPackageFolderPath = getCurrentPackageFolderPath();
231-
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
232-
WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath);
233-
ReadableArray deletedFiles = diffManifest.getArray("deletedFiles");
234-
for (int i = 0; i < deletedFiles.size(); i++) {
235-
String fileNameToDelete = deletedFiles.getString(i);
236-
File fileToDelete = new File(newPackageFolderPath, fileNameToDelete);
237-
FileUtils.deleteFileSilently(fileToDelete);
238-
}
230+
CodePushUpdateUtils.copyNecessaryFilesFromCurrentPackage(diffManifestFilePath, currentPackageFolderPath, newPackageFolderPath);
239231
}
240232

241-
// Move merged update contents to a folder with the packageHash as its name
242233
FileUtils.copyDirectoryContents(unzippedFolderPath, newPackageFolderPath);
243234
FileUtils.deleteFileAtPathSilently(unzippedFolderPath);
244235

245236
// For zip updates, we need to find the relative path to the jsBundle and save it in the
246237
// metadata so that we can find and run it easily the next time.
247-
String relativeBundlePath = CodePushUtils.findJSBundleInUpdateContents(newPackageFolderPath);
238+
String relativeBundlePath = CodePushUpdateUtils.findJSBundleInUpdateContents(newPackageFolderPath);
248239

249240
if (relativeBundlePath == null) {
250241
throw new CodePushInvalidUpdateException();
@@ -257,6 +248,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
257248
RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath +
258249
" in update package.", e);
259250
}
251+
260252
updatePackage = CodePushUtils.convertJsonObjectToWriteable(updatePackageJSON);
261253
}
262254
} else {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.microsoft.codepush.react;
2+
3+
import com.facebook.react.bridge.ReadableArray;
4+
import com.facebook.react.bridge.WritableMap;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
9+
public class CodePushUpdateUtils {
10+
11+
public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFilePath, String currentPackageFolderPath, String newPackageFolderPath) throws IOException{
12+
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
13+
WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath);
14+
ReadableArray deletedFiles = diffManifest.getArray("deletedFiles");
15+
for (int i = 0; i < deletedFiles.size(); i++) {
16+
String fileNameToDelete = deletedFiles.getString(i);
17+
File fileToDelete = new File(newPackageFolderPath, fileNameToDelete);
18+
FileUtils.deleteFileSilently(fileToDelete);
19+
}
20+
}
21+
22+
public static String findJSBundleInUpdateContents(String folderPath) {
23+
File folder = new File(folderPath);
24+
File[] folderFiles = folder.listFiles();
25+
for (File file : folderFiles) {
26+
String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName());
27+
if (file.isDirectory()) {
28+
String mainBundlePathInSubFolder = findJSBundleInUpdateContents(fullFilePath);
29+
if (mainBundlePathInSubFolder != null) {
30+
return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder);
31+
}
32+
} else {
33+
String fileName = file.getName();
34+
int dotIndex = fileName.lastIndexOf(".");
35+
if (dotIndex >= 0) {
36+
String fileExtension = fileName.substring(dotIndex + 1);
37+
if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) {
38+
return fileName;
39+
}
40+
}
41+
}
42+
}
43+
44+
return null;
45+
}
46+
}

android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,6 @@ public static JSONObject convertReadableToJsonObject(ReadableMap map) {
183183
return jsonObj;
184184
}
185185

186-
public static String findJSBundleInUpdateContents(String folderPath) {
187-
File folder = new File(folderPath);
188-
File[] folderFiles = folder.listFiles();
189-
for (File file : folderFiles) {
190-
String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName());
191-
if (file.isDirectory()) {
192-
String mainBundlePathInSubFolder = findJSBundleInUpdateContents(fullFilePath);
193-
if (mainBundlePathInSubFolder != null) {
194-
return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder);
195-
}
196-
} else {
197-
String fileName = file.getName();
198-
int dotIndex = fileName.lastIndexOf(".");
199-
if (dotIndex >= 0) {
200-
String fileExtension = fileName.substring(dotIndex + 1);
201-
if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) {
202-
return fileName;
203-
}
204-
}
205-
}
206-
}
207-
208-
return null;
209-
}
210-
211186
public static WritableMap getWritableMapFromFile(String filePath) throws IOException {
212187

213188
String content = FileUtils.readFileToString(filePath);

android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.microsoft.codepush.react;
22

3-
import android.util.Log;
4-
53
import java.io.BufferedInputStream;
64
import java.io.BufferedReader;
75
import java.io.File;

0 commit comments

Comments
 (0)