OpenHarmonyOS 3.2 编译生成的hap和app文件的名称如何配置追加版本号?

时间:2025-02-01 22:56:54
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 = version console.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. */ }