Skip to content

Commit

Permalink
chore: Move schema generator into non-published crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Feb 8, 2024
1 parent 6e6efea commit ecc4e4f
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: dtolnay/rust-toolchain@stable

- name: Run generate schemas
run: cargo run --bin schemas
run: cargo run -p homestar-schemas

# - name: Check for changed files
# id: changed-files
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"homestar-functions/*",
"homestar-invocation",
"homestar-runtime",
"homestar-schemas",
"homestar-wasm",
"homestar-workflow",
"homestar-workspace-hack",
Expand Down
7 changes: 0 additions & 7 deletions homestar-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ path = "src/main.rs"
doc = false
bench = false

[[bin]]
name = "schemas"
path = "schemas/generate.rs"
bench = false
doc = false
test = false

[[test]]
name = "integration"
path = "tests/main.rs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"openrpc": "1.2.6",
"info": {
"title": "homestar",
"description": "Homestar runtime implementation",
"version": "0.10.0",
"description": "",
"version": "0.1.0",
"contact": {
"name": null,
"email": null,
"url": "https://github.com/ipvm-wg/homestar/tree/main/homestar-runtime"
"url": ""
},
"license": {
"name": "Apache-2.0",
"name": "",
"url": null
}
},
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion homestar-runtime/src/network/webserver/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use tracing::debug;
use tracing::{error, warn};

/// OpenRPC API document
const API_SCHEMA_DOC: &str = include_str!("../../../schemas/docs/api.json");
const API_SCHEMA_DOC: &str = include_str!("../../../schemas/api.json");

/// OpenRPC API discovery endpoint.
pub(crate) const DISCOVER_ENDPOINT: &str = "rpc_discover";
Expand Down
2 changes: 1 addition & 1 deletion homestar-runtime/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn test_discovery_endpoint_integration() -> Result<()> {
assert_eq!(http_resp.status(), 200);
let http_resp = http_resp.json::<serde_json::Value>().await.unwrap();

const API_SCHEMA_DOC: &str = include_str!("../schemas/docs/api.json");
const API_SCHEMA_DOC: &str = include_str!("../schemas/api.json");
assert_eq!(http_resp, serde_json::json!(API_SCHEMA_DOC));
});

Expand Down
27 changes: 27 additions & 0 deletions homestar-schemas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "homestar-schemas"
publish = false
version = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }

[dependencies]
homestar-invocation = { version = "0.1", path = "../homestar-invocation", default-features = false }
homestar-runtime = { version = "0.1", path = "../homestar-runtime", default-features = false, features = [
"websocket-notify",
] }
homestar-workflow = { version = "0.1", path = "../homestar-workflow", default-features = false }
homestar-workspace-hack = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

[[bin]]
name = "schemas"
path = "src/main.rs"
bench = false
doc = false
test = false

[features]
default = []
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Standalone binary to generate OpenRPC API docs and
//! JSON Schemas for method params and notifications.
//! Binary to generate OpenRPC API docs and JSON Schemas.

use homestar_invocation::Receipt;
use homestar_runtime::{
Expand All @@ -11,7 +10,6 @@ use schemars::{
schema_for,
};
use std::{fs, io::Write};

mod openrpc;
use openrpc::document::{
ContactObject, ContentDescriptorObject, ContentDescriptorOrReference,
Expand All @@ -20,62 +18,48 @@ use openrpc::document::{
};

fn main() {
println!("{}", env!("CARGO_MANIFEST_DIR"));
fn schema_path(name: &str) -> String {
format!(
"{}/../homestar-runtime/schemas/{}",
env!("CARGO_MANIFEST_DIR"),
name
)
}

let health_schema = schema_for!(Health);
let _ = fs::File::create(format!(
"{}/schemas/docs/health.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&health_schema).unwrap());
let _ = fs::File::create(schema_path("health.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&health_schema).unwrap());

let metrics_schema = schema_for!(PrometheusData);
let _ = fs::File::create(format!(
"{}/schemas/docs/metrics.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&metrics_schema).unwrap());
let _ = fs::File::create(schema_path("metrics.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&metrics_schema).unwrap());

let node_info_schema = schema_for!(NodeInfo);
let _ = fs::File::create(format!(
"{}/schemas/docs/node_info.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&node_info_schema).unwrap());
let _ = fs::File::create(schema_path("node_info.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&node_info_schema).unwrap());

let network_schema = schema_for!(NetworkNotification);
let _ = fs::File::create(format!(
"{}/schemas/docs/network.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&network_schema).unwrap());
let _ = fs::File::create(schema_path("network.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&network_schema).unwrap());

let workflow_schema = schema_for!(Workflow<'static, ()>);
let _ = fs::File::create(format!(
"{}/schemas/docs/workflow.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&workflow_schema).unwrap());
let _ = fs::File::create(schema_path("workflow.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&workflow_schema).unwrap());

let receipt_schema = schema_for!(Receipt<()>);
let _ = fs::File::create(format!(
"{}/schemas/docs/receipt.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_schema).unwrap());
let _ = fs::File::create(schema_path("receipt.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_schema).unwrap());

let receipt_notification_schema = schema_for!(ReceiptNotification);
let _ = fs::File::create(format!(
"{}/schemas/docs/receipt_notification.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_notification_schema).unwrap());
let _ = fs::File::create(schema_path("receipt_notification.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_notification_schema).unwrap());

let api_doc = generate_api_doc(
health_schema,
Expand All @@ -85,12 +69,9 @@ fn main() {
workflow_schema,
receipt_notification_schema,
);
let _ = fs::File::create(format!(
"{}/schemas/docs/api.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&api_doc).unwrap());
let _ = fs::File::create(schema_path("api.json"))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&api_doc).unwrap());
}

// Spec: https://github.com/open-rpc/spec/blob/1.2.6/spec.md
Expand Down Expand Up @@ -329,7 +310,7 @@ fn generate_api_doc(
title: "homestar".to_string(),
description: Some(env!("CARGO_PKG_DESCRIPTION").into()),
terms_of_service: None,
version: "0.10.0".to_string(),
version: "0.1.0".to_string(),
contact: Some(ContactObject {
name: None,
url: Some(env!("CARGO_PKG_REPOSITORY").into()),
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit ecc4e4f

Please sign in to comment.