Skip to content

Commit

Permalink
Generate rust schemas (#119)
Browse files Browse the repository at this point in the history
- fixes #123 
- fixes #111
  • Loading branch information
Elmer Bulthuis authored Jul 31, 2024
1 parent 5893e20 commit bdd05a4
Show file tree
Hide file tree
Showing 53 changed files with 565 additions and 214 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ on:
jobs:
formatting:
runs-on: ubuntu-latest
container: node:21.5.0-alpine3.19
container: node:22.5.1-alpine3.19
steps:
- uses: actions/checkout@v4
- run: npm ci --workspace-root
- run: npm run formatting

spelling:
runs-on: ubuntu-latest
container: node:21.5.0-alpine3.19
container: node:22.5.1-alpine3.19
steps:
- uses: actions/checkout@v4
- run: npm ci --workspace-root
Expand Down
14 changes: 11 additions & 3 deletions .github/workflows/test-cargo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ on:
jobs:
test:
runs-on: ubuntu-latest
container: rust:1.77.0
container: rust:1.80.0
steps:
- uses: actions/checkout@v4
- run: cargo test --workspace --all-targets

clippy:
runs-on: ubuntu-latest
container: rust:1.77.0
container: rust:1.80.0
steps:
- uses: actions/checkout@v4
- run: rustup component add clippy
- run: cargo clippy --all-targets

rustfmt:
runs-on: ubuntu-latest
container: rust:1.77.0
container: rust:1.80.0
steps:
- uses: actions/checkout@v4
- run: rustup component add rustfmt
- run: cargo fmt --check

fixtures:
runs-on: ubuntu-latest
container: rust:1.80.0
steps:
- uses: actions/checkout@v4
- run: rustup component add rustfmt
- run: ./package-fixtures-cargo
38 changes: 37 additions & 1 deletion .github/workflows/test-npm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,44 @@ on:
jobs:
audit:
runs-on: ubuntu-latest
container: node:21.5.0-alpine3.19
container: node:22.5.1-alpine3.19
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm --workspaces audit --audit-level high

test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: 22.5.1
- run: corepack enable
- run: rustup default 1.80.0

- run: rustup target add wasm32-unknown-unknown
- run: cargo install wasm-bindgen-cli

- uses: actions/checkout@v4
- run: npm ci

- run: npm --workspaces run build --if-present
- run: npm --workspaces test --if-present

fixtures:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: 22.5.1
- run: corepack enable
- run: rustup default 1.80.0

- run: rustup target add wasm32-unknown-unknown
- run: cargo install wasm-bindgen-cli

- uses: actions/checkout@v4
- run: npm ci

- run: ./test-fixtures-npm
- run: ./package-fixtures-npm
29 changes: 0 additions & 29 deletions .github/workflows/test.yaml

This file was deleted.

1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["packages/cargo/jns42-core", "packages/cargo/jns42-generator"]
members = ["packages/cargo/*"]

[profile.release]
lto = true
23 changes: 0 additions & 23 deletions fixtures/specification/models.json

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/specification/string-or-boolean.json

This file was deleted.

3 changes: 0 additions & 3 deletions fixtures/specification/string.json

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ allOf:
- properties:
name:
type: string
examples:
- name: Piet
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "Collection of models",
"$dynamicAnchor": "child",
"$defs": {
"id": {
"$anchor": "id",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions fixtures/specifications/string-or-boolean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": ["string", "boolean"],
"examples": ["hi", true]
}
4 changes: 4 additions & 0 deletions fixtures/specifications/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "string",
"examples": "a"
}
File renamed without changes.
6 changes: 3 additions & 3 deletions fixtures/testing/simple-object.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ schemas:
required:
- id
properties:
id: { $ref: "./models.json#/$defs/id" }
name: { $ref: "./models.json#/$defs/name" }
child: { $ref: "./models.json#/$defs/child" }
id: { $ref: "../specifications/models.json#/$defs/id" }
name: { $ref: "../specifications/models.json#/$defs/name" }
child: { $ref: "../specifications/models.json#/$defs/child" }
examples:
- id: 1
- id: 2
Expand Down
60 changes: 60 additions & 0 deletions generate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env node

import cp from "child_process";
import path from "path";
import { fileURLToPath } from "url";

const dirname = path.dirname(fileURLToPath(import.meta.url));
const workspaceRoot = path.resolve(dirname);

const options = {
shell: true,
stdio: "inherit",
env: process.env,
cwd: workspaceRoot,
};

const packages = [
["schema-draft-04", "http://json-schema.org/draft-04/schema#"],
// ["schema-draft-06", "http://json-schema.org/draft-06/schema#"],
// ["schema-draft-07", "http://json-schema.org/draft-07/schema#"],
// ["schema-draft-2019-09", "https://json-schema.org/draft/2019-09/schema"],
["schema-draft-2020-12", "https://json-schema.org/draft/2020-12/schema"],
// ["schema-swagger-v2", "http://swagger.io/v2/schema.json#/definitions/schema"],
["schema-oas-v3-0", "https://spec.openapis.org/oas/3.0/schema/2021-09-28#/definitions/Schema"],
["schema-oas-v3-1", "https://spec.openapis.org/oas/3.1/dialect/base"],
["swagger-v2", "http://swagger.io/v2/schema.json#"],
["oas-v3-0", "https://spec.openapis.org/oas/3.0/schema/2021-09-28"],
["oas-v3-1", "https://spec.openapis.org/oas/3.1/schema/2022-10-07"],
];

cp.execFileSync("cargo", ["build"], options);
cp.execFileSync("npm", ["run", "--workspaces", "build"], options);

for (const [name, location] of packages) {
cp.execFileSync("cargo", [
"run",
"--package",
"jns42-generator",
"package",
location,
"--package-directory",
path.join(workspaceRoot, "generated", "cargo", name),
"--package-name",
name,
"--package-version",
"0.1.0",
]);

cp.execFileSync("node", [
path.join(workspaceRoot, "packages", "npm", "jns42-generator", "bundled", "program.js"),
"package",
location,
"--package-directory",
path.join(workspaceRoot, "generated", "npm", name),
"--package-name",
name,
"--package-version",
"0.1.0",
]);
}
39 changes: 39 additions & 0 deletions package-fixtures-cargo
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

set -e

cd $(dirname $0)

export NODE_ENV=development

set +e

ERROR=0

export DIRECTORY=/tmp/jns42/$RANDOM

for F in $(ls fixtures/specifications); do
export N=${F%.*};

echo $N;

if ! cargo run --package jns42-generator package \
fixtures/specifications/$F \
--package-directory $DIRECTORY/$N \
--package-name $N \
--package-version "0.0.0" \
; then
ERROR=1;
fi;

if ! ( cd $DIRECTORY/$N ; cargo test); then
ERROR=1;
fi;

done;

rm -rf $DIRECTORY

exit $ERROR


39 changes: 39 additions & 0 deletions package-fixtures-npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

set -e

cd $(dirname $0)

export NODE_ENV=development

npm run build --workspaces --if-present

set +e

ERROR=0

export DIRECTORY=/tmp/jns42/$RANDOM

for F in $(ls fixtures/specifications); do
export N=${F%.*};

echo $N;

if ! node ./packages/npm/jns42-generator/bundled/program.js package \
fixtures/specifications/$F \
--package-directory $DIRECTORY/$N \
--package-name $N \
--package-version "0.0.0" \
; then
ERROR=1;
fi;

if ! ( cd $DIRECTORY/$N ; npm install ; npm test); then
ERROR=1;
fi;

done;

rm -rf $DIRECTORY

exit $ERROR
6 changes: 2 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"private": true,
"packageManager": "npm@10.3.0",
"packageManager": "npm@10.8.1",
"workspaces": [
"generated/npm/*",
"packages/npm/jns42-core",
"packages/npm/jns42-generator"
"packages/npm/*"
],
"scripts": {
"spelling": "cspell",
Expand Down
1 change: 1 addition & 0 deletions packages/cargo/jns42-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ wasm-bindgen = "0.2.92"
wasm-bindgen-futures = "0.4.42"
gloo = "0.11.0"
semver = "1.0.23"
js-sys = "0.3.69"


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
Loading

0 comments on commit bdd05a4

Please sign in to comment.