Skip to content

Commit

Permalink
improve typegen error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Dec 3, 2023
1 parent 5c62ed8 commit dd1d07f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 92 deletions.
37 changes: 18 additions & 19 deletions crux_core/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,22 @@
//!use uuid::Uuid;
//!
//!#[test]
//!fn generate_types() {
//!fn generate_types() -> anyhow::Result<()> {
//! let mut gen = TypeGen::new();
//!
//! let sample_events = vec![Event::SendUuid(Uuid::new_v4())];
//! gen.register_type_with_samples(sample_events).unwrap();
//! gen.register_type_with_samples(sample_events)?;
//!
//! gen.register_app::<App>().unwrap();
//! gen.register_app::<App>()?;
//!
//! let temp = assert_fs::TempDir::new().unwrap();
//! let temp = assert_fs::TempDir::new()?;
//! let output_root = temp.join("crux_core_typegen_test");
//!
//! gen.swift("SharedTypes", output_root.join("swift"))
//! .expect("swift type gen failed");
//! gen.swift("SharedTypes", output_root.join("swift"))?;
//!
//! gen.java("com.example.counter.shared_types", output_root.join("java"))
//! .expect("java type gen failed");
//! gen.java("com.example.counter.shared_types", output_root.join("java"))?;
//!
//! gen.typescript("shared_types", output_root.join("typescript"))
//! .expect("typescript type gen failed");
//! gen.typescript("shared_types", output_root.join("typescript"))?;
//!}
//! ```
Expand Down Expand Up @@ -110,6 +107,8 @@ pub enum TypeGenError {
Generation(String),
#[error("error writing generated types")]
Io(#[from] std::io::Error),
#[error("`pnpm` is needed for TypeScript type generation, but it could not be found in PATH.\nPlease install it from https://pnpm.io/installation")]
PnpmNotFound(#[source] std::io::Error),
}

#[derive(Debug)]
Expand Down Expand Up @@ -309,8 +308,7 @@ impl TypeGen {
/// # use std::env::temp_dir;
/// # let mut gen = TypeGen::new();
/// # let output_root = temp_dir().join("crux_core_typegen_doctest");
/// gen.swift("SharedTypes", output_root.join("swift"))
/// .expect("swift type gen failed");
/// gen.swift("SharedTypes", output_root.join("swift"))?;
/// ```
pub fn swift(&mut self, module_name: &str, path: impl AsRef<Path>) -> Result {
self.ensure_registry()?;
Expand Down Expand Up @@ -373,8 +371,7 @@ impl TypeGen {
/// gen.java(
/// "com.redbadger.crux_core.shared_types",
/// output_root.join("java"),
/// )
/// .expect("java type gen failed");
/// )?;
/// ```
pub fn java(&mut self, package_name: &str, path: impl AsRef<Path>) -> Result {
self.ensure_registry()?;
Expand Down Expand Up @@ -429,8 +426,7 @@ impl TypeGen {
/// # use std::env::temp_dir;
/// # let mut gen = TypeGen::new();
/// # let output_root = temp_dir().join("crux_core_typegen_doctest");
/// gen.typescript("shared_types", output_root.join("typescript"))
/// .expect("typescript type gen failed");
/// gen.typescript("shared_types", output_root.join("typescript"))?;
/// ```
pub fn typescript(&mut self, module_name: &str, path: impl AsRef<Path>) -> Result {
self.ensure_registry()?;
Expand All @@ -448,7 +444,7 @@ impl TypeGen {

let extensions_dir =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("typegen_extensions/typescript");
copy(extensions_dir, path).expect("Could not copy TS runtime");
copy(extensions_dir, path)?;

let registry = match &self.state {
State::Generating(registry) => registry,
Expand Down Expand Up @@ -481,7 +477,10 @@ impl TypeGen {
.current_dir(output_dir.clone())
.arg("install")
.status()
.expect("Could not pnpm install");
.map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => TypeGenError::PnpmNotFound(e),
_ => TypeGenError::Io(e),
})?;

// Build TS code and emit declarations
std::process::Command::new("pnpm")
Expand All @@ -490,7 +489,7 @@ impl TypeGen {
.arg("tsc")
.arg("--build")
.status()
.expect("Could tsc --build");
.map_err(TypeGenError::Io)?;

Ok(())
}
Expand Down
15 changes: 7 additions & 8 deletions examples/bridge_echo/shared_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ use crux_core::typegen::TypeGen;
use shared::App;
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

gen.register_app::<App>().expect("register");
gen.register_app::<App>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
gen.swift("SharedTypes", output_root.join("swift"))?;

gen.java(
"com.example.bridge_echo.shared_types",
output_root.join("java"),
)
.expect("java type gen failed");
)?;

gen.typescript("shared_types", output_root.join("typescript"))
.expect("typescript type gen failed");
gen.typescript("shared_types", output_root.join("typescript"))?;

Ok(())
}
15 changes: 7 additions & 8 deletions examples/cat_facts/shared_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ use crux_core::typegen::TypeGen;
use shared::CatFacts;
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

gen.register_app::<CatFacts>().expect("register");
gen.register_app::<CatFacts>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
gen.swift("SharedTypes", output_root.join("swift"))?;

gen.java(
"com.redbadger.catfacts.shared_types",
output_root.join("java"),
)
.expect("java type gen failed");
)?;

gen.typescript("shared_types", output_root.join("typescript"))
.expect("typescript type gen failed");
gen.typescript("shared_types", output_root.join("typescript"))?;

Ok(())
}
15 changes: 7 additions & 8 deletions examples/counter/shared_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ use crux_core::typegen::TypeGen;
use shared::App;
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

gen.register_app::<App>().expect("register");
gen.register_app::<App>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
gen.swift("SharedTypes", output_root.join("swift"))?;

gen.java("com.example.counter.shared_types", output_root.join("java"))
.expect("java type gen failed");
gen.java("com.example.counter.shared_types", output_root.join("java"))?;

gen.typescript("shared_types", output_root.join("typescript"))
.expect("typescript type gen failed");
gen.typescript("shared_types", output_root.join("typescript"))?;

Ok(())
}
17 changes: 8 additions & 9 deletions examples/notes/shared_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ use crux_core::typegen::TypeGen;
use shared::{NoteEditor, TextCursor};
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

gen.register_app::<NoteEditor>().expect("register");
gen.register_app::<NoteEditor>()?;

// Note: currently required as we can't find enums inside enums, see:
// https://github.com/zefchain/serde-reflection/tree/main/serde-reflection#supported-features
gen.register_type::<TextCursor>().expect("register");
gen.register_type::<TextCursor>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
gen.swift("SharedTypes", output_root.join("swift"))?;

// TODO these are for later
//
// gen.java("com.example.counter.shared_types", output_root.join("java"))
// .expect("java type gen failed");
// gen.java("com.example.counter.shared_types", output_root.join("java"))?;

gen.typescript("shared_types", output_root.join("typescript"))
.expect("typescript type gen failed");
gen.typescript("shared_types", output_root.join("typescript"))?;

Ok(())
}
15 changes: 7 additions & 8 deletions examples/simple_counter/shared_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ use crux_core::typegen::TypeGen;
use shared::Counter;
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

gen.register_app::<Counter>().expect("register");
gen.register_app::<Counter>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
gen.swift("SharedTypes", output_root.join("swift"))?;

gen.java(
"com.example.simple_counter.shared_types",
output_root.join("java"),
)
.expect("java type gen failed");
)?;

gen.typescript("shared_types", output_root.join("typescript"))
.expect("typescript type gen failed");
gen.typescript("shared_types", output_root.join("typescript"))?;

Ok(())
}
1 change: 1 addition & 0 deletions examples/tap_to_pay/shared/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum Screen {

#[derive(Effect)]
#[effect(app = "App")]
#[cfg_attr(feature = "typegen", derive(crux_macros::Export))]
pub struct Capabilities {
render: Render<Event>,
delay: Delay<Event>,
Expand Down
32 changes: 8 additions & 24 deletions examples/tap_to_pay/shared_types/build.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
use anyhow::Result;
use crux_core::{bridge::Request, typegen::TypeGen};
use crux_http::protocol::{HttpRequest, HttpResponse};
use shared::{EffectFfi, Event, Payment, PaymentStatus, Receipt, ReceiptStatus, ViewModel};
use crux_core::typegen::TypeGen;
use shared::{App, PaymentStatus, ReceiptStatus};
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

register_types(&mut gen).expect("type registration failed");
gen.register_app::<App>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
}

fn register_types(gen: &mut TypeGen) -> Result<()> {
gen.register_type::<Request<EffectFfi>>()?;

gen.register_type::<EffectFfi>()?;
gen.register_type::<HttpRequest>()?;

gen.register_type::<Event>()?;
gen.register_type::<HttpResponse>()?;

gen.register_type::<ViewModel>()?;
gen.register_type::<Payment>()?;
gen.register_type::<PaymentStatus>()?;
gen.register_type::<Receipt>()?;
gen.register_type::<ReceiptStatus>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))?;

Ok(())
}
15 changes: 7 additions & 8 deletions templates/simple_counter/shared_types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ use crux_core::typegen::TypeGen;
use {{core_name}}::Counter;
use std::path::PathBuf;

fn main() {
fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=../shared");

let mut gen = TypeGen::new();

gen.register_app::<Counter>().expect("register");
gen.register_app::<Counter>()?;

let output_root = PathBuf::from("./generated");

gen.swift("SharedTypes", output_root.join("swift"))
.expect("swift type gen failed");
gen.swift("SharedTypes", output_root.join("swift"))?;

gen.java(
"com.example.simple_counter.shared_types",
output_root.join("java"),
)
.expect("java type gen failed");
)?;

gen.typescript("shared_types", output_root.join("typescript"))
.expect("typescript type gen failed");
gen.typescript("shared_types", output_root.join("typescript"))?;

Ok(())
}

0 comments on commit dd1d07f

Please sign in to comment.