中国城乡与住房建设部网站首页网页系统升级每天自动更新
2026/2/10 4:04:32 网站建设 项目流程
中国城乡与住房建设部网站首页,网页系统升级每天自动更新,济南shuncheng科技 网站建设,手机免费制图软件自定义任务介绍 创建一个自定义任务工程#xff0c;并且实现一个基础的任务流程 将自定义任务注册到harmony工程中的module中并执行 1、创建自定义任务工程 安装相应的nodejs与npm#xff08;可以用安装环境后默认自带的node环境#xff09;安装typescript环境#xff0…自定义任务介绍创建一个自定义任务工程并且实现一个基础的任务流程将自定义任务注册到harmony工程中的module中并执行1、创建自定义任务工程安装相应的nodejs与npm可以用安装环境后默认自带的node环境安装typescript环境npm install -g typescript新建一个harmony工程并且在工程内创建两个演示module以及一个Hvigor的plugin工程并且在plugin目录下通过tsc --init和npm init初始化node工程通过npm命令行安装hvigor 与 hvigor-ohos-plugin安装前需要添加.npmrc文件详情可参考 Hvigor 构建自动化部署脚本(一) Hvigor介绍以及使用npm install ohos/hvigor npm install ohos/hvigor-ohos-plugin完成后工程目录结构为下图所示我们可以直接用vscode将plugin工程打开并且创建我们自定义任务工程结构如下自定义任务需要实现HvigorTask接口并且实现内部的方法接口详情如下export interface HvigorTask { /** * 任务名称全局唯一 */ name: string; /** * task 上下文 */ context?: (() any) | any; /** * Task 定义增量输入接口 * * param input */ input?: (input: TaskInput) void; /** * task 定义增量输出接口 * * param output */ output?: (output: TaskOutput) void; /** * task beforeRun 在run方法之前执行 * * param taskContext */ beforeRun?: (taskContext: HvigorTaskContext) void | Promisevoid; /** * 增量输入输出 * * param taskContext */ afterRun?: (taskContext: HvigorTaskContext) void | Promisevoid; /** * Task执行逻辑执行时调用此方法 * * param taskContext */ run: (taskContext: HvigorTaskContext) void | Promisevoid; /** * 当前Task依赖的Task列表 * 前置依赖的tasks, 先执行前置依赖再执行此task */ dependencies?: (() string[]) | string[]; /** * 后置依赖的tasks, 执行后置依赖前必须先执行此task */ postDependencies?: (() string[]) | string[]; }我们主要需要实现run方法这个是当执行到我们任务时任务的主要功能并且声明任务处于任务链中的环节通过设置dependencies设置前置和后置依赖下面是一个演示任务作用于harmony工程中的module级工程可以实现编译并且自动拷贝har包到sdk文件内/** * 组件Har包打包导出脚本实现 */ import path from node:path; import os from node:os; import { HvigorTask, HvigorTaskContext, TaskInput, TaskOutput } from ohos/hvigor; import fs from fs-extra export class ExampleTask implements HvigorTask { private harPath: string /build/default/outputs/default/; constructor() { } name: string ExampleTask; context?: any; run(taskContext: HvigorTaskContext) { console.log(taskContext.modulePath); function getHarInfo() { try { let res fs.readJSONSync(path.join(taskContext.modulePath, oh-package.json5)) return { name: res.name, version: res.version } } catch (e) { console.log(e); } } let harInfo getHarInfo(); let source path.join(taskContext.modulePath, this.harPath, harInfo!.name .har); let type ; if (os.platform() win32) { type taskContext.modulePath.replace(\\ harInfo?.name, ) .substring(taskContext.modulePath.replace(\\ harInfo?.name, ).lastIndexOf(\\)) } else { type taskContext.modulePath.replace(/ harInfo?.name, ) .substring(taskContext.modulePath.replace(/ harInfo?.name, ).lastIndexOf(/)) } console.log(bundle, taskContext.modulePath.replace(/ harInfo?.name, ), type, type); if (!fs.existsSync(path.join(taskContext.modulePath, /sdk/, type))) { fs.mkdirSync(path.join(taskContext.modulePath, /sdk/, type), { recursive: true }) } let dist path.join(taskContext.modulePath, /sdk/, harInfo!.name - harInfo?.version .har) console.log(开始拷贝输出文件, source, dist); fs.copyFileSync(source, dist) }; beforeRun(taskContext: HvigorTaskContext) { this.dependencies [defaultPackageHar] } dependencies [defaultPackageHar] }2、将自定义任务注册到harmony工程中的module中并执行在需要增加任务的module下hvigorfile.ts中增加任务注册如下图所示import { harTasks } from ohos/hvigor-ohos-plugin; import { ExampleTask } from ../plugin/index.ts export function ExamplePlugin() { return { pluginId: ExamplePlugin, apply(pluginContext) { pluginContext.registerTask(new ExampleTask()) } } } export default { system: harTasks, plugins: [ExamplePlugin()] };注册一个任务名称为ExamplePlugin的任务并且导出任务级别为harTasks等级注册后可以通过命令hvigorwwindows为hvigorw.bat --mode module -p productdefault -p modulelibrarydefault ExampleTask 执行任务执行成功后控制台显示

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询