Skip to content

Commit 7eb2a7c

Browse files
committed
fix: project init and android settings
1 parent eef636b commit 7eb2a7c

44 files changed

Lines changed: 7796 additions & 95 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ android/keystores/debug.keystore
6767
!.yarn/releases
6868
!.yarn/sdks
6969
!.yarn/versions
70+
yarn.lock
7071

7172
# Expo
7273
.expo/

android/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ project(BsDiffPatch)
44
set (CMAKE_VERBOSE_MAKEFILE ON)
55
set (CMAKE_CXX_STANDARD 11)
66

7+
file(GLOB_RECURSE SOURCES "../cpp/*.c" "../cpp/*.cpp" "../cpp/bzlib/*.c" "../cpp/bzlib/*.cpp")
8+
79
add_library(react-native-bs-diff-patch SHARED
8-
../cpp/react-native-bs-diff-patch.cpp
10+
${SOURCES}
911
cpp-adapter.cpp
1012
)
1113

1214
# Specifies a path to native header files.
1315
include_directories(
1416
../cpp
17+
../cpp/bzlib
1518
)

android/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
buildscript {
2+
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["BsDiffPatch_kotlinVersion"]
23
repositories {
34
google()
45
mavenCentral()
56
}
67

78
dependencies {
89
classpath "com.android.tools.build:gradle:7.2.1"
10+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
911
}
1012
}
1113

@@ -14,6 +16,7 @@ def isNewArchitectureEnabled() {
1416
}
1517

1618
apply plugin: "com.android.library"
19+
apply plugin: "kotlin-android"
1720

1821
if (isNewArchitectureEnabled()) {
1922
apply plugin: "com.facebook.react"
@@ -38,7 +41,7 @@ def supportsNamespace() {
3841

3942
android {
4043
if (supportsNamespace()) {
41-
namespace "com.bsdiffpatch"
44+
namespace "com.jimmydaddy.bsdiffpatch"
4245

4346
sourceSets {
4447
main {
@@ -88,11 +91,13 @@ repositories {
8891
google()
8992
}
9093

94+
def kotlin_version = getExtOrDefault("kotlinVersion")
9195

9296
dependencies {
9397
// For < 0.71, this will be from the local maven repo
9498
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
9599
//noinspection GradleDynamicVersion
96100
implementation "com.facebook.react:react-native:+"
101+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
97102
}
98103

android/cpp-adapter.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,37 @@
22
#include "react-native-bs-diff-patch.h"
33

44
extern "C"
5-
JNIEXPORT jdouble JNICALL
6-
Java_com_bsdiffpatch_BsDiffPatchModule_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
7-
return bsdiffpatch::multiply(a, b);
5+
6+
JNIEXPORT jint JNICALL
7+
Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchModule_00024Companion_bsDiffFile(JNIEnv *env,
8+
jobject type, jstring oldFile_,
9+
jstring newFile_, jstring patchFile_) {
10+
const char *oldFile = env->GetStringUTFChars(oldFile_, 0);
11+
const char *newFile = env->GetStringUTFChars(newFile_, 0);
12+
const char *patchFile = env->GetStringUTFChars(patchFile_, 0);
13+
14+
int result = bsdiffpatch::diffFile(oldFile, newFile, patchFile);
15+
16+
env->ReleaseStringUTFChars(oldFile_, oldFile);
17+
env->ReleaseStringUTFChars(newFile_, newFile);
18+
env->ReleaseStringUTFChars(patchFile_, patchFile);
19+
20+
return result;
21+
}
22+
23+
extern "C" JNIEXPORT jint JNICALL
24+
Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchModule_00024Companion_bsPatchFile(JNIEnv *env,
25+
jobject type, jstring oldFile_,
26+
jstring newFile_, jstring patchFile_) {
27+
const char *oldFile = env->GetStringUTFChars(oldFile_, 0);
28+
const char *newFile = env->GetStringUTFChars(newFile_, 0);
29+
const char *patchFile = env->GetStringUTFChars(patchFile_, 0);
30+
31+
int result = bsdiffpatch::patchFile(oldFile, newFile, patchFile);
32+
33+
env->ReleaseStringUTFChars(oldFile_, oldFile);
34+
env->ReleaseStringUTFChars(newFile_, newFile);
35+
env->ReleaseStringUTFChars(patchFile_, patchFile);
36+
37+
return result;
838
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.bsdiffpatch">
2+
package="com.jimmydaddy.bsdiffpatch">
33
</manifest>

android/src/main/java/com/bsdiffpatch/BsDiffPatchModule.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

android/src/main/java/com/bsdiffpatch/BsDiffPatchPackage.java

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

0 commit comments

Comments
 (0)