/* * 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 { Config } from './TypesConfig'; import path from 'path'; import fs from 'fs'; import os from 'os'; import { Logger } from '../core/logger/Logger'; const ANY_PROTO_FILENAME = 'any.proto'; const cacheDir = path.join('.hvigor/cache/protoc-gen-ArkTS'); const includeDir = path.join(cacheDir, 'include/google/protobuf'); // 确保目标目录存在 if (!fs.existsSync(cacheDir)) { fs.mkdirSync(cacheDir, { recursive: true }); } if (!fs.existsSync(includeDir)) { fs.mkdirSync(includeDir, { recursive: true }); } // 根据平台选择子包 let platformPkgName: string; switch (os.platform()) { case 'win32': platformPkgName = '@hadss/turbo-trans-protobuf-plugin-windows'; break; case 'linux': platformPkgName = '@hadss/turbo-trans-protobuf-plugin-linux'; break; case 'darwin': if (os.arch() === 'arm64') { platformPkgName = '@hadss/turbo-trans-protobuf-plugin-darwin-arm64'; } else if (os.arch() === 'x64') { platformPkgName = '@hadss/turbo-trans-protobuf-plugin-darwin-x64'; } else { throw new Error(`Unsupported macOS architecture: ${os.arch()}`); } break; default: throw new Error(`Unsupported platform: ${os.platform()}`); } // 定位子包真实路径(包含 @hadss 层级) // const platformPkgRoot = path.dirname(require.resolve(`${platformPkgName}`)); let protocPath = ''; let pluginPath = ''; try { const platformPkgRoot = getOptionalPkgRoot(`${platformPkgName}`); Logger.info(`platformPkgRoot is ${platformPkgRoot}`); const binDir = path.join(platformPkgRoot, 'bin'); // 构造可执行文件路径 const exeExtension = os.platform() === 'win32' ? '.exe' : ''; protocPath = path.join(binDir, `protoc${exeExtension}`); pluginPath = path.join(binDir, `protoc-gen-ArkTS${exeExtension}`); // 校验文件是否存在 [protocPath, pluginPath].forEach((p) => { if (!fs.existsSync(p)) { throw new Error(`File not found: ${p}`); } }); // 拷贝可执行文件到 hvigor cache fs.copyFileSync(protocPath, path.join(cacheDir, path.basename(protocPath))); fs.copyFileSync(pluginPath, path.join(cacheDir, path.basename(pluginPath))); } catch (e2) { Logger.error(`copy error`); } // 拷贝 include 文件 const mainPkgRoot = path.dirname(require.resolve('@hadss/turbo-trans-protobuf-plugin/package.json')); const srcDir = path.join(mainPkgRoot, 'include/google/protobuf'); fs.copyFileSync(path.join(srcDir, ANY_PROTO_FILENAME), path.join(includeDir, ANY_PROTO_FILENAME)); // 输出 PathConstants export const PathConstants: Config = { exePath: path.join(cacheDir, path.basename(protocPath)), pluginPath: path.join(cacheDir, path.basename(pluginPath)), saveDir: 'src/generated/ArkTS', namePattern: 'DefaultPattern', sendable: false, debug: false, enableFunctions: [], scanDir: ['src/main'], srcDir: path.join(cacheDir, 'include'), ignoreModuleNames: [], }; function getOptionalPkgRoot(pkgName:string) { try { return path.dirname(require.resolve(`${pkgName}/package.json`)); } catch (e1) { try { const main = require.resolve(pkgName); const root = path.join(path.dirname(main), '..'); const pkgJson = path.join(root, 'package.json'); if (fs.existsSync(pkgJson)) { return root; } } catch (e2) { return ''; } console.warn(`Optional dependency ${pkgName} not found or invalid.`); return ''; } }