找了一圈发现官方的文档都是最新的,3.2很多API都不支持,比如获取OhosAppContext,通过OhosAppContext来获取应用版本号,最终是通过读取app.json5的文件内容来读取版本号,最终修改entry下的hvigorfile.ts如下,执行./hvigorw assembleHap,或者编译打包就可以生成新的hap名字了
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
import fs from 'fs'
import { HvigorNode, HvigorPlugin, HvigorTaskContext } from "@ohos/hvigor";
import { OhosAppContext, OhosPluginId } from '@ohos/hvigor-ohos-plugin';
import { AppJson } from "@ohos/hvigor-ohos-plugin/src/options/configure/app-json-options";const getDate = () => {return new Date().toISOString().split('T')[0]; // 返回 YYYY-MM-DD 格式
};export function rename(): HvigorPlugin {return {pluginId: 'renameHapPlugin',apply(node: HvigorNode) {// 插件主体node.registerTask({// 任务名称name: 'renameHapTask',// 重命名任务在default@SignHap任务执行完成后执行dependencies: ['default@SignHap'],// 重命名任务在default@assembleHap任务执行完成前执行postDependencies: ['assembleHap'],run: (taskContext: HvigorTaskContext) => {console.log(`开始执行重命名任务`)// 获取模块名const moduleName = taskContext.moduleName// 获取模块路径const modulePath = taskContext.modulePath// 假设我们在entry目录的hvigorfile.ts文件调用插件,那拿到的模块名就是entry,模块路径就是entry模块的绝对路径。console.log(`模块名:${moduleName}`)console.log(`模块路径:${modulePath}`)// hap所在路径const originSignFilePath = `${modulePath}/build/default/outputs/default/${moduleName}-default-signed.hap`const originUnsignFilePath = `${modulePath}/build/default/outputs/default/${moduleName}-default-unsigned.hap`console.log(`原签名文件路径:${originSignFilePath}`)console.log(`原未签名文件路径:${originUnsignFilePath}`)// 新文件所在的目录const targetFileDir = `${modulePath}/build/default/outputs/default/target`// 创建目录fs.mkdir(targetFileDir, { recursive: true }, (err) => {console.log(`目录创建失败:{err}`)})// 获取父节点// const parentNode = node.getParentNode()// // 获取OhosAppContext// const appContext = parentNode?.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext// 获取项目名// 获取AppScope目录下app.json文件里面的json// const appOptObj: AppJson.AppOptObj = appContext.getAppJsonOpt()const appJsonPath = './AppScope/app.json5';const content = fs.readFileSync(appJsonPath, 'utf8');const appJson = JSON.parse(content);// 只获取版本号const versionName = appJson.app.description;console.log(`app.json5获取的版本号:${versionName}`)// const versionName = versionconsole.log(`版本:${versionName}`)const date = getDate()// 新文件路径const targetSignFilePath = `${modulePath}/build/default/outputs/default/target/${versionName}_${date}-signed.hap`const targetUnsignFilePath = `${modulePath}/build/default/outputs/default/target/${versionName}_${date}-default-unsigned.hap`// 复制文件if (fs.existsSync(originSignFilePath)) {// 原文件存在才复制fs.copyFileSync(originSignFilePath, targetSignFilePath)// fs.unlink(originSignFilePath, (err: BusinessError) => {// })}if (fs.existsSync(originUnsignFilePath)) {// 原文件存在才复制fs.copyFileSync(originUnsignFilePath, targetUnsignFilePath)// fs.unlink(originUnsignFilePath, (err: BusinessError) => {// })}console.log(`重命名任务执行完成`)}})// const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;// const buildProfile = appContext.getBuildProfileOpt()//// buildProfile.app.products[0].output.artifactName = 'TestartifactName.0.0.1';//// appContext.setBuildProfileOpt(buildProfile);}}
}export default {system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */plugins: [rename()] /* Custom plugin to extend the functionality of Hvigor. */
}