Skip to content

Commit

Permalink
Add new TypeScript bindgen (#1783)
Browse files Browse the repository at this point in the history
* feat: added new bindgen with all types and utility functions

* feat: removed bunch of extracted functions and extracted models to function

* refactor: moved generating to main function to intertwine models and contracts

* feat: generated system calls

* cleanup: removed old contract generation code

* feat: finished bindgen

* refactor: renamed TypescriptNew to TypeScriptV2

* Updated flag naming to typescript_v2

* Fixed some build and layout issues

* Fixed empty line in models

* Renamed modules as well

* Update crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs

Co-authored-by: glihm <[email protected]>

* Fixed formatting

* Updated return values

* Added query function to class

* Added test for the generated file

* Removed full file test for now until DojoData is sorted

* Added comment

* fix: move mocks to test_data

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
RareSecond and glihm authored Apr 15, 2024
1 parent 0227d37 commit 017c0d1
Show file tree
Hide file tree
Showing 5 changed files with 938 additions and 2 deletions.
16 changes: 14 additions & 2 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub struct BuildArgs {
#[arg(help = "Generate Typescript bindings.")]
pub typescript: bool,

#[arg(long)]
#[arg(help = "Generate Typescript bindings.")]
pub typescript_v2: bool,

#[arg(long)]
#[arg(help = "Generate Unity bindings.")]
pub unity: bool,
Expand All @@ -32,6 +36,10 @@ impl BuildArgs {
builtin_plugins.push(BuiltinPlugins::Typescript);
}

if self.typescript_v2 {
builtin_plugins.push(BuiltinPlugins::TypeScriptV2);
}

if self.unity {
builtin_plugins.push(BuiltinPlugins::Unity);
}
Expand Down Expand Up @@ -67,8 +75,12 @@ mod tests {
fn build_example_with_typescript_and_unity_bindings() {
let config = build_test_config("../../examples/spawn-and-move/Scarb.toml").unwrap();

let build_args =
BuildArgs { bindings_output: "generated".to_string(), typescript: true, unity: true };
let build_args = BuildArgs {
bindings_output: "generated".to_string(),
typescript: true,
unity: true,
typescript_v2: true,
};
let result = build_args.run(&config);
assert!(result.is_ok());
}
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use error::{BindgenResult, Error};

mod plugins;
use plugins::typescript::TypescriptPlugin;
use plugins::typescript_v2::TypeScriptV2Plugin;
use plugins::unity::UnityPlugin;
use plugins::BuiltinPlugin;
pub use plugins::BuiltinPlugins;
Expand Down Expand Up @@ -85,6 +86,7 @@ impl PluginManager {
let builder: Box<dyn BuiltinPlugin> = match plugin {
BuiltinPlugins::Typescript => Box::new(TypescriptPlugin::new()),
BuiltinPlugins::Unity => Box::new(UnityPlugin::new()),
BuiltinPlugins::TypeScriptV2 => Box::new(TypeScriptV2Plugin::new()),
};

let files = builder.generate_code(&data).await?;
Expand Down
3 changes: 3 additions & 0 deletions crates/dojo-bindgen/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ use crate::error::BindgenResult;
use crate::DojoData;

pub mod typescript;
pub mod typescript_v2;
pub mod unity;

#[derive(Debug)]
pub enum BuiltinPlugins {
Typescript,
Unity,
TypeScriptV2,
}

impl fmt::Display for BuiltinPlugins {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BuiltinPlugins::Typescript => write!(f, "typescript"),
BuiltinPlugins::Unity => write!(f, "unity"),
BuiltinPlugins::TypeScriptV2 => write!(f, "typescript_v2"),
}
}
}
Expand Down
Loading

0 comments on commit 017c0d1

Please sign in to comment.