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 { classInitTemplate, classMethodDeclareTemplate, napiFuncInitTemplate } from "../../template/func_template"; 18import { replaceAll } from "../../common/tool"; 19import * as fs from 'fs'; 20import {genClsVariableDeclare, genStructVariableDeclare} from "./gencommonfunc"; 21 22export function doGenNapiInitFile(rootInfo: GenInfo, fileContent: string) { 23 let napiInitContent = ''; 24 25 // class 的声明 26 let classesInit = ''; 27 if (rootInfo.parseObj && rootInfo.parseObj.classes) { 28 let classInit = ''; 29 rootInfo.parseObj.classes.forEach(classObj => { 30 classInit = replaceAll(classInitTemplate, '[class_name_replace]', classObj.name); 31 let clsMethodDeclare = ''; 32 for (let i = 0; i < classObj.functionList.length; ++i) { 33 clsMethodDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', classObj.functionList[i].name); 34 } 35 classInit = replaceAll(classInit, '[class_method_replace]', clsMethodDeclare); 36 let { clsVariableDeclare, clsVariableGetSetDeclare } = genClsVariableDeclare(classObj); 37 classInit = replaceAll(classInit, '[class_property_replace]', clsVariableGetSetDeclare); 38 classesInit += classInit; 39 }); 40 } 41 42 // struct的声明 43 let structsInit = ''; 44 if (rootInfo.parseObj && rootInfo.parseObj.structs) { 45 let structInit = ''; 46 rootInfo.parseObj.structs.forEach(classObj => { 47 structInit = replaceAll(classInitTemplate, '[class_name_replace]', classObj.name); 48 let clsMethodDeclare = ''; 49 for (let i = 0; i < classObj.functions.length; ++i) { 50 clsMethodDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', classObj.functions[i].name); 51 } 52 structInit = replaceAll(structInit, '[class_method_replace]', clsMethodDeclare); 53 let { clsVariableDeclare, clsVariableGetSetDeclare } = genStructVariableDeclare(classObj); 54 structInit = replaceAll(structInit, '[class_property_replace]', clsVariableGetSetDeclare); 55 structsInit += structInit; 56 }); 57 } 58 59 if (rootInfo.parseObj && rootInfo.parseObj.funcs) { 60 rootInfo.parseObj.funcs.forEach(func => { 61 let funcName = func.name; 62 napiInitContent += replaceAll(napiFuncInitTemplate, 63 '[func_name_replace]', funcName); 64 }); 65 } 66 // 写文件 67 fileContent = replaceAll(fileContent, '[fileName]', rootInfo.fileName); 68 fileContent = replaceAll(fileContent, '[init_replace]', napiInitContent); 69 // class 的init 如果class是空的这里会不会替换出错? 后面再测试 70 fileContent = replaceAll(fileContent, '[class_init_replace]', classesInit); 71 fileContent = replaceAll(fileContent, '[struct_init_replace]', structsInit); 72 return fileContent; 73} 74 75// 生成napiinit.cpp 76export function genInitCppFile(rootInfo: GenInfo, filePath: string, 77 fileContent: string) { 78 fileContent = doGenNapiInitFile(rootInfo, fileContent); 79 fs.writeFileSync(filePath, fileContent); 80} 81