Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

rn bs diff patch file

Supports both the legacy bridge and React Native New Architecture (TurboModule).

## Installation

```sh
Expand Down
18 changes: 15 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ apply plugin: "kotlin-android"

if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"

react {
jsRootDir = file("../src")
libraryName = "RNBsDiffPatchSpec"
codegenJavaPackageName = "com.jimmydaddy.bsdiffpatch"
}
}

def getExtOrDefault(name) {
Expand All @@ -42,11 +48,18 @@ def supportsNamespace() {
android {
if (supportsNamespace()) {
namespace "com.jimmydaddy.bsdiffpatch"
}

sourceSets {
main {
sourceSets {
main {
if (supportsNamespace()) {
manifest.srcFile "src/main/AndroidManifestNew.xml"
}
java.setSrcDirs(
isNewArchitectureEnabled()
? ["src/main/java", "newarch/java"]
: ["src/main/java", "oldarch/java"]
)
}
}

Expand Down Expand Up @@ -100,4 +113,3 @@ dependencies {
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

30 changes: 20 additions & 10 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#include <jni.h>
#include "react-native-bs-diff-patch.h"

extern "C"

JNIEXPORT jint JNICALL
Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchModule_00024Companion_bsDiffFile(JNIEnv *env,
jobject type, jstring oldFile_,
jstring newFile_, jstring patchFile_) {
static jint bsDiffFileJNI(JNIEnv *env, jstring oldFile_, jstring newFile_, jstring patchFile_) {
const char *oldFile = env->GetStringUTFChars(oldFile_, 0);
const char *newFile = env->GetStringUTFChars(newFile_, 0);
const char *patchFile = env->GetStringUTFChars(patchFile_, 0);
Expand All @@ -20,10 +15,7 @@ Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchModule_00024Companion_bsDiffFile(JNIE
return result;
}

extern "C" JNIEXPORT jint JNICALL
Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchModule_00024Companion_bsPatchFile(JNIEnv *env,
jobject type, jstring oldFile_,
jstring newFile_, jstring patchFile_) {
static jint bsPatchFileJNI(JNIEnv *env, jstring oldFile_, jstring newFile_, jstring patchFile_) {
const char *oldFile = env->GetStringUTFChars(oldFile_, 0);
const char *newFile = env->GetStringUTFChars(newFile_, 0);
const char *patchFile = env->GetStringUTFChars(patchFile_, 0);
Expand All @@ -36,3 +28,21 @@ Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchModule_00024Companion_bsPatchFile(JNI

return result;
}

extern "C" JNIEXPORT jint JNICALL
Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchNative_bsDiffFile(JNIEnv *env,
jobject type,
jstring oldFile_,
jstring newFile_,
jstring patchFile_) {
return bsDiffFileJNI(env, oldFile_, newFile_, patchFile_);
}

extern "C" JNIEXPORT jint JNICALL
Java_com_jimmydaddy_bsdiffpatch_BsDiffPatchNative_bsPatchFile(JNIEnv *env,
jobject type,
jstring oldFile_,
jstring newFile_,
jstring patchFile_) {
return bsPatchFileJNI(env, oldFile_, newFile_, patchFile_);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.jimmydaddy.bsdiffpatch

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule

@ReactModule(name = BsDiffPatchNative.NAME)
class BsDiffPatchModule(reactContext: ReactApplicationContext) :
NativeBsDiffPatchSpec(reactContext) {
override fun getName(): String = BsDiffPatchNative.NAME

override fun patch(
oldFile: String,
newFile: String,
patchFile: String,
promise: Promise
) {
execute(promise) {
BsDiffPatchNative.patch(oldFile, newFile, patchFile)
}
}

override fun diff(
oldFile: String,
newFile: String,
patchFile: String,
promise: Promise
) {
execute(promise) {
BsDiffPatchNative.diff(oldFile, newFile, patchFile)
}
}

private fun execute(promise: Promise, block: () -> Int) {
try {
promise.resolve(block())
} catch (error: BsDiffPatchException) {
promise.reject(error.code, error.message, error)
} catch (error: Exception) {
promise.reject("EUNSPECIFIED", error.message, error)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jimmydaddy.bsdiffpatch

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider

class BsDiffPatchPackage : TurboReactPackage() {
override fun getModule(
name: String,
reactContext: ReactApplicationContext
): NativeModule? {
return if (name == BsDiffPatchNative.NAME) {
BsDiffPatchModule(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
mapOf(
BsDiffPatchNative.NAME to ReactModuleInfo(
BsDiffPatchNative.NAME,
BsDiffPatchModule::class.java.name,
false,
false,
false,
false,
true
)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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

@ReactModule(name = BsDiffPatchNative.NAME)
class BsDiffPatchModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
override fun getName(): String = BsDiffPatchNative.NAME

@ReactMethod
fun patch(oldFile: String?, newFile: String?, patchFile: String?, promise: Promise) {
execute(promise) {
BsDiffPatchNative.patch(
requireArgument(oldFile, "oldFile"),
requireArgument(newFile, "newFile"),
requireArgument(patchFile, "patchFile")
)
}
}

@ReactMethod
fun diff(oldFile: String?, newFile: String?, patchFile: String?, promise: Promise) {
execute(promise) {
BsDiffPatchNative.diff(
requireArgument(oldFile, "oldFile"),
requireArgument(newFile, "newFile"),
requireArgument(patchFile, "patchFile")
)
}
}

private fun requireArgument(value: String?, fieldName: String): String {
if (value.isNullOrEmpty()) {
throw BsDiffPatchException("EINVAL", "$fieldName can not be null or empty")
}
return value
}

private fun execute(promise: Promise, block: () -> Int) {
try {
promise.resolve(block())
} catch (error: BsDiffPatchException) {
promise.reject(error.code, error.message, error)
} catch (error: Exception) {
promise.reject("EUNSPECIFIED", error.message, error)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jimmydaddy.bsdiffpatch

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager

class BsDiffPatchPackage : ReactPackage {
override fun createNativeModules(
reactContext: ReactApplicationContext
): List<NativeModule> = listOf(BsDiffPatchModule(reactContext))

override fun createViewManagers(
reactContext: ReactApplicationContext
): List<ViewManager<*, *>> = emptyList()
}
107 changes: 0 additions & 107 deletions android/src/main/java/com/jimmydaddy/bsdiffpatch/BsDiffPatchModule.kt

This file was deleted.

Loading
Loading