소스 검색

feat: 集成数据库可视化插件, 自动转发端口

YuJing 1 개월 전
부모
커밋
50038ed836

+ 8 - 2
commons/basic/src/main/ets/rdb/utils/RelationalStoreUtis.ts

@@ -16,10 +16,16 @@ export class RelationalStoreUtils {
   private static cloudLoadStore: relationalStore.RdbStore | undefined = undefined;
 
   //初始化数据库
-  static initReplayCloudStore(context: Context, success?: () => void) {
+  static initReplayCloudStore(context: Context, success?: () => void, isDeBug: boolean = false) {
+    let securityLevel: relationalStore.SecurityLevel = relationalStore.SecurityLevel.S3
+    // 在 debug模式下,数据库安全级别为S1。 确保数据库可视化工具可以正常访问
+    if (isDeBug) {
+      securityLevel = relationalStore.SecurityLevel.S1
+    }
+
     const REPLAY_CLOUD_STORE_CONFIG: relationalStore.StoreConfig = {
       name: 'YT_HM.db', // 数据库文件名
-      securityLevel: relationalStore.SecurityLevel.S3, // 数据库安全级别
+      securityLevel: securityLevel, // 数据库安全级别
       encrypt: false, // 可选参数,指定数据库是否加密,默认不加密
       customDir: 'customDir/subCustomDir', // 可选参数,数据库自定义路径。数据库将在如下的目录结构中被创建:context.databaseDir + '/rdb/' + customDir,其中context.databaseDir是应用沙箱对应的路径,'/rdb/'表示创建的是关系型数据库,customDir表示自定义的路径。当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。
       isReadOnly: false // 可选参数,指定数据库是否以只读方式打开。该参数默认为false,表示数据库可读可写。该参数为true时,只允许从数据库读取数据,不允许对数据库进行写操作,否则会返回错误码801。

+ 26 - 1
hvigorfile.ts

@@ -2,6 +2,7 @@ import { appTasks } from '@ohos/hvigor-ohos-plugin';
 import { HvigorPlugin, HvigorNode } from '@ohos/hvigor';
 import fs from 'fs';
 import path from 'path';
+import { execSync } from 'child_process';
 
 function customPlugin(): HvigorPlugin {
     return {
@@ -478,10 +479,34 @@ function rDBPlugin(): HvigorPlugin {
     }
 }
 
+function forwardingPort(){
+    return {
+        pluginId: 'customPlugin',
+        apply(node: HvigorNode) {
+            try {
+                var connectKeys = execSync('hdc list targets', { encoding: 'utf-8' }).split('\n');
+
+                var targetKey = connectKeys[0].trim();
+
+                var operation = `hdc -t ${targetKey} fport rm tcp:8080 tcp:8080`
+                var result = execSync(operation, { encoding: 'utf-8' })
+                console.log(`执行命令 ${operation}, 删除端口结果:${result.trim()}`);
+
+                operation = `hdc -t ${targetKey} fport tcp:8080 tcp:8080`
+                result = execSync(operation, { encoding: 'utf-8' })
+                console.log(`执行命令 ${operation}, 转发端口结果:${result.trim()}`);
+            } catch (error) {
+                console.error(`执行失败: ${error.message}`);
+            }
+        }
+    }
+}
+
 export default {
     system: appTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
     plugins: [
         customPlugin(),  // 应用自定义Plugin
-        rDBPlugin()
+        rDBPlugin(),
+        forwardingPort()
     ]         /* Custom plugin to extend the functionality of Hvigor. */
 }

+ 1 - 0
oh-package.json5

@@ -2,6 +2,7 @@
   "modelVersion": "5.0.2",
   "description": "Please describe the basic information.",
   "dependencies": {
+    "@hadss/debug-db": "^1.0.0-rc.10"
   },
   "devDependencies": {
     "@ohos/hypium": "1.0.21",

+ 8 - 0
products/entry/build-profile.json5

@@ -1,6 +1,14 @@
 {
   "apiType": "stageMode",
   "buildOption": {
+    "arkOptions": {
+      "branchElimination": true,
+      "runtimeOnly": {
+        "packages": [
+          '@hadss/debug-db'
+        ],
+      },
+    }
   },
   "buildOptionSet": [
     {

+ 12 - 1
products/entry/src/main/ets/entryability/EntryAbility.ets

@@ -4,6 +4,7 @@ import { window } from '@kit.ArkUI';
 import {
   AppStorageKeyCollect, IBestInit, RelationalStoreUtils, YTAvoid, YTBreakPoint, YTLog
 } from 'basic';
+import BuildProfile from 'BuildProfile';
 
 const DOMAIN = 0x0000;
 
@@ -22,6 +23,17 @@ export default class EntryAbility extends UIAbility {
         hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
         return;
       }
+
+      // 动态引入: 仅在 DEBUG 模式下引入该模块
+      if (BuildProfile.DEBUG) {
+        const pkg = '@hadss/debug-db';
+        import(pkg).then(async (ns: ESObject) => {
+          ns.DebugDB.initialize(this.context, { port: 8080, defaultStart: true });
+        });
+      }
+      RelationalStoreUtils.initReplayCloudStore(this.context, null, BuildProfile.DEBUG)
+
+
       YTLog.init({
         tag: "YTLog",
         domain: 0x0000,
@@ -66,7 +78,6 @@ export default class EntryAbility extends UIAbility {
         AppStorage.setOrCreate(AppStorageKeyCollect.SCREEN_WIDTH, px2vp(size.width))
         AppStorage.setOrCreate(AppStorageKeyCollect.SCREEN_HEIGHT, px2vp(size.height))
       })
-      RelationalStoreUtils.initReplayCloudStore(this.context)
     });
 
   }