react-native种怎么使用patches文件呢?

关于patches文件,那么需要知道diff是什么,diff和patch是什么关系,他们做什么用的,知道这些,我们就可以知道我们要做什么了。

那么patches文件夹内通常是我们对第三方库的.d.ts文件的类型一个补充或修改的差异,知道这个重点之后,我们就知道,我们需要的patch文件是第三方库的.d.ts文件更改前后的差异对比(这里就用到了diff,比较并记录前后差异)。例如下边的patch文件。

diff --git a/node_modules/react-native-ble-manager/dist/esm/types.d.ts b/node_modules/react-native-ble-manager/dist/esm/types.d.ts
index a9b198c..89b3327 100644
--- a/node_modules/react-native-ble-manager/dist/esm/types.d.ts
+++ b/node_modules/react-native-ble-manager/dist/esm/types.d.ts
@@ -202,22 +202,37 @@ export interface Descriptor {
     value: string;
     uuid: string;
 }
+export type iOSProperties = Array<
+  | 'Broadcast'
+  | 'Read'
+  | 'WriteWithoutResponse'
+  | 'Write'
+  | 'Notify'
+  | 'Indicate'
+  | 'AuthenticatedSignedWrites'
+  | 'ExtendedProperties'
+  | 'NotifyEncryptionRequired'
+  | 'IndicateEncryptionRequired'
+>;
+
+export type AndroidProperties = {
+  Broadcast?: "Broadcast";
+  Read?: "Read";
+  WriteWithoutResponse?: "WriteWithoutResponse";
+  Write?: "Write";
+  Notify?: "Notify";
+  Indicate?: "Indicate";
+  AuthenticatedSignedWrites?: "AuthenticatedSignedWrites";
+  ExtendedProperties?: "ExtendedProperties";
+  NotifyEncryptionRequired?: "NotifyEncryptionRequired";
+  IndicateEncryptionRequired?: "IndicateEncryptionRequired";
+};
+
 export interface Characteristic {
     /**
      * See https://developer.apple.com/documentation/corebluetooth/cbcharacteristicproperties
      */
-    properties: {
-        Broadcast?: "Broadcast";
-        Read?: "Read";
-        WriteWithoutResponse?: "WriteWithoutResponse";
-        Write?: "Write";
-        Notify?: "Notify";
-        Indicate?: "Indicate";
-        AuthenticatedSignedWrites?: "AuthenticatedSignedWrites";
-        ExtendedProperties?: "ExtendedProperties";
-        NotifyEncryptionRequired?: "NotifyEncryptionRequired";
-        IndicateEncryptionRequired?: "IndicateEncryptionRequired";
-    };
+    properties: iOSProperties | AndroidProperties;
     characteristic: string;
     service: string;
     descriptors?: Descriptor[];

那么我们该怎么生成这种patch文件呢?肯定不是我们手写的,上边的patch文件内容的前八行什么意思呢?这里简单介绍下

第一行 Git 版本控制系统中 diff 命令输出的一部分,用于展示两个文件之间的差异。下面是对这一行的详细解释:

  • diff --git: 这表示接下来的内容是 Git 用来展示两个文件之间差异的输出。--git 参数告诉 Git 接下来的差异是针对 Git 仓库中的文件。

  • a/b/: 这两个前缀分别表示 "before"(之前)和 "after"(之后)。它们用来区分差异中涉及的两个文件版本。

第二行 index a9b198c..89b3327 100644

  • 这部分显示了文件在 Git 中的索引,d132ad62 是文件在旧版本中的索引哈希值,be06d152 是文件在新版本中的索引哈希值。这些哈希值是 Git 用来唯一标识文件内容的。

  • 100644: 这是一个权限模式,表示文件的权限。在 Git 中,100644 表示该文件是一个普通文件,具有读写权限。

第五行 @@ -202,22 +202,37 @@

  • 这一行是 diff 输出中的一个特殊标记,它指示接下来的行是文件中发生变更的部分。@@ 后面跟着的是两个数字范围,分别对应原始文件和修改后文件中的行号。

生成patch文件的步骤

1.找到你需要改的第三方库的.d.ts文件,然后进行Type的修改

2.执行代码 yarn patch-package react-native-ble-manager(这里写你改的包的名)

3.滞后性结束会自己生成一个你需要的patch文件          

按照你所说操作后 D:\AwesomeProject>npx react-native run-android info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag. Jetifier found 1985 file(s) to forward-jetify. Using 12 workers... info JS server already running. info Installing the app... > Configure project :react-native-reanimated AAR for react-native-reanimated has been found D:\AwesomeProject\node_modules\react-native-reanimated\android\react-native-reanimated-68-jsc.aar Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings 6 actionable tasks: 6 up-to-date FAILURE: Build failed with an exception. * Where: Build file 'D:\AwesomeProject\node_modules\react-native-tcp\android\build.gradle' line: 47 * What went wrong: A problem occurred evaluating project ':react-native-tcp'. > Could not find method compile() for arguments [com.facebook.react:react-native:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 22s error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * Where: Build file 'D:\AwesomeProject\node_modules\react-native-tcp\android\build.gradle' line: 47 * What went wrong: A problem occurred evaluating project ':react-native-tcp'. > Could not find method compile() for arguments [com.facebook.react:react-native:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 22s at makeError (D:\AwesomeProject\node_modules\execa\index.js:174:9) at D:\AwesomeProject\node_modules\execa\index.js:278:16 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async runOnAllDevices (D:\AwesomeProject\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:109:5) at async Command.handleAction (D:\AwesomeProject\node_modules\@react-native-community\cli\build\index.js:192:9) info Run CLI with --verbose flag for more details.
08-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值