hvigorfile.ts 1.45 KB
import { hspTasks } from '@ohos/hvigor-ohos-plugin';
import { appTasks, OhosAppContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin';
import { hvigor, getNode } from '@ohos/hvigor'

// 获取根节点
const rootNode = getNode(__filename);
// 为根节点添加一个afterNodeEvaluate hook 在hook中修改根目录下的build-profile.json5的内容并使能
rootNode.afterNodeEvaluate(node => {
  // 获取app插件的上下文对象
  const appContext = node.getContext(OhosPluginId.OHOS_HSP_PLUGIN) as OhosHspContext;
  // 通过上下文对象获取从根目录build-profile.json5文件中读出来的obj对象
  const buildProfileOpt = appContext.getBuildProfileOpt();
  buildProfileOpt['buildOption']['arkOptions']['buildProfileFields'] = {
    "BUILD_VERSION": getBuildVersion(),
  };
  // 将obj对象设置回上下文对象以使能到构建的过程与结果中
  appContext.setBuildProfileOpt(buildProfileOpt);
})

export default {
  system: hspTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: []         /* Custom plugin to extend the functionality of Hvigor. */
}

function getBuildVersion() {
  // build时间作为版本
  let now = new Date()
  let year = now.getFullYear()
  let month = ('0' + (now.getMonth() + 1)).slice(-2)
  let day = ('0' + (now.getDate())).slice(-2)
  let hours = ('0' + (now.getHours())).slice(-2)
  let minutes = ('0' + (now.getMinutes())).slice(-2)
  let str = year + month + day + hours + minutes
  return str;
}