1/* 2* Copyright (c) 2022 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*/ 15const { analyzeFile } = require("./analyze"); 16const { generateAll } = require("./generate"); 17const { NapiLog } = require("./tools/NapiLog"); 18const re = require("./tools/re"); 19const { print } = require("./tools/tool"); 20var fs = require('fs'); 21 22function doGenerate(ifname, destdir, imports,numberType) { 23 let structOfTs = analyzeFile(ifname); 24 let fn = re.getFileInPath(ifname); 25 let tt = re.match('@ohos.([.a-z_A-Z0-9]+).d.ts', fn); 26 if (tt) { 27 let moduleName = re.getReg(fn, tt.regs[1]); 28 let importsStr = '' + imports 29 if (importsStr == 'true') { 30 importsFun(structOfTs.imports, destdir, ifname); 31 } else { 32 structOfTs.imports = []; 33 } 34 generateAll(structOfTs, destdir, moduleName, numberType); 35 } else { 36 NapiLog.logError('file name ' + fn + ' format invalid in function of doGenerate!'); 37 } 38 return structOfTs.declareNamespace[0].name 39} 40 41function importsFun(imports, destDir, ifname) { 42 for (let i = 0; i < imports.length; i++) { 43 let importSearch = re.search("([.,/a-zA-Z {}']+from)", imports[i]) 44 let importPath = re.removeReg(imports[i], importSearch.regs[0]) 45 importPath = importPath.replace 46 (/[`:~!#$%^&*() \+ =<>?"{}|, ;' [ \] ·~!#¥%……&*()—— \+ ={}|《》?:“”【】、;‘’,。、]/g,'') 47 importPath = importPath.split('/') 48 49 let ifnameSearch = re.search("(@[./a-zA-Z]+d.ts)", ifname) 50 let ifnamePath = re.removeReg(ifname, ifnameSearch.regs[0]) 51 let filePath = ifnamePath+importPath[importPath.length-1]+'.d.ts' 52 53 let ifnameFile = fs.readFileSync(ifname,'utf-8') 54 let importFile 55 try { 56 importFile = fs.readFileSync(ifnamePath+importPath[importPath.length-1]+'.d.ts','utf-8') 57 } catch (err) { 58 imports[i] = '' 59 return 60 } 61 62 if (ifnameFile == importFile) { 63 return 64 } else { 65 try { 66 fs.accessSync(destDir+'/'+importPath[importPath.length-1], fs.constants.R_OK | fs.constants.W_OK); 67 } catch (err) { 68 fs.mkdirSync(destDir+'/'+importPath[importPath.length-1]); 69 } 70 imports[i] = '#include '+'"'+importPath[importPath.length-1]+'/'+ 71 doGenerate(filePath,destDir+'/'+importPath[importPath.length-1])+'.h"\n' 72 } 73 } 74} 75 76module.exports = { 77 doGenerate 78} 79