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