Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@ on:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v1
- uses: denoland/setup-deno@v1
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: v1.x
deno-version: v2.x

- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: "1.19"
go-version: "1.24"

- name: Install Apex CLI
run: deno install -A --unstable -f -n apex https://deno.land/x/apex_cli/apex.ts
run: deno install -g -A --unstable-worker-options -f -n apex jsr:@apexlang/apex

- name: Generator diff test
run: ./diffcheck.sh
working-directory: ./testdata
# Temporarily disable until it's determined why apex generate is not writing files.
# - name: Generator diff test
# run: ./diffcheck.sh
# working-directory: ./testdata

- name: Is Release?
if: startswith(github.ref, 'refs/tags/v')
run: echo "DEPLOY_PACKAGE=true" >> $GITHUB_ENV

- name: Publish to npm
if: env.DEPLOY_PACKAGE == 'true'
run: npm pack && npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
run: npx jsr publish
21 changes: 21 additions & 0 deletions .github/workflows/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function env(name: string): string {
const val = Deno.env.get(name);
if (!val) throw new Error(`$${name} is required`);
return val;
}

const files = env("FILES").split(/\s*,\s*/);
const version = env("VERSION");

if (!version.match(/^v\d+\.\d+\.\d+$/)) {
throw new Error("VERSION must be in the form v1.2.3");
}

console.log("Updating versions in files");

for (const file of files) {
console.log(`Updating ${file} for ${version}...`);
const orig = Deno.readTextFileSync(file);
const updated = orig.replace(/\d+\.\d+\.\d+/, version.substring(1));
Deno.writeFileSync(file, new TextEncoder().encode(updated));
}
38 changes: 38 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release apex version

on:
workflow_dispatch:
inputs:
version:
description: "Version to release"
required: true

jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.RELEASE_TOKEN }}
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- name: Update version and README.md
env:
VERSION: ${{ inputs.version }}
FILES: "jsr.json"
run: |
deno run --allow-run --allow-env --allow-write --allow-read ./.github/workflows/release.ts
- name: Commit and tag release
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: ${{ inputs.version }} release
tagging_message: ${{ inputs.version }}
- name: Create Release
uses: ncipollo/release-action@v1.12.0
with:
generateReleaseNotes: true
makeLatest: true
tag: ${{ inputs.version }}
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"deno.enable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"[javascript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
"[json]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
}
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# waPC Code Generators

