Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
continued server codegen work
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed Nov 18, 2022
1 parent b32159c commit 8a9066c
Show file tree
Hide file tree
Showing 36 changed files with 395 additions and 240 deletions.
1 change: 1 addition & 0 deletions .github/workflows/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ jobs:
key: ${{ runner.os }}-deno-${{ hashFiles('lock.json', 'deps/**/*.ts') }}
- name: Setup Polkadot
uses: ./.github/actions/setup-polkadot
- run: deno task codegen
- run: deno task run ${{ matrix.example_path }}
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ jobs:
- name: Setup polkadot
uses: ./.github/actions/setup-polkadot
- run: deno task lint
- run: deno task codegen
- run: deno task star
- run: deno task test
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"deno.codeLens.testArgs": ["--no-check=remote", "-A", "-L=info"],
"deno.config": "./deno.jsonc",
"deno.importMap": "./import_map_cache.json",
"deno.enable": true,
"deno.lint": true,
"editor.defaultFormatter": "dprint.dprint",
Expand Down
40 changes: 20 additions & 20 deletions _tasks/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { codegen } from "../codegen/mod.ts"
import { FsCache } from "../codegen/cache.ts"
import { CodegenServer } from "../codegen/serve.ts"
import * as fs from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"
import * as C from "../mod.ts"
import * as testClients from "../test_util/clients/mod.ts"
import * as U from "../util/mod.ts"

const currentDir = path.dirname(path.fromFileUrl(import.meta.url))
const codegenDir = path.join(currentDir, "../codegen/_output")
const cacheDir = "target/codegen"
await fs.emptyDir(path.join(cacheDir, "generated"))
const cache = new FsCache(cacheDir)
const port = 5646
console.log(`http://localhost:${port}/`)
new CodegenServer(cache).listen(port)

await Deno.remove(codegenDir, { recursive: true })
await Deno.run({
cmd: [
"deno",
"cache",
"--import-map",
"import_map_localhost.json",
`--reload=http://localhost:${port}/`,
"examples/mod.ts",
],
}).status()

await Promise.all(
Object.entries(testClients).map(async ([runtime, client]) => {
// if (runtime !== "polkadot") return
const metadata = U.throwIfError(await C.metadata(client)().run())
const outDir = path.join(codegenDir, runtime)
await codegen({
importSpecifier: "../../../mod.ts",
clientFile: `../../../test_util/clients/${runtime}.ts`,
metadata,
})
.write(outDir)
}),
)
Deno.exit(0)
43 changes: 43 additions & 0 deletions codegen/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as $ from "../deps/scale.ts"
import * as fs from "../deps/std/fs.ts"
import * as path from "../deps/std/path.ts"
import { getOrInit, WeakMemo } from "../util/mod.ts"

export abstract class Cache {
rawMemo = new WeakMemo<string, Uint8Array>()
decodedMemo = new Map<$.AnyCodec, WeakMemo<string, object>>()
abstract _getRaw(key: string, init: () => Promise<Uint8Array>): Promise<Uint8Array>

getRaw(key: string, init: () => Promise<Uint8Array>): Promise<Uint8Array> {
return this.rawMemo.run(key, () => this._getRaw(key, init))
}

get<T extends object>(key: string, $value: $.Codec<T>, init: () => Promise<T>): Promise<T> {
const memo = getOrInit(this.decodedMemo, $value, () => new WeakMemo()) as WeakMemo<string, T>
return memo.run(key, async () => {
let value: T | undefined
const raw = await this.getRaw(key, async () => $value.encode(value = await init()))
value ??= $value.decode(raw)
return value
})
}
}

export class FsCache extends Cache {
constructor(readonly location: string) {
super()
}

async _getRaw(key: string, init: () => Promise<Uint8Array>) {
const file = path.join(this.location, key)
try {
return await Deno.readFile(file)
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) throw e
const content = await init()
await fs.ensureDir(path.dirname(file))
await Deno.writeFile(file, content)
return content
}
}
}
8 changes: 4 additions & 4 deletions codegen/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { S } from "./utils.ts"
export interface CodegenProps {
metadata: M.Metadata
importSpecifier: string
clientFile: string
clientDecl: string
}

export function codegen(props: CodegenProps): Files {
Expand All @@ -25,9 +25,9 @@ export function codegen(props: CodegenProps): Files {
"capi.ts",
() =>
`\
export const C = await import("" + ${S.string(props.importSpecifier)})
export const { $ } = C;
export const { client } = await import("" + ${S.string(props.clientFile)})
export { $ } from ${S.string(props.importSpecifier)}
export * as C from ${S.string(props.importSpecifier)}
${props.clientDecl}
`,
)

Expand Down
Loading

0 comments on commit 8a9066c

Please sign in to comment.