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 7 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
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.

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 @@
})?;

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(|_| ()),

Check warning on line 126 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L108-L126

Added lines #L108 - L126 were not covered by tests
}
}
}
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)"));
}
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 @@
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 @@
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);

Check warning on line 650 in crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs#L649-L650

Added lines #L649 - L650 were not covered by tests

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();

Check warning on line 663 in crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs#L657-L663

Added lines #L657 - L663 were not covered by tests

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" }
16 changes: 9 additions & 7 deletions crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use dojo_world::manifest::{
ABIS_DIR, BASE_CONTRACT_TAG, BASE_DIR, BASE_QUALIFIED_PATH, CONTRACTS_DIR, MANIFESTS_DIR,
MODELS_DIR, WORLD_CONTRACT_TAG, WORLD_QUALIFIED_PATH,
};
use dojo_world::metadata::get_default_namespace_from_ws;
use dojo_world::metadata::get_namespace_config_from_ws;
use itertools::Itertools;
use scarb::compiler::helpers::{build_compiler_config, collect_main_crate_ids};
use scarb::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
Expand Down Expand Up @@ -88,8 +88,6 @@ impl Compiler for DojoCompiler {
let props: Props = unit.main_component().target_props()?;
let target_dir = unit.target_dir(ws);

let default_namespace = get_default_namespace_from_ws(ws)?;

let compiler_config = build_compiler_config(&unit, ws);

let mut main_crate_ids = collect_main_crate_ids(&unit, db);
Expand Down Expand Up @@ -137,7 +135,6 @@ impl Compiler for DojoCompiler {
&main_crate_ids,
compiled_classes,
props.build_external_contracts,
&default_namespace,
)?;
Ok(())
}
Expand Down Expand Up @@ -169,15 +166,18 @@ fn find_project_contracts(
.map(|selector| selector.package().into())
.unique()
.map(|package_name: SmolStr| {
debug!(target: LOG_TARGET, %package_name, "Looking for internal crates.");
db.upcast_mut().intern_crate(CrateLongId::Real(package_name))
})
.collect::<Vec<_>>();

find_contracts(db, crate_ids.as_ref())
.into_iter()
.filter(|decl| {
external_contracts.iter().any(|selector| {
let contract_path = decl.module_id().full_path(db.upcast());
contract_path == selector.full_path()
// Snake case is used to ensure we match the `compile()` output.
contract_path == selector.full_path().to_case(Case::Snake)
})
})
.collect::<Vec<ContractDeclaration>>()
Expand Down Expand Up @@ -220,8 +220,9 @@ fn update_files(
crate_ids: &[CrateId],
compiled_artifacts: HashMap<String, (Felt, ContractClass)>,
external_contracts: Option<Vec<ContractSelector>>,
default_namespace: &str,
) -> anyhow::Result<()> {
let namespace_config = get_namespace_config_from_ws(ws)?;

let profile_name =
ws.current_profile().expect("Scarb profile expected to be defined.").to_string();
let relative_manifest_dir = Utf8PathBuf::new().join(MANIFESTS_DIR).join(profile_name);
Expand All @@ -244,6 +245,7 @@ fn update_files(

let mut crate_ids = crate_ids.to_vec();

// World and base contracts from Dojo core.
for (qualified_path, tag) in
[(WORLD_QUALIFIED_PATH, WORLD_CONTRACT_TAG), (BASE_QUALIFIED_PATH, BASE_CONTRACT_TAG)]
{
Expand Down Expand Up @@ -308,7 +310,7 @@ fn update_files(
contracts.extend(get_dojo_contract_artifacts(
db,
module_id,
&naming::get_tag(default_namespace, &aux_data.contract_name),
&naming::get_tag(&namespace_config.default, &aux_data.contract_name),
&compiled_artifacts,
)?);
}
Expand Down
Loading
Loading