Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for model from other libraries #2172

Merged
merged 16 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

8 changes: 3 additions & 5 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ fn create_stats_table(mut contracts_statistics: Vec<ContractStatistics>) -> Tabl

#[cfg(test)]
mod tests {
use camino::Utf8PathBuf;
use dojo_test_utils::compiler;
use dojo_test_utils::compiler::CompilerTestSetup;
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::{format, Cell, Row, Table};
use scarb::compiler::Profile;
Expand All @@ -227,10 +226,9 @@ mod tests {
// Uncomment once bindings support arrays.
#[test]
fn build_example_with_typescript_and_unity_bindings() {
let source_project_dir = Utf8PathBuf::from("../../examples/spawn-and-move/");
let dojo_core_path = Utf8PathBuf::from("../../crates/dojo-core");
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");

let config = compiler::copy_tmp_config(&source_project_dir, &dojo_core_path, Profile::DEV);
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let build_args = BuildArgs {
bindings_output: "generated".to_string(),
Expand Down
23 changes: 5 additions & 18 deletions bin/sozo/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl CleanArgs {
// which are normally built by the `build.rs` of `dojo-test-utils`.
#[cfg(test)]
mod tests {
use dojo_test_utils::compiler;
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_world::manifest::DEPLOYMENT_DIR;
use dojo_world::metadata::ABIS_DIR;
use scarb::compiler::Profile;
Expand All @@ -100,16 +100,8 @@ mod tests {

#[test]
fn default_clean_works() {
let source_project = "../../examples/spawn-and-move";
let dojo_core_path = "../../crates/dojo-core";

let config = compiler::copy_tmp_config(
&Utf8PathBuf::from(source_project),
&Utf8PathBuf::from(dojo_core_path),
Profile::DEV,
);

println!("path {:?}", config.manifest_path());
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let temp_project_dir = config.manifest_path().parent().unwrap().to_path_buf();

Expand Down Expand Up @@ -180,14 +172,9 @@ mod tests {

#[test]
fn all_profile_clean_works() {
let source_project = "../../examples/spawn-and-move";
let dojo_core_path = "../../crates/dojo-core";
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");

let config = compiler::copy_tmp_config(
&Utf8PathBuf::from(source_project),
&Utf8PathBuf::from(dojo_core_path),
Profile::DEV,
);
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let temp_project_dir = config.manifest_path().parent().unwrap().to_path_buf();

Expand Down
66 changes: 36 additions & 30 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,42 @@ impl MigrateArgs {
})?;

match self.command {
MigrateCommand::Plan => config.tokio_handle().block_on(async {
trace!(name, "Planning migration.");
migration::migrate(
&ws,
world_address,
rpc_url,
account,
&name,
true,
TxnConfig::default(),
dojo_metadata.skip_migration,
)
.await
}),
MigrateCommand::Apply { transaction } => config.tokio_handle().block_on(async {
trace!(name, "Applying migration.");
let txn_config: TxnConfig = transaction.into();

migration::migrate(
&ws,
world_address,
rpc_url,
account,
&name,
false,
txn_config,
dojo_metadata.skip_migration,
)
.await
}),
MigrateCommand::Plan => config
.tokio_handle()
.block_on(async {
trace!(name, "Planning migration.");
migration::migrate(
&ws,
world_address,
rpc_url,
account,
&name,
true,
TxnConfig::default(),
dojo_metadata.skip_migration,
)
.await
})
.map(|_| ()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt understand why this was added

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The outer function is expecting a result with () unit type, but in our case, we now return MigrationOutput, which in other places can be very useful to access.

Using .map(|_| ()) ensures that whatever is returned with Ok, we return the unit type discarding the returned value.

MigrateCommand::Apply { transaction } => config
.tokio_handle()
.block_on(async {
trace!(name, "Applying migration.");
let txn_config: TxnConfig = transaction.into();

migration::migrate(
&ws,
world_address,
rpc_url,
account,
&name,
false,
txn_config,
dojo_metadata.skip_migration,
)
.await
})
.map(|_| ()),
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions bin/sozo/tests/register_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod utils;

use camino::Utf8PathBuf;
use dojo_test_utils::compiler;
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_test_utils::migration::prepare_migration;
use dojo_world::metadata::{dojo_metadata_from_workspace, get_default_namespace_from_ws};
use dojo_world::migration::TxnConfig;
Expand All @@ -15,10 +14,8 @@ use utils::snapbox::get_snapbox;

#[tokio::test(flavor = "multi_thread")]
async fn reregister_models() {
let source_project_dir = Utf8PathBuf::from("../../examples/spawn-and-move/");
let dojo_core_path = Utf8PathBuf::from("../../crates/dojo-core");

let config = compiler::copy_tmp_config(&source_project_dir, &dojo_core_path, Profile::DEV);
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let ws = ops::read_workspace(config.manifest_path(), &config)
.unwrap_or_else(|op| panic!("Error building workspace: {op:?}"));
Expand All @@ -31,7 +28,7 @@ async fn reregister_models() {
let default_namespace = get_default_namespace_from_ws(&ws).unwrap();

let migration = prepare_migration(
source_project_dir.clone(),
config.manifest_path().parent().unwrap().into(),
target_path,
dojo_metadata.skip_migration,
&default_namespace,
Expand Down
11 changes: 4 additions & 7 deletions bin/sozo/tests/test_migrate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod utils;

use camino::Utf8PathBuf;
use dojo_test_utils::compiler;
use dojo_test_utils::compiler::CompilerTestSetup;
use katana_runner::KatanaRunner;
use scarb::compiler::Profile;
use starknet::accounts::Account;
Expand All @@ -10,10 +9,8 @@ use utils::snapbox::get_snapbox;

#[tokio::test(flavor = "multi_thread")]
async fn migrate_dry_run() {
let source_project_dir = Utf8PathBuf::from("../../examples/spawn-and-move/");
let dojo_core_path = Utf8PathBuf::from("../../crates/dojo-core");

let config = compiler::copy_tmp_config(&source_project_dir, &dojo_core_path, Profile::DEV);
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let sequencer = KatanaRunner::new().expect("Failed to start runner.");

Expand Down Expand Up @@ -45,7 +42,7 @@ async fn migrate_dry_run() {

assert!(output.contains("Migration Strategy"));
assert!(output.contains("# Base Contract"));
assert!(output.contains("# Models (8)"));
assert!(output.contains("# Models (10)"));
assert!(output.contains("# World"));
assert!(output.contains("# Contracts (4)"));
}
3 changes: 3 additions & 0 deletions crates/benches/contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ initializer_class_hash = "0xbeef"
# Default account for katana with seed = 0
account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"

[tool.dojo.world.namespace]
default = "benches"
25 changes: 12 additions & 13 deletions crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,33 +213,32 @@ fn filter_model_tokens(tokens: &TokenizedAbi) -> TokenizedAbi {

#[cfg(test)]
mod tests {
use dojo_test_utils::compiler;
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_world::metadata::dojo_metadata_from_workspace;
use scarb::compiler::Profile;

use super::*;

#[test]
fn gather_data_ok() {
let manifest_path = Utf8PathBuf::from("src/test_data/spawn-and-move/Scarb.toml");

let config = compiler::copy_tmp_config(
&Utf8PathBuf::from("../../examples/spawn-and-move"),
&Utf8PathBuf::from("../dojo-core"),
Profile::DEV,
);
let setup = CompilerTestSetup::from_examples("../dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();
let dojo_metadata = dojo_metadata_from_workspace(&ws).expect(
"No current package with dojo metadata found, bindgen is not yet supported for \
workspaces.",
);

let data =
gather_dojo_data(&manifest_path, "dojo_example", "dev", dojo_metadata.skip_migration)
.unwrap();

assert_eq!(data.models.len(), 8);
let data = gather_dojo_data(
&config.manifest_path().to_path_buf(),
"dojo_example",
"dev",
dojo_metadata.skip_migration,
)
.unwrap();

assert_eq!(data.models.len(), 10);

assert_eq!(data.world.name, "dojo_example");

Expand Down
21 changes: 10 additions & 11 deletions crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,7 @@ mod tests {
use std::fs;
use std::io::Read;

use camino::Utf8PathBuf;
use dojo_test_utils::compiler;
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_world::metadata::dojo_metadata_from_workspace;
use scarb::compiler::Profile;

Expand All @@ -647,21 +646,21 @@ mod tests {
let expected_output_without_header =
expected_output.lines().skip(1).collect::<Vec<&str>>().join("\n");

let manifest_path = Utf8PathBuf::from("src/test_data/spawn-and-move/Scarb.toml");
let config = compiler::copy_tmp_config(
&Utf8PathBuf::from("../../examples/spawn-and-move"),
&Utf8PathBuf::from("../dojo-core"),
Profile::DEV,
);
let setup = CompilerTestSetup::from_examples("../dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let ws = scarb::ops::read_workspace(config.manifest_path(), &config).unwrap();
let dojo_metadata = dojo_metadata_from_workspace(&ws).expect(
"No current package with dojo metadata found, bindgen is not yet support for \
workspaces.",
);
let data =
gather_dojo_data(&manifest_path, "dojo_examples", "dev", dojo_metadata.skip_migration)
.unwrap();
let data = gather_dojo_data(
&config.manifest_path().to_path_buf(),
"dojo_examples",
"dev",
dojo_metadata.skip_migration,
)
.unwrap();

let actual_output = TypeScriptV2Plugin::generate_code_content(&data);
let actual_output_without_header =
Expand Down
5 changes: 5 additions & 0 deletions crates/dojo-core/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ version = "0.7.3"
[dependencies]
dojo_plugin = { git = "https://github.com/dojoengine/dojo", tag = "v0.7.3" }
starknet = "=2.6.4"

# Dojo core is tested with sozo, hence we need a namespace for the test
# command to work.
[tool.dojo.world]
namespace = { default = "dojo" }
4 changes: 2 additions & 2 deletions crates/dojo-core/src/world_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct Foo {
}

#[derive(Copy, Drop, Serde)]
#[dojo::model(namespace: "another_namespace")]
#[dojo::model(namespace: "another_namespace", nomapping: true)]
struct Buzz {
#[key]
caller: ContractAddress,
Expand Down Expand Up @@ -940,7 +940,7 @@ trait IDojoInit<ContractState> {
#[dojo::contract]
mod test_contract {}

#[dojo::contract(namespace: "buzz_namespace")]
#[dojo::contract(namespace: "buzz_namespace", nomapping: true)]
mod buzz_contract {}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tracing.workspace = true
url.workspace = true

[dev-dependencies]
assert_fs.workspace = true
cairo-lang-semantic.workspace = true
cairo-lang-test-utils.workspace = true
dojo-test-utils = { path = "../dojo-test-utils" }
Expand Down
Loading