|
29 | 29 | import java.util.Collections; |
30 | 30 | import java.util.Map; |
31 | 31 |
|
| 32 | +import name.fraser.neil.plaintext.diff_match_patch; |
| 33 | + |
32 | 34 | public class CodePushUpdateUtils { |
33 | 35 |
|
34 | 36 | public static final String NEW_LINE = System.getProperty("line.separator"); |
@@ -113,6 +115,26 @@ public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFileP |
113 | 115 | } |
114 | 116 | } |
115 | 117 |
|
| 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 | + |
116 | 138 | public static String findJSBundleInUpdateContents(String folderPath, String expectedFileName) { |
117 | 139 | File folder = new File(folderPath); |
118 | 140 | File[] folderFiles = folder.listFiles(); |
|
0 commit comments