-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create codegen.ts * codegen moment * create lab for generating other labs * generate codegen lab * add titles to example notes
- Loading branch information
1 parent
0c67e38
commit e80f98d
Showing
12 changed files
with
262 additions
and
45 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { generateLab } from "./generate.ts"; | ||
|
||
await Deno.writeTextFile( | ||
"./codegen/codegen_lab.ts", | ||
generateLab({ | ||
name: "codegenLab", | ||
labsImportSource: "labs/labs.ts", | ||
procedures: [ | ||
{ | ||
name: "codegen.lab", | ||
import: { | ||
name: "generateLab", | ||
source: "labs/codegen/generate.ts", | ||
}, | ||
}, | ||
], | ||
}), | ||
); |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Generated lab file. DO NOT MODIFY. | ||
import { Lab } from "labs/labs.ts"; | ||
import { generateLab } from "labs/codegen/generate.ts" | ||
|
||
export const codegenLab = new Lab() | ||
.procedure("codegen.lab", generateLab); |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { importsOf } from "./imports.ts"; | ||
import type { | ||
ExtendDescriptor, | ||
ImportName, | ||
ImportSource, | ||
LabDescriptor, | ||
ProcedureDescriptor, | ||
VariableDescriptor, | ||
} from "./types.ts"; | ||
|
||
export const LABS_IMPORT_SOURCE = "./labs.ts"; | ||
|
||
export function generateLab(descriptor: LabDescriptor): string { | ||
const instructions = generateInstructions(descriptor); | ||
return `// Generated lab file. DO NOT MODIFY. | ||
import { Lab } from "${descriptor.labsImportSource ?? LABS_IMPORT_SOURCE}"; | ||
${generateImports(importsOf(descriptor))} | ||
export const ${descriptor.name} = new Lab()${ | ||
instructions.length === 0 ? "" : `\n${indent(...instructions)}` | ||
};\n`; | ||
} | ||
|
||
export function generateImports( | ||
imports: Map<ImportSource, Set<ImportName>>, | ||
): string { | ||
const generated = Array.from(imports) | ||
.toSorted(([a], [b]) => a.localeCompare(b)) | ||
.map(([importSource, names]) => { | ||
return generateImport(Array.from(names).toSorted(), importSource); | ||
}) | ||
.join("\n"); | ||
if (generated.length === 0) { | ||
return ""; | ||
} | ||
|
||
return `${generated}\n`; | ||
} | ||
|
||
export function generateImport(names: string[], importSource: string): string { | ||
return `import { ${names.join(", ")} } from "${importSource}"`; | ||
} | ||
|
||
export function generateInstructions(descriptor: LabDescriptor): string[] { | ||
const instructions: string[] = []; | ||
if (descriptor.extends) { | ||
instructions.push(...generateExtends(descriptor.extends)); | ||
} | ||
|
||
if (descriptor.variables) { | ||
instructions.push(...generateVariables(descriptor.variables)); | ||
} | ||
|
||
if (descriptor.procedures) { | ||
instructions.push(...generateProcedures(descriptor.procedures)); | ||
} | ||
|
||
return instructions; | ||
} | ||
|
||
export function generateExtends(descriptors: ExtendDescriptor[]): string[] { | ||
return descriptors.map((descriptor) => generateExtend(descriptor)); | ||
} | ||
|
||
export function generateExtend(descriptor: ExtendDescriptor): string { | ||
return `.extend(${descriptor.import.name})`; | ||
} | ||
|
||
export function generateVariables(descriptors: VariableDescriptor[]): string[] { | ||
return descriptors.map((descriptor) => generateVariable(descriptor)); | ||
} | ||
|
||
export function generateVariable(descriptor: VariableDescriptor): string { | ||
return `.variable("${descriptor.name}", ${descriptor.value})`; | ||
} | ||
|
||
export function generateProcedures( | ||
descriptors: ProcedureDescriptor[], | ||
): string[] { | ||
return descriptors.map((descriptor) => generateProcedure(descriptor)); | ||
} | ||
|
||
export function generateProcedure(descriptor: ProcedureDescriptor): string { | ||
return `.procedure("${descriptor.name}", ${descriptor.import.name})`; | ||
} | ||
|
||
export function indent(...lines: string[]): string { | ||
return lines.map((line) => ` ${line}`).join("\n"); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ImportName, ImportSource, LabDescriptor } from "./types.ts"; | ||
|
||
export function importsOf( | ||
descriptor: LabDescriptor, | ||
): Map<ImportSource, Set<ImportName>> { | ||
const imports = new Map<ImportSource, Set<ImportName>>(); | ||
for (const extend of descriptor.extends ?? []) { | ||
if (!imports.has(extend.import.source)) { | ||
imports.set(extend.import.source, new Set()); | ||
} | ||
|
||
imports.get(extend.import.source)!.add(extend.import.name); | ||
} | ||
|
||
for (const procedure of descriptor.procedures ?? []) { | ||
if (!imports.has(procedure.import.source)) { | ||
imports.set(procedure.import.source, new Set()); | ||
} | ||
|
||
imports.get(procedure.import.source)!.add(procedure.import.name); | ||
} | ||
|
||
return imports; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface LabDescriptor { | ||
name: string; | ||
labsImportSource?: ImportSource; | ||
extends?: ExtendDescriptor[]; | ||
variables?: VariableDescriptor[]; | ||
procedures?: ProcedureDescriptor[]; | ||
} | ||
|
||
export interface VariableDescriptor { | ||
name: string; | ||
value: string; | ||
} | ||
|
||
export interface ExtendDescriptor { | ||
import: Importable; | ||
} | ||
|
||
export interface ProcedureDescriptor { | ||
name: string; | ||
import: Importable; | ||
} | ||
|
||
export interface Importable { | ||
name: ImportName; | ||
source: ImportSource; | ||
} | ||
|
||
export type ImportSource = string; | ||
export type ImportName = string; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"imports": { | ||
"labs/": "./" | ||
}, | ||
"tasks": { | ||
"generate": "deno run -Ar https://deno.land/x/generate/cli/main.ts gen.ts", | ||
"example": "deno run ./example/example.ts" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { codegenLab } from "labs/codegen/codegen_lab.ts"; | ||
|
||
await Deno.writeTextFile( | ||
"./example/my_lab.ts", | ||
codegenLab.execute("codegen.lab", { | ||
name: "myLab", | ||
labsImportSource: "labs/labs.ts", | ||
extends: [ | ||
{ import: { name: "notesLab", source: "labs/notes.ts" } }, | ||
{ import: { name: "linksLab", source: "labs/links.ts" } }, | ||
], | ||
}), | ||
); |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { myLab } from "./my_lab.ts"; | ||
|
||
if (import.meta.main) { | ||
main(); | ||
} | ||
|
||
function main() { | ||
const note1 = myLab.execute( | ||
"notes.add", | ||
{ title: "Hi", content: "Hello, world!" }, | ||
); | ||
const note2 = myLab.execute( | ||
"notes.add", | ||
{ title: "Bye", content: "Goodbye, world!" }, | ||
); | ||
myLab.execute( | ||
"links.link", | ||
{ ids: [note1.id, note2.id] }, | ||
); | ||
|
||
console.log(renderNote(note1.id)); | ||
} | ||
|
||
function renderNote(id: string): string { | ||
const note = myLab.execute("notes.get", { id }); | ||
if (!note) { | ||
throw new Error(`Note not found: ${id}`); | ||
} | ||
|
||
const title = renderTitle(note.title); | ||
const links = myLab.execute("links.get", { id }); | ||
return `# [${title}](${id}) | ||
${note.content ?? "No content."} | ||
## Links | ||
${ | ||
!links || links.links.length === 0 ? "None" : links.links.map(({ id }) => { | ||
const linkedNote = myLab.execute("notes.get", { id }); | ||
if (!linkedNote) { | ||
throw new Error(`Linked note not found: ${id}`); | ||
} | ||
return `- [${renderTitle(linkedNote.title)}](${id})`; | ||
}).join("\n") | ||
} | ||
`; | ||
} | ||
|
||
function renderTitle(title?: string): string { | ||
return title ?? "Untitled"; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Generated lab file. DO NOT MODIFY. | ||
import { Lab } from "labs/labs.ts"; | ||
import { linksLab } from "labs/links.ts" | ||
import { notesLab } from "labs/notes.ts" | ||
|
||
export const myLab = new Lab() | ||
.extend(notesLab) | ||
.extend(linksLab); |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//deno:generate deno run --allow-write codegen/codegen.ts | ||
//deno:generate deno run --allow-write example/codegen.ts |