1// Copyright (C) 2020 The Android Open Source Project 2// 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 15// Generation of reference from protos 16 17'use strict'; 18 19const fs = require('fs'); 20const argv = require('yargs').argv 21 22// Removes \n due to 80col wrapping and preserves only end-of-sentence line 23// breaks. 24// TODO dedupe, this is copied from the other gen_proto file. 25function singleLineComment(comment) { 26 comment = comment || ''; 27 comment = comment.trim(); 28 comment = comment.replace(/\.\n/g, '<br>'); 29 comment = comment.replace(/\n/g, ' '); 30 return comment; 31} 32 33function trimQuotes(s) { 34 if (s === undefined) { 35 return s; 36 } 37 const regex = /\"(.*)"/; 38 let m = regex.exec(s); 39 if (m === null) { 40 return null; 41 } 42 return m[1] 43} 44 45function parseTablesInCppFile(filePath) { 46 const hdr = fs.readFileSync(filePath, 'UTF8'); 47 const regex = /^\s*F\(([\s\S]*?)\),\s*\\/mg; 48 let match; 49 let table = []; 50 while ((match = regex.exec(hdr)) !== null) { 51 let def = match[1]; 52 let s = def.split(',').map(s => s.trim()); 53 table.push({ 54 name: s[0], 55 cardinality: s[1], 56 type: s[2], 57 scope: s[3], 58 comment: s[4] === undefined ? undefined : 59 s[4].split('\n').map(trimQuotes).join(' '), 60 }); 61 } 62 return table; 63} 64 65 66function tableToMarkdown(table) { 67 let md = `# Trace Processor Stats\n\n`; 68 md += `<table><thead><tr><td>Name</td><td>Cardinality</td><td>Type</td> 69 <td>Scope</td><td>Description</td></tr></thead>\n`; 70 for (const col of table) { 71 md += `<tr id="${col.name}"><td>${col.name}</td> 72 <td>${col.cardinality}</td><td>${col.type}</td><td>${col.scope}</td> 73 <td>${singleLineComment(col.comment)} </td></tr>\n` 74 } 75 md += '</table>\n\n'; 76 return md; 77} 78 79function main() { 80 const inFile = argv['i']; 81 const outFile = argv['o']; 82 if (!inFile) { 83 console.error('Usage: -i hdr1.h -i hdr2.h -[-o out.md]'); 84 process.exit(1); 85 } 86 87 // Can be either a string (-i single) or an array (-i one -i two). 88 const inFiles = (inFile instanceof Array) ? inFile : [inFile]; 89 90 const table = Array.prototype.concat(...inFiles.map(parseTablesInCppFile)); 91 const md = tableToMarkdown(table); 92 if (outFile) { 93 fs.writeFileSync(outFile, md); 94 } else { 95 console.log(md); 96 } 97 process.exit(0); 98} 99 100main(); 101