This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,69 @@ | ||
import { tsFormatter } from "../deps/dprint.ts"; | ||
import { parse } from "../deps/std/flags.ts"; | ||
import * as path from "../deps/std/path.ts"; | ||
import * as M from "../frame_metadata/mod.ts"; | ||
import { createCodecVisitor } from "./codecVisitor.ts"; | ||
import { genMetadata } from "./genMetadata.ts"; | ||
import { createTypeVisitor } from "./typeVisitor.ts"; | ||
import { Decl, printDecls, S } from "./utils.ts"; | ||
|
||
const importSource = new URL("../mod.ts", import.meta.url).toString(); | ||
import { Decl, Files, importSource, printDecls, S } from "./utils.ts"; | ||
|
||
if (import.meta.main) { | ||
const args = parse(Deno.args, { | ||
string: ["metadata", "output"], | ||
}); | ||
if (args.help || args["?"]) { | ||
console.log("Usage: codegen --metadata <file> --output <file>"); | ||
console.log("Usage: codegen --metadata <file> --output <dir>"); | ||
Deno.exit(0); | ||
} | ||
if (!args.metadata) throw new Error("Must specify metadata file"); | ||
if (!args.output) throw new Error("Must specify output file"); | ||
const { metadata: metadataFile, output: outputFile } = args; | ||
const metadata = M.fromPrefixedHex(await Deno.readTextFile(metadataFile!)); | ||
if (!args.output) throw new Error("Must specify output dir"); | ||
const { metadata: metadataFile, output: outputDir } = args; | ||
const metadata = M.fromPrefixedHex(await Deno.readTextFile(metadataFile)); | ||
const output = codegen(metadata); | ||
try { | ||
const formatted = tsFormatter.formatText("gen.ts", output); | ||
await Deno.writeTextFile(outputFile!, formatted); | ||
} catch (e) { | ||
await Deno.writeTextFile(outputFile!, output); | ||
throw e; | ||
const errors = []; | ||
await Deno.mkdir(outputDir, { recursive: true }); | ||
for (const [relativePath, file] of output.entries()) { | ||
const outputPath = path.join(outputDir, relativePath); | ||
const content = S.toString(file.getContent()); | ||
try { | ||
const formatted = tsFormatter.formatText("gen.ts", content); | ||
await Deno.writeTextFile(outputPath, formatted); | ||
} catch (e) { | ||
await Deno.writeTextFile(outputPath, content); | ||
errors.push(e); | ||
} | ||
} | ||
if (errors.length) { | ||
console.error(errors); | ||
Deno.exit(1); | ||
} | ||
} | ||
|
||
export function codegen(metadata: M.Metadata) { | ||
export function codegen(metadata: M.Metadata): Files { | ||
const decls: Decl[] = []; | ||
|
||
const { tys } = metadata; | ||
|
||
const files: Files = new Map(); | ||
|
||
decls.push({ | ||
path: "_", | ||
code: [ | ||
"import { ChainError, BitSequence, Era, $, $null, $era } from", | ||
S.string(importSource), | ||
"\n", | ||
["import { ChainError, BitSequence, Era, $ } from", S.string(importSource)], | ||
[`import * as _codec from "./codecs.ts"`], | ||
], | ||
}); | ||
|
||
const typeVisitor = createTypeVisitor(tys, decls); | ||
const codecVisitor = createCodecVisitor(tys, decls, typeVisitor); | ||
const codecVisitor = createCodecVisitor(tys, decls, typeVisitor, files); | ||
|
||
typeVisitor.tys.map((x) => typeVisitor.visit(x)); | ||
codecVisitor.tys.map((x) => codecVisitor.visit(x)); | ||
|
||
genMetadata(metadata, decls, codecVisitor); | ||
genMetadata(metadata, decls); | ||
|
||
files.set("mod.ts", { getContent: () => printDecls(decls) }); | ||
|
||
return printDecls(decls); | ||
return files; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters