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

Commit 9dcef06

Browse files
committed
android add google diff-match-patch
1 parent 82b3336 commit 9dcef06

File tree

3 files changed

+2494
-0
lines changed

3 files changed

+2494
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
226226
if (isDiffUpdate) {
227227
String currentPackageFolderPath = getCurrentPackageFolderPath();
228228
CodePushUpdateUtils.copyNecessaryFilesFromCurrentPackage(diffManifestFilePath, currentPackageFolderPath, newUpdateFolderPath);
229+
CodePushUpdateUtils.diffPatchApplyNecessaryFilesFromCurrentPackage(diffManifestFilePath, currentPackageFolderPath, unzippedFolderPath);
229230
File diffManifestFile = new File(diffManifestFilePath);
230231
diffManifestFile.delete();
231232
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Collections;
3030
import java.util.Map;
3131

32+
import name.fraser.neil.plaintext.diff_match_patch;
33+
3234
public class CodePushUpdateUtils {
3335

3436
public static final String NEW_LINE = System.getProperty("line.separator");
@@ -113,6 +115,26 @@ public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFileP
113115
}
114116
}
115117

118+
public static void diffPatchApplyNecessaryFilesFromCurrentPackage(String diffManifestFilePath, String currentPackageFolderPath, String unzippedFolderPath) throws IOException {
119+
diff_match_patch dmp = new diff_match_patch();
120+
JSONObject diffManifest = CodePushUtils.getJsonObjectFromFile(diffManifestFilePath);
121+
try {
122+
JSONArray patchedFiles = diffManifest.getJSONArray("patchedFiles");
123+
for (int i = 0; i < patchedFiles.length(); i++) {
124+
String fileNameToPatch = patchedFiles.getString(i);
125+
File fileToPatch = new File(currentPackageFolderPath, fileNameToPatch);
126+
File patchFile = new File(unzippedFolderPath, fileNameToPatch);
127+
if (fileToPatch.exists() && patchFile.exists()) {
128+
List<diff_match_patch.Patch> patches = dmp.patch_fromText(FileUtils.readFileToString(patchFile.getAbsolutePath()));
129+
Object[] results = dmp.patch_apply((LinkedList<diff_match_patch.Patch>) patches, FileUtils.readFileToString(fileToPatch.getAbsolutePath()));
130+
FileUtils.writeStringToFile((String) results[0], patchFile.getAbsolutePath());
131+
}
132+
}
133+
} catch (JSONException e) {
134+
throw new CodePushUnknownException("Unable to diff-patch-apply files from current package during diff update", e);
135+
}
136+
}
137+
116138
public static String findJSBundleInUpdateContents(String folderPath, String expectedFileName) {
117139
File folder = new File(folderPath);
118140
File[] folderFiles = folder.listFiles();

0 commit comments

Comments
 (0)