UNPKG

@alvin917/jsbridge-plugin

Version:
166 lines (165 loc) 7.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BridgePluginHapHandler = void 0; const hvigor_1 = require("@ohos/hvigor"); const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin"); const BridgeConstant_1 = require("./BridgeConstant"); const BridgePluginHandler_1 = require("./BridgePluginHandler"); const analyzer_plugin_1 = require("@alvin917/analyzer-plugin"); class BridgePluginHapHandler extends BridgePluginHandler_1.BridgePluginHandler { constructor() { super(...arguments); this.bundleName = ""; } handle() { super.handle(); this.context.targets((target) => { this.bundleName = target.getCurrentProduct().getBundleName(); const targetName = target.getTargetName(); this.node.registerTask({ name: `${targetName}@${BridgeConstant_1.BridgeConstant.BRIDGE_PROCESS_TASK_NAME}`, run: () => { this.processBridgeMap(); }, dependencies: [`${targetName}@${BridgeConstant_1.BridgeConstant.BRIDGE_GEN_TASK_NAME}`], postDependencies: [`${targetName}@MergeProfile`] }); }); } processBridgeMap() { const bridgeMap = []; this.processSelfBridgeMap(bridgeMap); this.processDependenciesBridgeMap(this.node.getParentNode(), bridgeMap); this.processOhpmDependenciesBridgeMap(bridgeMap); this.checkBridgeMapObj(bridgeMap); this.generateFinalBridgeMap(bridgeMap); this.appendMetadataToabilitiy(); } processSelfBridgeMap(bridgeMap) { let mapObj = this.getBridgeMapObjList(this.context.getModuleJsonOpt(), this.context.getModulePath()); bridgeMap.push(...mapObj); } processDependenciesBridgeMap(node, bridgeMap) { if (!node) return; node?.subNodes((childNode) => { if (childNode.getNodeName() != this.node.getNodeName()) { let subNodeContext = childNode.getContext(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAR_PLUGIN); if (!subNodeContext) { subNodeContext = childNode.getContext(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN); } if (subNodeContext) { let mapObj = this.getBridgeMapObjList(subNodeContext.getModuleJsonOpt(), subNodeContext.getModulePath()); bridgeMap.push(...mapObj); } this.processDependenciesBridgeMap(childNode, bridgeMap); } }); } processOhpmDependenciesBridgeMap(bridgeMap) { const ohpmDependencyInfo = this.context.getOhpmDependencyInfo(); for (const key in ohpmDependencyInfo) { const depInfo = ohpmDependencyInfo[key]; const modulePath = depInfo.packagePath; const moduleObj = this.getModuleObjByOhpmDependency(modulePath); let mapObj = this.getBridgeMapObjList(moduleObj, modulePath); bridgeMap.push(...mapObj); } } getBridgeMapObjList(moduleObj, modulePath) { const bridgeMapPath = this.getBridgeMapJsonPath(moduleObj, modulePath); if (!bridgeMapPath || !hvigor_1.FileUtil.exist(bridgeMapPath)) return []; const bridgeMapJson5 = hvigor_1.FileUtil.readJson5(bridgeMapPath); return bridgeMapJson5?.bridgeMap?.map((item) => Object.assign(item, { modulePath, moduleName: this.context.getModuleName(), bundleName: this.bundleName })) ?? []; } getBridgeMapJsonPath(moduleObj, modulePath) { const brdigeMapName = this.getBridgeMapJsonFileName(moduleObj); if (brdigeMapName) { return this.getResourceBaseProfilePath(modulePath, `${brdigeMapName}.json`); } } getBridgeMapJsonFileName(moduleObj) { if (!moduleObj) return; let brdigeMapName; moduleObj.module.metadata?.forEach((metadata) => { if (metadata.name === BridgeConstant_1.BridgeConstant.BRIDGE_MAP_METADATA_NAME) { brdigeMapName = metadata.resource; } }); if (brdigeMapName) { brdigeMapName = this.parsingProfileName(brdigeMapName); } return brdigeMapName; } getResourceBaseProfilePath(modulePath, relativePath) { const bridgeMapPath = hvigor_1.FileUtil.pathResolve(modulePath, analyzer_plugin_1.Constants.SRC, analyzer_plugin_1.Constants.MAIN, analyzer_plugin_1.Constants.RESOURCES_DIR, analyzer_plugin_1.Constants.BASE_DIR, analyzer_plugin_1.Constants.PROFILE_DIR, relativePath); if (hvigor_1.FileUtil.exist(bridgeMapPath)) { return bridgeMapPath; } } getModuleObjByOhpmDependency(modulePath) { const mainPath = hvigor_1.FileUtil.pathResolve(modulePath, analyzer_plugin_1.Constants.SRC, analyzer_plugin_1.Constants.MAIN); const moduleJsonPath = hvigor_1.FileUtil.pathResolve(mainPath, analyzer_plugin_1.Constants.MODULE_JSON); const moduleJson5Path = hvigor_1.FileUtil.pathResolve(mainPath, analyzer_plugin_1.Constants.MODULE_JSON5); if (hvigor_1.FileUtil.exist(moduleJsonPath)) { return hvigor_1.FileUtil.readJson5(moduleJsonPath); } else if (hvigor_1.FileUtil.exist(moduleJson5Path)) { return hvigor_1.FileUtil.readJson5(moduleJson5Path); } } checkBridgeMapObj(bridgeMap) { const handlerMap = new Map(); bridgeMap.forEach((item) => { if (handlerMap.has(item.alias)) { throw new Error(`Bridge api: [${item.name} ${item.alias}] 与 [${handlerMap.get(item.alias)} ${item.alias}] 重复`); } else { handlerMap.set(item.alias, item.name); } }); } generateFinalBridgeMap(bridgeMap) { let bridgeInfos = { bridgeMap: bridgeMap.map((item) => { delete item.modulePath; return item; }) }; const bridgeMapJsonStr = JSON.stringify(bridgeInfos, null, 2); const bridgeMapFilePath = this.config.getProfileFilePath(BridgeConstant_1.BridgeConstant.BRIDGE_MAP_NAME); hvigor_1.FileUtil.ensureFileSync(bridgeMapFilePath); hvigor_1.FileUtil.writeFileSync(bridgeMapFilePath, bridgeMapJsonStr); } parsingProfileName(profileName) { if (/^[$]profile:[0-9a-zA-Z_.]+|(?=.*[{])(?=.*[}])[0-9a-zA-Z_.{}]+$/.test(profileName)) return profileName.split(":")[1]; } appendMetadataToabilitiy() { const moduleJsonOpt = this.context.getModuleJsonOpt(); let mainElement = moduleJsonOpt.module.mainElement; let abilities = moduleJsonOpt.module.abilities; if (!mainElement || !abilities || abilities.length == 0) return; abilities.forEach((ability, index, abilityesRef) => { if (ability.name === mainElement) { let abilityRef = abilityesRef[index]; let bridgeMetadata = { name: BridgeConstant_1.BridgeConstant.BRIDGE_MAP_METADATA_NAME, value: "bridge_map.json5 配置信息", resource: BridgeConstant_1.BridgeConstant.BRIDGE_MAP_METADATA_RESOURCE }; let originalMetadata = abilityRef['metadata'] ?? []; Reflect.set(abilityRef, 'metadata', [bridgeMetadata, ...originalMetadata]); } }); this.context.setModuleJsonOpt(moduleJsonOpt); } } exports.BridgePluginHapHandler = BridgePluginHapHandler;