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("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 collectFeature(pth) { 104 let files = fs.readdirSync(pth); 105 106 for (let f of files) { 107 let fn = path.join(pth, f); 108 let st = fs.statSync(fn); 109 if (st.isDirectory()) { 110 collectFeature(fn); 111 } 112 else if (st.isFile()) { 113 if (f.endsWith(".h")) { 114 projectFiles.h.push(fn); 115 collectFromFile(fn); 116 } 117 else if (f.endsWith(".cpp")) { 118 projectFiles.cpp.push(fn); 119 collectFromFile(fn); 120 } 121 else if (f.endsWith(".c")) { 122 projectFiles.c.push(fn); 123 collectFromFile(fn); 124 } 125 } 126 } 127} 128 129collectFeature(distDir) 130 131let result = { 132 function: intersection(androidFeature["function"], projectFeature["function"]), 133 include: intersection(androidFeature["include"], projectFeature["include"]), 134} 135console.log(result); 136///////////////////////////////save to excel 137function string2u8buff(s) { 138 let buf = new ArrayBuffer(s.length); 139 let view = new Uint8Array(buf); 140 for (let i = 0; i !== s.length; ++i) { 141 view[i] = s.charCodeAt(i) & 0xff; 142 } 143 return view; 144} 145 146let wopts = { 147 bookType: "xlsx", 148 bookSST: false, 149 type: 'binary' 150}; 151let workbook = xlsx.utils.book_new(); 152 153let s1 = [] 154for (let f of result.function) { 155 s1.push({ function: f }) 156} 157let sheet1 = xlsx.utils.json_to_sheet(s1); 158xlsx.utils.book_append_sheet(workbook, sheet1, 'sheet1'); 159 160let s2 = [] 161for (let f of result.include) { 162 s2.push({ include: f }) 163} 164let sheet2 = xlsx.utils.json_to_sheet(s2); 165xlsx.utils.book_append_sheet(workbook, sheet2, 'sheet2'); 166 167let wbout = xlsx.write(workbook, wopts); 168let ddd = string2u8buff(wbout); 169let outPath = path.join(outDir, "result.xlsx"); 170console.log("output:", outPath); 171fs.writeFileSync(outPath, ddd); 172