/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { exec } from 'child_process'; import { HvigorNode } from '@ohos/hvigor'; import { PBConfig } from './PBConfig'; import { Config } from '../constants/TypesConfig'; import { Target } from '@ohos/hvigor-ohos-plugin'; import { Logger } from '../core/logger/Logger'; import path from 'path'; let totalExecutionTime: number = 0; class PBExecuteController { public execute(subnode: HvigorNode, options: Config): void { // 获取上下文信息 const hapContext = subnode.getContext(subnode.getAllPluginIds()[0]); hapContext?.targets((target: Target) => { this.registerProtoTask(subnode, target, options); }); } /** * 注册Protobuf生成任务 */ private registerProtoTask(subnode: HvigorNode, target: Target, options: Config): void { const targetName = target.getTargetName(); subnode.registerTask({ name: targetName, run: async () => { await this.generateProtoFiles(subnode, options); }, postDependencies: [`${targetName}@PreBuild`], }); } /** * 生成Protobuf文件 */ private async generateProtoFiles(subnode: HvigorNode, options: Config): Promise { const config = this.prepareConfig(options); const cmdStr = config.getExeParaStr(subnode); if (cmdStr === '') { return; } Logger.info(cmdStr); try { const startTime = Date.now(); const message = await this.executeCommand(cmdStr); const executionTime = Date.now() - startTime; totalExecutionTime += executionTime; this.logExecutionResult(config, subnode, executionTime, message); } catch (error) { throw error; } } /** * 准备配置 */ private prepareConfig(options: Config): PBConfig { const config: PBConfig = new PBConfig(); config.setParas(options); config.parseParas(); return config; } /** * 执行命令 */ private async executeCommand(cmdStr: string): Promise { return new Promise((resolve, reject) => { exec(cmdStr, { encoding: 'utf8' }, (error, stdout, stderr) => { if (error) { const formattedError = this.formatCommandError(stderr); return reject(new Error(formattedError)); } resolve(stderr); }); }); } /** * 格式化命令错误 */ private formatCommandError(stderr: string): string { if (!/Error\s*600\d/.test(stderr)) { return 'Error Code 6020: ' + stderr; } return stderr; } /** * 记录执行结果 */ private logExecutionResult(config: PBConfig, subnode: HvigorNode, executionTime: number, message: string): void { if (config.debug) { Logger.info(message); } // 记录生成的文件 config.protoFiles.forEach((file: string) => { Logger.info(`Generating file: ${file}... done`); }); // 记录输出目录和执行时间 const outputDir = path.join(subnode.getNodePath(), config.saveDir); Logger.info(`Output directory: ${outputDir}`); Logger.info(`Generated ${config.protoFiles.length} files successfully in ${executionTime}ms`); } } export default new PBExecuteController();