-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBsDiffPatchModule.kt
More file actions
107 lines (98 loc) · 3.66 KB
/
BsDiffPatchModule.kt
File metadata and controls
107 lines (98 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.jimmydaddy.bsdiffpatch
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule
import com.jimmydaddy.bsdiffpatch.BsDiffPatchModule
import java.io.File
@ReactModule(name = BsDiffPatchModule.NAME)
class BsDiffPatchModule(reactContext: ReactApplicationContext?) :
ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return NAME
}
private fun getFileDir(dir: String): String {
if (dir.startsWith("file://")) {
return dir.substring(7)
}
return dir
}
// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
fun patch(oldFile: String?, newFile: String?, patchFile: String?, promise: Promise) {
if (oldFile == null || newFile == null || patchFile == null || oldFile.isEmpty() || newFile.isEmpty() || patchFile.isEmpty()) {
promise.reject("error", "oldFile, newFile, patchFile can not be null or empty")
return
}
if (oldFile == newFile || oldFile == patchFile || newFile == patchFile) {
promise.reject("error", "oldFile, newFile, patchFile can not be the same")
return
}
val oldFileObj = File(getFileDir(oldFile))
if (!oldFileObj.exists()) {
promise.reject("error", "oldFile: $oldFile not exist")
return
}
val patchFileObj = File(getFileDir(patchFile))
if (!patchFileObj.exists()) {
promise.reject("error", "patchFile: $patchFile not exist")
return
}
val newFileObj = File(getFileDir(newFile))
if (newFileObj.exists()) {
promise.reject("error", "newFile: $newFile already exist")
return
}
try {
val result = bsPatchFile(oldFileObj.absolutePath, newFileObj.absolutePath, patchFileObj.absolutePath)
promise.resolve(result)
} catch (e: Exception) {
promise.reject("error", e.message)
}
}
@ReactMethod
fun diff(oldFile: String?, newFile: String?, patchFile: String?, promise: Promise) {
if (oldFile == null || newFile == null || patchFile == null || oldFile.isEmpty() || newFile.isEmpty() || patchFile.isEmpty()) {
promise.reject("error", "oldFile, newFile, patchFile can not be null or empty")
return
}
if (oldFile == newFile || oldFile == patchFile || newFile == patchFile) {
promise.reject("error", "oldFile, newFile, patchFile can not be the same")
return
}
val oldFileObj = File(getFileDir(oldFile))
if (!oldFileObj.exists()) {
promise.reject("error", "oldFile: $oldFile not exist")
return
}
val newFileObj = File(getFileDir(newFile))
if (!newFileObj.exists()) {
promise.reject("error", "newFile: $newFile not exist")
return
}
val patchFileObj = File(getFileDir(patchFile))
if (patchFileObj.exists()) {
promise.reject("error", "patchFile: $patchFile already exist")
return
}
try {
val result = bsDiffFile(oldFileObj.absolutePath, newFileObj.absolutePath, patchFileObj.absolutePath)
promise.resolve(result)
} catch (e: Exception) {
promise.reject("error", e.message)
}
}
companion object {
const val NAME = "BsDiffPatch"
init {
System.loadLibrary("react-native-bs-diff-patch")
}
// Used to load the 'native-lib' library on application startup.
// get new file from old file and patch file
private external fun bsPatchFile(oldFile: String, newFile: String, patchFile: String): Int
// generate patch file
private external fun bsDiffFile(oldFile: String, newFile: String, patchFile: String): Int
}
}