Location>code7788 >text

Hongmeng develops Hvigor plugin to dynamically generate code

Popularity:946 ℃/2024-11-20 18:02:26

Hvigor allows developers to implement their own plug-ins , developers can define their own build logic , and share it with others.Hvigor provides two main ways to implement plug-ins : hvigorfile script development plug-ins based on typescript project development . The following is based on hvigorfile script development plug-ins are introduced.

Based on hvigorfile script development

Script-based development approach, the advantage is that it can realize rapid development, direct editing project or module can be written under the plug-in code, the shortcomings are in multiple projects, can not easily reuse the plug-in code and sharing and distribution.

  1. Import module dependencies.
// Import the interface
import { HvigorPlugin, HvigorNode } from '@ohos/hvigor'
  1. Write the plug-in code.
    Define plugin methods in that implement the HvigorPlugin interface.
// Implementing a custom plugin
function customPlugin(): HvigorPlugin {
    return {
        pluginId: 'customPlugin', apply(node: HvigorNode) {
        apply(node: HvigorNode) {
            // The body of the plugin
            ('hello customPlugin!');
        }
    }
}
  1. Use the plugin in the export statement.
export default {
    system: appTasks,
    plugins:[
        customPlugin() // Application CustomizationPlugin
    ]
}

Dynamically generate navigation anti-obfuscation files using the hvigorfile plugin

When we use navigation's system routing table, every time we add a new page, we need to configure the release environment to prevent confusion. If we put these pages in a fixed directory, it is against our modular design, if we use a fixed prefix or suffix for naming, it always feels a bit redundant, and adding them manually one by one, although it is in line with our code specification design, but it is a bit cumbersome. Is there a more convenient way to deal with this obfuscated configuration?

In fact, we can write an hvigorfilew plugin to automatically generate the obfuscation configuration file. We customize a HvigorPlugin task, through the OhosHapContext object to read the module.json5 file in the routerMap field, you can get the name of the system routing table, and then read the profile directory routing table. Parse the json file memory and write the page path to an obfuscated file so that every time we compile, the anti-obfuscated file is automatically generated and we only need to introduce this file. The example is as follows

import { hapTasks, OhosHapContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin'
import { HvigorPlugin, HvigorNode, FileUtil } from '@ohos/hvigor'

function parseRouterMap(): HvigorPlugin {
  return {
    pluginId: 'parseRouterMap',
    apply(node: HvigorNode) {
      const hapCtx = (OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext
      const moduleJson = ()
      const routerMapName = moduleJson['module']['routerMap'].split(':')[1]
      const dir = ()
      const srcFile = (dir, 'src', 'main', 'resources', 'base', 'profile', `${routerMapName}.json`)
      const json = FileUtil.readJson5(srcFile)
      const routerRuleFile = (dir, '')
      (routerRuleFile)
      const routerMapArray = json['routerMap']
      let rules = '-keep-file-name\n'
      for (const element of routerMapArray) {
        const pageSourceFile = element['pageSourceFile']
        const path = (0, ('.'))
        rules += `${path}\n`
      }
      (routerRuleFile, rules)
    }
  }
}

export default {
  system: hapTasks,
  plugins:[parseRouterMap()]
}

An anti-obfuscation file will be generated in the entry directory after compilation, just introduce this file.

Dynamically generating navigation page enumeration names using the hvigorfile plugin

We in our navigation push jump to a new page, we have to define the system routing table in advance of the page name, because the use of the name and the system routing table is not the same as the name of the definition of the jump page will be a white screen. With the previous experience, we can also dynamically generate an ets file to automatically generate an enumeration of the page names in the system routing table, so that you do not have to configure the system routing table each time, or copy the name. For example, our system routing table is like this

{
  "routerMap": [
    {
      "name": "dialog",
      "pageSourceFile": "src/main/ets/pages/dialog/",
      "buildFunction": "dialogBuilder"
    },
    {
      "name": "web",
      "pageSourceFile": "src/main/ets/pages/web/",
      "buildFunction": "webBuilder"
    },
    {
      "name": "login",
      "pageSourceFile": "src/main/ets/pages/login/",
      "buildFunction": "loginBuilder"
    }
  ]
}

We now implement an hvigorfile plugin to parse the name field in the system routing table and generate the corresponding enumeration value. The example is as follows

import { hapTasks, OhosHapContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin'
import { HvigorPlugin, HvigorNode, FileUtil } from '@ohos/hvigor'

function parseRouterMap(): HvigorPlugin {
  return {
    pluginId: 'parseRouterMap',
    apply(node: HvigorNode) {
      const hapCtx = (OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext
      const moduleJson = ()
      const routerMapName = moduleJson['module']['routerMap'].split(':')[1]
      const dir = ()
      const srcFile = (dir, 'src', 'main', 'resources', 'base', 'profile', `${routerMapName}.json`)
      const json = FileUtil.readJson5(srcFile)
      const routerMapFile = (dir, 'src', 'main', 'ets', '')
      (routerMapFile)
      const routerMapArray = json['routerMap']
      let ss = ''
      for (const element of routerMapArray) {
        const name = element['name']
        ss += `  ${name} = '${name}',\n`
      }
      ss = `export enum Pages {\n${ss}}`
      (routerMapFile, ss)
    }
  }
}

export default {
  system: hapTasks,
  plugins:[parseRouterMap()]
}

We generated a file in the ets directory and generated enumeration values for all navigation pages, so that when the page jumps, we can use these enumeration values without fear of error. The content is as follows

export enum Pages {
  dialog = 'dialog',
  web = 'web',
  login = 'login',
}