1 2/* 3* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd. 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/ 16import { GenInfo } from "../datatype"; 17import { classMethodDeclareTemplate, classNapiHTemplate, napiFuncHTemplate } from "../../template/func_template"; 18import { replaceAll } from "../../common/tool"; 19import * as path from 'path'; 20import * as fs from 'fs'; 21import { genClsVariableDeclare, genStructVariableDeclare } from "./gencommonfunc"; 22 23export function doGenNapiHFile(rootInfo: GenInfo, fileContent: string) { 24 let napiHContent = ''; 25 26 // 实现class的 27 if (rootInfo.parseObj && rootInfo.parseObj.classes) { 28 let classesDeclare = ''; 29 rootInfo.parseObj.classes.forEach(cls => { 30 let clsDeclare = replaceAll(classNapiHTemplate, '[class_name_replace]', cls.name) 31 // 每个成员方法的声明 32 let clsMethodDeclare = ''; 33 for (let i = 0; i < cls.functionList.length; ++i) { 34 clsMethodDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', cls.functionList[i].name); 35 } 36 clsDeclare = replaceAll(clsDeclare, '[class_method_declare]', clsMethodDeclare); 37 // 每个成员变量的get set方法的声明 38 let { clsVariableDeclare, clsVariableGetSetDeclare } = genClsVariableDeclare(cls); 39 clsDeclare = replaceAll(clsDeclare, '[class_variable_declare]', clsVariableDeclare); 40 clsDeclare = replaceAll(clsDeclare, '[class_property_get_set_declare]', clsVariableGetSetDeclare); 41 // 所有class的napi声明 42 classesDeclare += clsDeclare; 43 }); 44 napiHContent += classesDeclare; 45 } 46 47 // 实现struct的 48 if (rootInfo.parseObj && rootInfo.parseObj.structs) { 49 let structDeclare = ''; 50 rootInfo.parseObj.structs.forEach(struct => { 51 let clsDeclare = replaceAll(classNapiHTemplate, '[class_name_replace]', struct.name) 52 // 每个成员方法的声明 53 let clsMethodDeclare = ''; 54 for (let i = 0; i < struct.functions.length; ++i) { 55 clsMethodDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', struct.functions[i].name); 56 } 57 clsDeclare = replaceAll(clsDeclare, '[class_method_declare]', clsMethodDeclare); 58 // 每个成员变量的get set方法的声明 59 let { clsVariableDeclare, clsVariableGetSetDeclare } = genStructVariableDeclare(struct); 60 clsDeclare = replaceAll(clsDeclare, '[class_variable_declare]', clsVariableDeclare); 61 clsDeclare = replaceAll(clsDeclare, '[class_property_get_set_declare]', clsVariableGetSetDeclare); 62 // 所有struct的napi声明 63 structDeclare += clsDeclare; 64 }); 65 napiHContent += structDeclare; 66 } 67 68 // 实现function的 69 if (rootInfo.parseObj && rootInfo.parseObj.funcs) { 70 rootInfo.parseObj.funcs.forEach(func => { 71 let funcParams = ''; 72 for (let i = 0; i < func.parameters.length; ++i) { 73 funcParams += i > 0 ? ', ' : ''; 74 funcParams += func.parameters[i].name + ': ' + func.parameters[i].type; 75 } 76 let rawFileName = path.basename(rootInfo.rawFilePath); 77 let hContent = replaceAll(napiFuncHTemplate, '[file_introduce_replace]', rawFileName); 78 hContent = replaceAll(hContent, '[input_introduce_replace]', funcParams === '' ? 'void' : funcParams); 79 hContent = replaceAll(hContent, '[func_name_replace]', func.name); 80 hContent = replaceAll(hContent, '[func_param_replace]', funcParams); 81 hContent = replaceAll(hContent, '[func_return_replace]', 82 func.returns === '' ? 'void' : func.returns); 83 napiHContent += hContent; 84 }); 85 } 86 let upperFileName = rootInfo.fileName.toLocaleUpperCase(); 87 fileContent = replaceAll(fileContent, '[fileName]', rootInfo.fileName); 88 fileContent = replaceAll(fileContent, '[upper_filename]', upperFileName); 89 fileContent = replaceAll(fileContent, '[func_declare_replace]', napiHContent); 90 return fileContent; 91} 92 93// 生成napi.h文件 94export function genNapiHFile(rootInfo: GenInfo, filePath: string, 95 fileContent: string) { 96 fileContent = doGenNapiHFile(rootInfo, fileContent); 97 fs.writeFileSync(filePath, fileContent); 98}