往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)
插件上下文
OhosPluginId
本组件是hvigor-ohos-plugin插件id常量类。
导入模块
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
常量:
常量名 | 类型 | 描述 |
---|---|---|
OHOS_APP_PLUGIN | string | AppPlugin插件ID |
OHOS_HAP_PLUGIN | string | HapPlugin插件ID |
OHOS_HSP_PLUGIN | string | HspPlugin插件ID |
OHOS_HAR_PLUGIN | string | HarPlugin插件ID |
OhosAppContext
本组件是appTasks插件对外提供的上下文扩展接口,包括工程信息、product信息等。
导入模块
import { OhosAppContext } from '@ohos/hvigor-ohos-plugin';
getProjectName
getProjectName: () => string
获取工程名称。
返回值:
类型 | 说明 |
---|---|
string | 工程名称 |
getProjectPath
getProjectPath: () => string
获取工程路径。
返回值:
类型 | 说明 |
---|---|
string | 工程路径 |
getBuildRootPath
getBuildRootPath: () => string
获取构建目录根路径。
返回值:
类型 | 说明 |
---|---|
string | 构建根路径 |
getBuildProductOutputPath
getBuildProductOutputPath: () => string
获取当前product构建的打包输出路径。
返回值:
类型 | 说明 |
---|---|
string | 当前product构建的打包输出路径 |
getCurrentProduct
getCurrentProduct: () => Product
获取当前构建指定的product对象。
返回值:
类型 | 说明 |
---|---|
Product | 当前构建指定的product对象 |
getBuildMode
getBuildMode: () => string
获取当前构建指定的BuildMode。
返回值:
类型 | 说明 |
---|---|
string | 当前构建指定的BuildMode |
getAppJsonOpt
getAppJsonOpt: () => any
获取当前构建的app.json5文件中内容的obj对象。
返回值:
类型 | 说明 |
---|---|
any | 当前构建的app.json5文件中内容的obj对象 |
setAppJsonOpt
setAppJsonOpt: (appJsonOpt) => void
修改当前构建的app.json5文件中内容的obj对象。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
appJsonOpt | any | 是 | 设置当前构建的app.json5文件解析出来的obj对象 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks, OhosPluginId, OhosAppContext, AppJson } from '@ohos/hvigor-ohos-plugin';
import { hvigor, getNode, HvigorNode } from '@ohos/hvigor';
hvigor.nodesEvaluated(() => {
const node: HvigorNode = getNode(__filename);
const appContext = node.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
console.log('projectName:', appContext.getProjectName());
const appJson5: AppJson.AppOptObj = appContext.getAppJsonOpt();
if (appContext.getBuildMode() === 'debug') {
appJson5.app.versionName = '1.0.0-debug';
} else {
appJson5.app.versionName = '1.0.0-release';
}
appContext.setAppJsonOpt(appJson5);
});
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
说明
setAppJsonOpt会进行schema校验,如果传入的对象不符合校验规则则会抛出异常。
getBuildProfileOpt
getBuildProfileOpt: () => any
获取当前构建的根目录下build-profile.json5文件中内容的obj对象。
返回值:
类型 | 说明 |
---|---|
any | 当前构建的根目录下build-profile.json5文件中内容的obj对象 |
setBuildProfileOpt
setBuildProfileOpt: (buildProfileOpt) => any
设置当前构建的build-profile.json5文件中内容的obj对象。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
buildProfileOpt | any | 是 | 设置当前构建的根目录下build-profile.json5文件中内容的obj对象 |
说明
setBuildProfileOpt会进行schema校验,如果传入的对象不符合校验规则则会抛出异常。
getOhpmDependencyInfo5.0.0+
getOhpmDependencyInfo: () => Record<string, OhpmDependencyInfo> | object
获取工程下oh-package.json5中配置的依赖信息。
返回值:
类型 | 说明 |
---|---|
Record<string, OhpmDependencyInfo> | object | oh-package.json5中配置的依赖信息 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const ohpmInfo = rootNodeContext.getOhpmDependencyInfo();
console.log(ohpmInfo)
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
执行hvigorw --sync输出示例:
// 工程下oh-package.json5中已配置dependencies依赖
{
har: {
name: 'har01',
version: '1.0.0',
dependencies: {},
packagePath: 'D:\\project\\deveco_project\\MyApplication38\\har01'
}
}
getOhpmRemoteHspDependencyInfo5.6.2+
getOhpmRemoteHspDependencyInfo: (signed) => Record<string, OhpmDependencyInfo> | object
获取工程下oh-package.json5中配置的hsp包依赖信息。
参数值:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
signed | boolean | 否 | 是否获取签名的hsp包路径,默认为false |
返回值:
类型 | 说明 |
---|---|
Record<string, OhpmDependencyInfo> | object | 工程下oh-package.json5中配置的hsp包依赖信息 |
在工程级hvigorfile.ts中编写示例代码:
import { hvigor, HvigorNode, HvigorPlugin } from '@ohos/hvigor';
import { appTasks, OhosHapContext, OhosAppContext, OhosPluginId, Target } from '@ohos/hvigor-ohos-plugin';
// 实现自定义插件
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
context() {
return {
data: 'customPlugin xxx'
};
},
async apply(currentNode: HvigorNode): Promise<void> {
hvigor.nodesEvaluated(async () => {
// 注册模块级任务
hapTask(currentNode);
});
}
};
}
function hapTask(currentNode: HvigorNode) {
// 等待全部节点加载完成之后获取子节点信息
currentNode.subNodes((node: HvigorNode) => {
// 获取hap模块上下文信息
const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
const moduleName = hapContext?.getModuleName();
hapContext?.targets((target: Target) => {
const targetName = target.getTargetName();
node.registerTask({
// 任务名称
name: `${targetName}@getRemoteHspInfo`,
// 任务执行逻辑主体函数
run() {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
if (!rootNodeContext) {
return;
}
// 获取未签名的远程hsp相关信息
const remoteHspInfo = rootNodeContext.getOhpmRemoteHspDependencyInfo();
console.log(remoteHspInfo)
// 获取已签名的远程hsp相关信息
const signedRemoteHspInfo = rootNodeContext.getOhpmRemoteHspDependencyInfo(true);
console.log(signedRemoteHspInfo)
},
// 配置前置任务依赖
dependencies: [`${targetName}@PackageHap`],
// 配置任务的后置任务依赖
postDependencies: ['assembleHap']
});
});
});
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
};
执行hvigorw assembleHap输出示例:
// 工程下oh-package.json5中已配置hsp包依赖
// 获取无签名的hsp包路径
{
integrated_hsp1_100: {
name: 'integrated_hsp1_100',
version: '1.0.0',
dependencies: {},
packagePath: 'D:\\code\\testproject\\dependenices\\oh_modules\\.ohpm\\integrated_hsp1_100@1.0.0\\oh_modules\\integrated_hsp1_100',
remoteHspPath: 'D:\\code\\testproject\\dependenices\\build\\cache\\default\\integrated_hsp\\integrated_hsp1_100@1.0.0\\integrated_hsp1_100.hsp'
}
}
// 获取已签名的hsp包路径
{
integrated_hsp1_100: {
name: 'integrated_hsp1_100',
version: '1.0.0',
dependencies: {},
packagePath: 'D:\\code\\testproject\\dependenices\\oh_modules\\.ohpm\\integrated_hsp1_100@1.0.0\\oh_modules\\integrated_hsp1_100',
signedRemoteHspPath: 'D:\\code\\testproject\\dependenices\\build\\cache\\default\\remote_hsp\\integrated_hsp1_100@1.0.0\\integrated_hsp1_100-signed.hsp'
}
}
getDependenciesOpt5.0.10+
getDependenciesOpt: () => object
获取工程下oh-package.json5中配置的dependencies依赖。
返回值:
类型 | 说明 |
---|---|
object | 获取工程级别下oh-package.json5中dependencies信息 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const DependenciesInfo = rootNodeContext.getDependenciesOpt();
console.log(DependenciesInfo)
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
setDependenciesOpt5.0.10+
setDependenciesOpt: (:any) => any
设置工程下oh-package.json5中的dependencies依赖。
参数值:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
dependencies | any | 是 | 设置当前工程下oh-package.json5中dependencies依赖 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const dependenciesInfo = rootNodeContext.getDependenciesOpt()
dependenciesInfo["har"] = "./har";
rootNodeContext.setDependenciesOpt(dependenciesInfo);
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
getDevDependenciesOpt5.0.10+
getDevDependenciesOpt: () => object
获取工程下oh-package.json5中配置的devDependencies依赖。
返回值:
类型 | 说明 |
---|---|
object | 获取工程级别下oh-package.json5中devDependencies信息 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const devDependenciesInfo = rootNodeContext.getDevDependenciesOpt();
console.log(devDependenciesInfo)
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
setDevDependenciesOpt5.0.10+
setDevDependenciesOpt: (:any) => any
设置工程下oh-package.json5中的devDependencies依赖。
参数值:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
devDependencies | any | 是 | 设置当前工程下oh-package.json5中devdependencies依赖 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const devDependenciesInfo = rootNodeContext.getDevDependenciesOpt()
devDependenciesInfo["har"] = "./har";
rootNodeContext.setDevDependenciesOpt(devDependenciesInfo);
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
getDynamicDependenciesOpt5.0.10+
getDynamicDependenciesOpt: () => object
获取工程下oh-package.json5中配置的dynamicDependencies依赖。
返回值:
类型 | 说明 |
---|---|
object | 获取工程级别下oh-package.json5中DynamicDependencies信息 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const dynamicDependenciesInfo = rootNodeContext.getDynamicDependenciesOpt();
console.log(dynamicDependenciesInfo)
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
setDynamicDependenciesOpt5.0.10+
setDynamicDependenciesOpt: (:any) => any
设置工程下oh-package.json5中的dynamicDependencies依赖。
参数值:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
dynamicDependencies | any | 是 | 设置当前工程下oh-package.json5中dynamicDependencies依赖 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 自定义插件代码
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
async apply(currentNode: HvigorNode): Promise<void> {
const rootNodeContext = currentNode.getContext(OhosPluginId.OHOS_APP_PLUGIN);
if (!rootNodeContext) {
return;
}
const dynamicDependenciesInfo = rootNodeContext.getDynamicDependenciesOpt()
dynamicDependenciesInfo["har"] = "./har";
rootNodeContext.setDynamicDependenciesOpt(dynamicDependenciesInfo);
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
getOverrides5.10.3+
getOverrides:()=>object
获取工程下oh-package.json5中配置的overrides字段。
返回值:
类型 | 说明 |
---|---|
object | 获取工程下oh-package.json5中配置的overrides字段 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import {OhosAppContext, OhosHapContext, OhosPluginId} from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
hvigor.afterNodeEvaluate(node => {
const appContext = node.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
if (appContext) {
let dependency = appContext.getOverrides() ?? {};
console.log(dependency)
}
});
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
setOverrides5.10.3+
setOverrides:(overrides: any)=>void
设置工程下oh-package.json5中的overrides字段。
参数值:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
overrides | any | 是 | 设置工程下oh-package.json5中的overrides字段 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import {OhosAppContext, OhosHapContext, OhosPluginId} from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
hvigor.afterNodeEvaluate(node => {
const appContext = node.getContext(OhosPluginId.OHOS_APP_PLUGIN) as OhosAppContext;
if (appContext) {
let dependency = appContext.getOverrides() ?? {};
dependency['library'] = 'file:./library.har'; //在工程级oh-package.json5中动态添加工程内HAR包依赖
appContext.setOverrides(dependency);
return;
}
const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (hapContext) {
const dependency = hapContext.getDependenciesOpt();
dependency['library'] = 'file:./../library'; //在entry上动态添加工程内模块依赖
hapContext.setDependenciesOpt(dependency);
}
});
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
}
OhosHapContext
hap模块Plugin提供的上下文接口,在hap模块的hvigor节点中可通过getContext方法传入OhosPluginId.OHOS_HAP_PLUGIN_ID获取该接口,接口中主要包含了hap模块中module、target信息。
导入模块
import { OhosHapContext } from '@ohos/hvigor-ohos-plugin';
示例:获取hap模块上下文接口信息。
const hapContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
getModuleName
getModuleName: () => string
获取模块名称。
返回值:
类型 | 说明 |
---|---|
string | 模块名称 |
getModulePath
getModulePath: () => string
获取模块路径。
返回值:
类型 | 说明 |
---|---|
string | 模块路径 |
getModuleType
getModuleType: () => string
获取模块类型,取值来自模块配置文件module.json5中moduleTyp字段。
返回值:
类型 | 说明 |
---|---|
string | 模块类型 |
getBuildProductRootPath
getBuildProductRootPath: () => string
获取模块基于product构建根路径。
返回值:
类型 | 说明 |
---|---|
string | 模块基于product构建根路径 |
targets
targets: (callbackfn: (target: Target) => void) => void
当前需构建的target对象回调方法
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
callback | (target: Target) => void | 是 | 入参类型为Target,返回类型为void的函数 |
getModuleJsonOpt
getModuleJsonOpt: () => any
获取当前模块的module.json5文件中内容的obj对象。
返回值:
类型 | 说明 |
---|---|
any | 当前模块的module.json5文件中内容的obj对象 |
setModuleJsonOpt
setModuleJsonOpt: (moduleJsonOpt) => void
修改当前构建的module.json5文件中内容的obj对象。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
moduleJsonOpt | any | 是 | 设置当前模块的module.json5文件解析出来的obj对象 |
说明
setModuleJsonOpt会进行schema校验,如果传入的对象不符合校验规则则会抛出异常。
getBuildProfileOpt
getBuildProfileOpt: () => any
获取当前模块的build-profile.json5文件中内容的obj对象。
返回值:
类型 | 说明 |
---|---|
any | 当前模块的build-profile.json5文件中内容的obj对象 |
setBuildProfileOpt
setBuildProfileOpt: (buildProfileOpt) => any
设置当前模块的build-profile.json5文件中内容的obj对象。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
buildProfileOpt | any | 是 | 设置当前模块的build-profile.json5文件中内容的obj对象 |
说明
setBuildProfileOpt会进行schema校验,如果传入的对象不符合校验规则则会抛出异常。
getVersion
getVersion: () => string
获取模块oh-package.json5中配置的版本号。
返回值:
类型 | 说明 |
---|---|
string | 模块oh-package.json5中配置的版本号 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 实现自定义插件
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
context() {
return {
data: 'customPlugin xxx'
};
},
async apply(currentNode: HvigorNode): Promise<void> {
hvigor.nodesEvaluated(async () => {
currentNode.subNodes((node: HvigorNode) => {
// 获取hap模块上下文信息
// 如果是HAR或HSP,使用OHOS_HAR_PLUGIN/OHOS_HSP_PLUGIN
const hapNodeContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (!hapNodeContext) {
return;
}
const moduleVersion = hapNodeContext.getVersion();
console.log(moduleVersion);
});
});
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
执行hvigorw --sync输出示例:
1.1.0
setVersion
setVersion: (version: string) => void
修改模块oh-package.json5中的版本号。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
version | string | 是 | 修改模块oh-package.json5中的版本号 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 实现自定义插件
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
context() {
return {
data: 'customPlugin xxx'
};
},
async apply(currentNode: HvigorNode): Promise<void> {
hvigor.nodesEvaluated(async () => {
currentNode.subNodes((node: HvigorNode) => {
// 获取hap模块上下文信息
// 如果是HAR或HSP,使用OHOS_HAR_PLUGIN/OHOS_HSP_PLUGIN
const hapNodeContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (!hapNodeContext) {
return;
}
hapNodeContext.setVersion('2.0.0');
const moduleVersion = hapNodeContext.getVersion();
console.log(moduleVersion);
});
});
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
执行hvigorw --sync输出:
2.0.0
getOhpmDependencyInfo5.0.0+
getOhpmDependencyInfo: () => Record<string, OhpmDependencyInfo> | object
获取模块下oh-package.json5中配置的依赖信息。
返回值:
类型 | 说明 |
---|---|
Record<string, OhpmDependencyInfo> | object | oh-package.json5中配置的依赖信息 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 实现自定义插件
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
context() {
return {
data: 'customPlugin xxx'
};
},
async apply(currentNode: HvigorNode): Promise<void> {
hvigor.nodesEvaluated(async () => {
currentNode.subNodes((node: HvigorNode) => {
// 获取hap模块上下文信息
const hapNodeContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
if (!hapNodeContext) {
return;
}
const ohpmInfo = hapNodeContext.getOhpmDependencyInfo();
console.log(ohpmInfo)
});
});
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
执行hvigorw --sync输出示例:
// 模块下oh-package.json5中已配置dependencies依赖
{
har: {
name: 'har01',
version: '1.0.0',
dependencies: {},
packagePath: 'D:\\project\\deveco_project\\MyApplication38\\har01'
}
}
getOhpmRemoteHspDependencyInfo5.6.2+
getOhpmRemoteHspDependencyInfo: (signed) => Record<string, OhpmDependencyInfo> | object
获取模块下oh-package.json5中配置的hsp包依赖信息。
参数值:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
signed | boolean | 否 | 是否获取签名的hsp包路径,默认为false |
返回值:
类型 | 说明 |
---|---|
Record<string, OhpmDependencyInfo> | object | 模块下oh-package.json5中配置的hsp包依赖信息 |
在工程级hvigorfile.ts中编写示例代码:
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { hvigor } from '@ohos/hvigor';
import { OhosPluginId } from '@ohos/hvigor-ohos-plugin';
// 实现自定义插件
export function customPlugin(): HvigorPlugin {
return {
pluginId: 'customPlugin',
context() {
return {
data: 'customPlugin xxx'
};
},
async apply(currentNode: HvigorNode): Promise<void> {
hvigor.nodesEvaluated(async () => {
currentNode.subNodes((node: HvigorNode) => {
// 获取hap模块上下文信息
const hapNodeContext = node.getContext(OhosPluginId.OHOS_HAP_PLUGIN) as OhosHapContext;
hapNodeContext?.targets((target: Target) => {
const targetName = target.getTargetName();
node.registerTask({
// 任务名称
name: `${targetName}@getRemoteHspInfo`,
// 任务执行逻辑主体函数
run() {
// 获取未签名的远程hsp相关信息
const remoteHspInfo = hapNodeContext.getOhpmRemoteHspDependencyInfo();
console.log(remoteHspInfo);
// 获取已签名的远程hsp相关信息
const signedRemoteHspInfo = hapNodeContext.getOhpmRemoteHspDependencyInfo(true);
console.log(signedRemoteHspInfo);
},
// 配置前置任务依赖
dependencies: [`${targetName}@PackageHap`],
// 配置任务的后置任务依赖
postDependencies: ['assembleHap']
});
});
});
});
}
};
}
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins:[customPlugin()] /* Custom plugin to extend the functionality of Hvigor. */
}
执行hvigorw assembleHap输出示例:
// 模块下oh-package.json5中已配置hsp包依赖
// 获取无签名的hsp包路径
{
integrated_hsp1_100: {
name: 'integrated_hsp1_100',
version: '1.0.0',
dependencies: {},
packagePath: 'D:\\code\\testproject\\dependenices\\oh_modules\\.ohpm\\integrated_hsp1_100@1.0.0\\oh_modules\\integrated_hsp1_100',
remoteHspPath: 'D:\\code\\testproject\\dependenices\\build\\cache\\default\\integrated_hsp\\integrated_hsp1_100@1.0.0\\integrated_hsp1_100.hsp'
}
}
// 获取已签名的hsp包路径
{
integrated_hsp1_100: {
name: 'integrated_hsp1_100',
version: '1.0.0',
dependencies: {},
packagePath: 'D:\\code\\testproject\\dependenices\\oh_modules\\.ohpm\\integrated_hsp1_100@1.0.0\\oh_modules\\integrated_hsp1_100',
signedRemoteHspPath: 'D:\\code\\testproject\\dependenices\\build\\cache\\default\\remote_hsp\\integrated_hsp1_100@1.0.0\\integrated_hsp1_100-signed.hsp'
}
}