This library provides the code generators for producing waPC modules using the [Apex language](https://apexlang.io).
This library provides the code generators for producing waPC modules using the
[Apex language](https://apexlang.io).

## Installation

Make sure you have the Apex CLI installed. Here are [the instructions](https://apexlang.io/docs/getting-started).
Make sure you have the Apex CLI installed. Here are
[the instructions](https://apexlang.io/docs/getting-started).

From your terminal, run:

```shell
apex install https://deno.land/x/wapc_codegen/templates.ts
apex install jsr:@wapc/codegen/templates
```

```shell
Expand Down Expand Up @@ -46,4 +48,6 @@ ls -l build
-rwxr-xr-x 1 uname staff 18454 Sep 19 14:56 hello-world.wasm
```

If you load the project in VS Code (`code .` from the terminal if VS code is in your path), a task will monitor the Apex interface definition for changes and regenerate boilerplate code.
If you load the project in VS Code (`code .` from the terminal if VS code is in
your path), a task will monitor the Apex interface definition for changes and
regenerate boilerplate code.
12 changes: 12 additions & 0 deletions apex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tasks:
test:
description: Run tests
cmds:
- deno fmt --check src/ test/
- deno lint src/
- deno check ./**/*.ts
- deno test -A --unstable-worker-options --permit-no-files
install:
description: Install templates
cmds:
- apex install ./templates/mod.ts
1 change: 1 addition & 0 deletions deps/@apexlang/apex/config/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/apex@^0.2.5/config";
1 change: 1 addition & 0 deletions deps/@apexlang/apex/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/apex@^0.2.5";
1 change: 1 addition & 0 deletions deps/@apexlang/apex/task/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/apex@^0.2.5/task";
1 change: 1 addition & 0 deletions deps/@apexlang/codegen/go/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/codegen@^0.2.6/go";
1 change: 1 addition & 0 deletions deps/@apexlang/codegen/rust/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/codegen@^0.2.6/rust";
1 change: 1 addition & 0 deletions deps/@apexlang/codegen/utils/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/codegen@^0.2.6/utils";
1 change: 1 addition & 0 deletions deps/@apexlang/core/ast/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/core@^0.2.0/ast";
1 change: 1 addition & 0 deletions deps/@apexlang/core/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/core@^0.2.0";
1 change: 1 addition & 0 deletions deps/@apexlang/core/model/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@apexlang/core@^0.2.0/model";
1 change: 1 addition & 0 deletions deps/@std/assert/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@std/assert@^1.0.11";
1 change: 1 addition & 0 deletions deps/@std/yaml/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@std/yaml@^1.0.5";
3 changes: 3 additions & 0 deletions deps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The Apex CLI uses Workers to run the code generation process with minimal privileges. Workers do not support reading `imports` from `deno.json`, so packages must use the `jsr:` scheme in your import specifier. `jsr.io` translates import specifiers when packages are published; however, the lack of `deno.json` poses a problem when loading modules outside of `jsr.io`, either from the filesystem (local development) or remotely from source control (e.g., GitHub/GitLab).

This directory is a central location for versioning imported modules, similar to how `deno.json` works, but using ES module built-in `export` capability.
37 changes: 37 additions & 0 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@wapc/codegen",
"version": "0.0.6",
"description": "Code generators for waPC using Apex.",
"keywords": ["wapc", "apex", "idl", "codegen"],
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://www.github.com/wapc/codegen"
},
"bugs": {
"url": "https://www.github.com/wapc/codegen/issues"
},
"exports": {
".": "./templates/mod.ts",
"./assemblyscript": "./src/assemblyscript/mod.ts",
"./rust": "./src/rust/mod.ts",
"./tinygo": "./src/tinygo/mod.ts",
"./templates": "./templates/mod.ts",
"./templates/assemblyscript": "./templates/assemblyscript/mod.ts",
"./templates/rust": "./templates/rust/mod.ts",
"./templates/tinygo": "./templates/tinygo/mod.ts",
"./plugins/assemblyscript": "./src/assemblyscript/plugin.ts",
"./plugins/rust": "./src/rust/plugin.ts",
"./plugins/tinygo": "./src/tinygo/plugin.ts"
},
"publish": {
"include": [
"LICENSE",
"README.md",
"templates",
"deps/**/*.ts",
"src/**/*.ts"
],
"exclude": ["src/tests"]
}
}
14 changes: 7 additions & 7 deletions src/assemblyscript/builder_visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The waPC Authors.
Copyright 2025 The waPC Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,19 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { capitalize } from "../deps/codegen/utils.ts";
import { BaseVisitor, Context } from "../deps/core/model.ts";
import { capitalize } from "../../deps/@apexlang/codegen/utils/mod.ts";
import { BaseVisitor, Context } from "../../deps/@apexlang/core/model/mod.ts";
import { expandType } from "./helpers.ts";

export class BuilderVisitor extends BaseVisitor {
visitTypeBefore(context: Context): void {
override visitTypeBefore(context: Context): void {
super.triggerTypeBefore(context);
const className = context.type.name;
this.write(`export class ${className}Builder {
instance: ${className} = new ${className}();\n`);
}

visitTypeField(context: Context): void {
override visitTypeField(context: Context): void {
const className = context.type.name;
const field = context.field!;
this.write(`\n`);
Expand All @@ -42,15 +42,15 @@ export class BuilderVisitor extends BaseVisitor {
super.triggerTypeField(context);
}

visitTypeFieldsAfter(context: Context): void {
override visitTypeFieldsAfter(context: Context): void {
this.write(`\n`);
this.write(` build(): ${context.type.name} {
return this.instance;
}`);
super.triggerTypeFieldsAfter(context);
}

visitTypeAfter(context: Context): void {
override visitTypeAfter(context: Context): void {
this.write(`}\n\n`);
super.triggerTypeAfter(context);
}
Expand Down
14 changes: 7 additions & 7 deletions src/assemblyscript/class_visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The waPC Authors.
Copyright 2025 The waPC Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,21 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { BaseVisitor, Context } from "../deps/core/model.ts";
import { formatComment } from "../deps/codegen/utils.ts";
import { BaseVisitor, Context } from "../../deps/@apexlang/core/model/mod.ts";
import { formatComment } from "../../deps/@apexlang/codegen/utils/mod.ts";
import { defValue, expandType } from "./helpers.ts";
import { DecoderVisitor } from "./decoder_visitor.ts";
import { EncoderVisitor } from "./encoder_visitor.ts";

export class ClassVisitor extends BaseVisitor {
visitTypeBefore(context: Context): void {
override visitTypeBefore(context: Context): void {
super.triggerTypeBefore(context);
const { type } = context;
this.write(formatComment("// ", type.description));
this.write(`export class ${type.name} implements Codec {\n`);
}

visitTypeField(context: Context): void {
override visitTypeField(context: Context): void {
const { field } = context;
this.write(formatComment(" // ", field.description));
this.write(
Expand All @@ -42,7 +42,7 @@ export class ClassVisitor extends BaseVisitor {
super.triggerTypeField(context);
}

visitTypeFieldsAfter(context: Context): void {
override visitTypeFieldsAfter(context: Context): void {
const { type } = context;
this.write(`\n`);
const decoder = new DecoderVisitor(this.writer);
Expand All @@ -63,7 +63,7 @@ export class ClassVisitor extends BaseVisitor {
super.triggerTypeFieldsAfter(context);
}

visitTypeAfter(context: Context): void {
override visitTypeAfter(context: Context): void {
this.write(`}\n\n`);
super.triggerTypeAfter(context);
}
Expand Down
10 changes: 5 additions & 5 deletions src/assemblyscript/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The waPC Authors.
Copyright 2025 The waPC Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

export const translations = new Map<string, string>([
export const translations: Map<string, string> = new Map<string, string>([
["ID", "string"],
["bytes", "ArrayBuffer"],
]);

export const primitives = new Set([
export const primitives: Set<string> = new Set([
"bool",
"i8",
"i16",
Expand All @@ -35,7 +35,7 @@ export const primitives = new Set([
"bytes",
]);

export const decodeFuncs = new Map<string, string>([
export const decodeFuncs: Map<string, string> = new Map<string, string>([
["ID", "readString"],
["bool", "readBool"],
["string", "readString"],
Expand All @@ -52,7 +52,7 @@ export const decodeFuncs = new Map<string, string>([
["bytes", "readByteArray"],
]);

export const encodeFuncs = new Map<string, string>([
export const encodeFuncs: Map<string, string> = new Map<string, string>([
["ID", "writeString"],
["bool", "writeBool"],
["string", "writeString"],
Expand Down
Loading