SRS/tools/preprop.ts

135 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

2022-06-11 15:10:10 +09:00
import { readAll } from "https://deno.land/std@0.143.0/streams/mod.ts";
import * as log from "https://deno.land/std@0.143.0/log/mod.ts";
import { WriterHandler } from "https://deno.land/std@0.143.0/log/handlers.ts";
import * as Eta from "https://deno.land/x/eta@v1.12.3/mod.ts";
import { Issue } from "./githubType.ts";
2022-06-11 17:20:41 +09:00
import testcaseData from "./testcases.json" assert { type: "json"};
type Testcase = {
id: number,
subId: number | null,
content: string,
procedure: string,
testData: string| null,
expected: string,
actual: string,
pass: boolean
}
const testcases = testcaseData as Testcase[];
2022-06-11 15:10:10 +09:00
class StderrHandler extends WriterHandler {
protected _writer: Deno.Writer;
#encoder: TextEncoder;
constructor(levelName: log.LevelName, options: log.HandlerOptions = {}){
super(levelName, options);
this.#encoder = new TextEncoder();
this._writer = Deno.stderr;
}
log(msg: string): void {
const encoded = this.#encoder.encode(msg);
Deno.stderr.writeSync(encoded);
}
}
interface Book {
2022-06-11 16:28:45 +09:00
sections: BookItem[];
2022-06-11 15:10:10 +09:00
}
2022-06-11 16:28:45 +09:00
type Separator = "Separator";
//type PartTitle = ;
type BookItem = Separator | {
2022-06-11 15:10:10 +09:00
Chapter: Chapter;
}
2022-06-11 16:28:45 +09:00
2022-06-11 15:10:10 +09:00
interface Chapter {
name: string;
content: string;
/** section number */
number?: number[];
2022-06-11 16:28:45 +09:00
sub_items: BookItem[];
2022-06-11 15:10:10 +09:00
path?: string;
source_path?: string;
parent_names: string[];
}
if (import.meta.main) {
await log.setup({
handlers: {
console: new StderrHandler("INFO", {
formatter: "{levelName} {msg}",
}),
},
loggers: {
default: {
level: "INFO",
handlers: ["console"],
}
}
}
);
main(Deno.args);
}
async function getIssues(){
const issue_path = "cache/issues.json";
const c = await Deno.readTextFile(issue_path);
const issues = JSON.parse(c) as Issue[];
issues.sort((a, b) => a.number - b.number);
return issues;
}
2022-06-11 15:47:16 +09:00
function toHeadId(name: string){
return name.replaceAll(/[^A-Za-z\s0-9]/gi,"").toLocaleLowerCase().replaceAll(" ","-");
}
2022-06-11 16:28:45 +09:00
async function getCurrentGitHash() {
const res = await Deno.run({cmd:["git", "rev-parse", "HEAD"], stdout:"piped"});
const hash = new TextDecoder().decode(await res.output()).trim();
res.close();
return hash;;
}
2022-06-11 15:10:10 +09:00
async function main(args: string[]) {
if (args.length > 1) {
//log.info(`args: ${JSON.stringify(args)}`);
if (args[0] === "supports") {
Deno.exit(0);
}
}
const issues = await getIssues();
2022-06-11 15:47:16 +09:00
const table = new Map();
issues.forEach((x)=>{
const category = x.title.split(":")[0];
if(!category) return;
let c = table.get(category)
if(!c){
c = [];
table.set(category,c);
}
c.push(x);
2022-06-11 16:28:45 +09:00
});
const gitHash = await getCurrentGitHash();
log.info(`start\n`);
2022-06-11 15:10:10 +09:00
const data = await readAll(Deno.stdin);
const jsonText = new TextDecoder().decode(data);
await Deno.writeTextFile("log.json", jsonText);
const [context, book] = JSON.parse(jsonText) as [any, Book];
book.sections.forEach(x=>{
2022-06-11 16:28:45 +09:00
if(x === "Separator"){
//skip
}
else {
2022-06-11 19:38:24 +09:00
log.info(`render ${x.Chapter.name}\n`);
2022-06-11 16:28:45 +09:00
x.Chapter.content = Eta.render(x.Chapter.content, {
issues: issues,
table: table,
gitHash: gitHash,
toHeadId: toHeadId,
2022-06-11 17:20:41 +09:00
testcases: testcases
2022-06-11 16:28:45 +09:00
}) as string;
}
2022-06-11 15:10:10 +09:00
})
//Deno.stderr.writeSync(new TextEncoder().encode(`context: ${JSON.stringify(context)}\n`));
console.log(JSON.stringify(book));
}