• 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 xlsx = require('xlsx');
16const fs = require('fs');
17const path = require('path');
18const stdio = require('stdio');
19
20let ops = stdio.getopt({
21    'directory': { key: 'd', args: 1, description: 'scan directory' },
22    'output': { key: 'o', args: 1, description: 'output directory', default: '.' },
23});
24
25let distDir = ops.directory;
26let outDir = ops.output;
27
28function union(a, b) {
29    let ret = new Set();
30    a.forEach((n1) => {
31        ret.add(n1);
32    });
33    b.forEach((n1) => {
34        ret.add(n1);
35    });
36    return ret;
37}
38
39function intersection(a, b) {
40    let ret = new Set();
41    a.forEach((n1) => {
42        if (b.has(n1)) {
43            ret.add(n1);
44        }
45    });
46    return ret;
47}
48
49function collectFromSheet(sheet) {
50    let row = 2;
51    let collectInclude = new Set();
52    let collectFunction = new Set();
53    while (true) {
54        let idx = 'B' + row;
55        if (!(idx in sheet)) {
56            break;
57        }
58        collectInclude.add(sheet[idx].w);
59
60        if (sheet['C' + row].w === 'Function') {
61            collectFunction.add(sheet['D' + row].w);
62        }
63        row += 1;
64    }
65    return [collectInclude, collectFunction];
66}
67let wb = xlsx.readFile(path.join(__dirname, 'Andr_N_Games_api.xlsx'));
68
69let c1 = collectFromSheet(wb.Sheets.Games);
70let c2 = collectFromSheet(wb.Sheets.N);
71
72let androidFeature = {
73    include: union(c1[0], c2[0]),
74    function: union(c1[1], c2[1]),
75};
76//////////////////////////////////////////////////////
77
78let projectFiles = {
79    c: [],
80    cpp: [],
81    h: [],
82};
83
84let projectFeature = {
85    'function': new Set(),
86    'include': new Set()
87};
88
89function collectFromFile(fn) {
90    let data = fs.readFileSync(fn, { encoding: 'utf8' });
91
92    for (let ss of data.matchAll(/([A-Za-z_][A-Za-z0-9_]*) *\(/g)) {
93        projectFeature['function'].add(ss[1]);
94    }
95
96    for (let ss of data.matchAll(/# *include *(<|") *([A-Za-z_][A-Za-z0-9_\-/]*(.h(pp)*)*) *(>|")/g)) {
97        let s = ss[2].split('/');
98        s = s[s.length - 1];
99        projectFeature['include'].add(s);
100    }
101}
102
103function collectFile(pth, f) {
104    let fn = path.join(pth, f);
105    let st;
106    try {
107        st = fs.statSync(fn);
108    } catch (err) {
109        return;
110    }
111    if (st.isDirectory()) {
112        collectFeature(fn);
113    }
114    else if (st.isFile()) {
115        if (f.endsWith('.h')) {
116            projectFiles.h.push(fn);
117            collectFromFile(fn);
118        }
119        else if (f.endsWith('.cpp')) {
120            projectFiles.cpp.push(fn);
121            collectFromFile(fn);
122        }
123        else if (f.endsWith('.c')) {
124            projectFiles.c.push(fn);
125            collectFromFile(fn);
126        }
127    }
128}
129
130function collectFeature(pth) {
131    let files = fs.readdirSync(pth);
132
133    for (let f of files) {
134        collectFile(pth, f);
135    }
136}
137
138collectFeature(distDir);
139
140let result = {
141    function: intersection(androidFeature.function, projectFeature.function),
142    include: intersection(androidFeature.include, projectFeature.include),
143};
144console.log(result);
145///////////////////////////////save to excel
146function string2u8buff(s) {
147    let buf = new ArrayBuffer(s.length);
148    let view = new Uint8Array(buf);
149    for (let i = 0; i !== s.length; ++i) {
150        view[i] = s.charCodeAt(i) & 0xff;
151    }
152    return view;
153}
154
155let wopts = {
156    bookType: 'xlsx',
157    bookSST: false,
158    type: 'binary'
159};
160let workbook = xlsx.utils.book_new();
161
162let s1 = [];
163for (let f of result.function) {
164    s1.push({ function: f });
165}
166let sheet1 = xlsx.utils.json_to_sheet(s1);
167xlsx.utils.book_append_sheet(workbook, sheet1, 'sheet1');
168
169let s2 = [];
170for (let f of result.include) {
171    s2.push({ include: f });
172}
173let sheet2 = xlsx.utils.json_to_sheet(s2);
174xlsx.utils.book_append_sheet(workbook, sheet2, 'sheet2');
175
176let wbout = xlsx.write(workbook, wopts);
177let ddd = string2u8buff(wbout);
178let outPath = path.join(outDir, 'result.xlsx');
179console.log('output:', outPath);
180fs.writeFileSync(outPath, ddd);
181