1/* 2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16import * as path from 'path'; 17import * as fs from 'fs'; 18import { DirTemp, HdfRootInfo } from "./datatype"; 19import { hdf_version_map } from '../template/hdf/hdfdir'; 20import { genIdlFile } from './tools/genidlfile'; 21import { genServiceHFile } from './tools/genservicehfile'; 22import { genServiceCppFile } from './tools/genservicecppfile'; 23import { genHdfCommonFile } from './tools/gencommonfile'; 24import { Logger } from '../common/log'; 25const fileHandlers: { [key: string]: Function } = { 26 'I[marcoName]Interface.idl': genIdlFile, 27 '[driverName]_interface_service.h': genServiceHFile, 28 '[driverName]_interface_service.cpp': genServiceCppFile, 29 '[driverName]_interface_driver.cpp': genHdfCommonFile, 30 'BUILD.gn': genHdfCommonFile, 31 'bundle.json': genHdfCommonFile, 32 'hello_dump.h': genHdfCommonFile, 33 'hello_dump.c': genHdfCommonFile, 34 'device_info.hcs': genHdfCommonFile, 35 'readme.md': genHdfCommonFile, 36}; 37 38// 循环写入文件, 并将funcContent的内容写入模板 39export function genDir(dirItem: DirTemp, rootInfo: HdfRootInfo, out: string) { 40 let dirPath = path.join(out, dirItem.name.replace('[driverName]', rootInfo.driverName)); 41 // 创建目录 42 if (!fs.existsSync(dirPath)) { 43 fs.mkdirSync(dirPath, { recursive: true }); 44 } 45 // 生成当前目录文件 46 dirItem.files.forEach(file => { 47 let fileName = file.name.replace('[driverName]', rootInfo.driverName); 48 let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); 49 let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); 50 fileName = fileName.replace('[marcoName]', marcoName); 51 let filePath = path.join(dirPath, fileName); 52 if (!fs.existsSync(filePath)) { 53 const handler = fileHandlers[file.name]; 54 if (handler) { 55 // 调用对应的生成文件方法 56 handler(rootInfo, filePath, file.content); 57 } 58 } 59 }) 60 // 遍历生成子目录文件 61 dirItem.dirs.forEach(subDir => { 62 genDir(subDir, rootInfo, dirPath); 63 }) 64} 65 66export function genHdfFile(rootInfo: HdfRootInfo, out: string) { 67 Logger.getInstance().info("rootInfo: " + JSON.stringify(rootInfo)) 68 69 let dirContentTemplete = hdf_version_map.get(rootInfo.versionTag); 70 if (dirContentTemplete !== undefined) { 71 genDir(dirContentTemplete, rootInfo, out); 72 } 73 74 Logger.getInstance().info('generate success!') 75}