Skip to content

Commit

Permalink
refactor: update manifest structure (#2153)
Browse files Browse the repository at this point in the history
* update manifest structure

* fix abi path in manifest

* fix test

* fix: fix typo and remove dbg

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
lambda-0x and glihm authored Jul 8, 2024
1 parent ff30e1d commit 7f5514d
Show file tree
Hide file tree
Showing 84 changed files with 224 additions and 104 deletions.
55 changes: 30 additions & 25 deletions bin/sozo/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use anyhow::{Context, Result};
use camino::Utf8PathBuf;
use clap::Args;
use dojo_world::manifest::{ABIS_DIR, BASE_DIR, MANIFESTS_DIR};
use dojo_world::manifest::{BASE_DIR, MANIFESTS_DIR};
use scarb::core::Config;
use scarb::ops;
use tracing::trace;
Expand All @@ -28,7 +28,7 @@ impl CleanArgs {
/// * `profile_dir` - The directory where the profile files are located.
pub fn clean_manifests(profile_dir: &Utf8PathBuf) -> Result<()> {
trace!(?profile_dir, "Cleaning manifests.");
let dirs = vec![profile_dir.join(BASE_DIR), profile_dir.join(ABIS_DIR).join(BASE_DIR)];
let dirs = vec![profile_dir.join(BASE_DIR)];

for d in dirs {
if d.exists() {
Expand Down Expand Up @@ -96,6 +96,8 @@ impl CleanArgs {
#[cfg(test)]
mod tests {
use dojo_test_utils::compiler;
use dojo_world::manifest::DEPLOYMENT_DIR;
use dojo_world::metadata::ABIS_DIR;
use scarb::compiler::Profile;

use super::*;
Expand All @@ -111,6 +113,8 @@ mod tests {
Profile::DEV,
);

println!("path {:?}", config.manifest_path());

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

let clean_cmd = CleanArgs { full: false, all_profiles: false };
Expand All @@ -126,14 +130,14 @@ mod tests {
let release_manifests_dir = temp_project_dir.join("manifests").join(release_profile_name);

let dev_manifests_base_dir = dev_manifests_dir.join("base");
let dev_manifests_abis_base_dir = dev_manifests_dir.join("abis").join("base");
let dev_manifests_abis_base_dir = dev_manifests_dir.join("base").join("abis");
let release_manifests_base_dir = release_manifests_dir.join("base");
let release_manifests_abis_base_dir = release_manifests_dir.join("abis").join("base");

let dev_manifests_abis_depl_dir = dev_manifests_dir.join("abis").join("deployments");
let release_manifests_abis_base_dir = release_manifests_dir.join("base").join("abis");

let dev_manifest_toml = dev_manifests_dir.join("manifest").with_extension("toml");
let dev_manifest_json = dev_manifests_dir.join("manifest").with_extension("json");
let dev_manifests_depl_dir = dev_manifests_dir.join("deployment");
let dev_manifests_abis_depl_dir = dev_manifests_depl_dir.join("abis");
let dev_manifest_toml = dev_manifests_depl_dir.join("manifest").with_extension("toml");
let dev_manifest_json = dev_manifests_depl_dir.join("manifest").with_extension("json");

assert!(fs::read_dir(target_dev_dir).is_err(), "Expected 'target/dev' to be empty");
assert!(
Expand All @@ -147,11 +151,11 @@ mod tests {
);
assert!(
fs::read_dir(dev_manifests_abis_base_dir).is_err(),
"Expected 'manifests/dev/abis/base' to be empty"
"Expected 'manifests/dev/base/abis' to be empty"
);
assert!(
fs::read_dir(&dev_manifests_abis_depl_dir).is_ok(),
"Expected 'manifests/dev/abis/deployments' to not be empty"
"Expected 'manifests/dev/deployment/abis' to not be empty"
);

// we expect release profile to be not affected
Expand All @@ -161,7 +165,7 @@ mod tests {
);
assert!(
fs::read_dir(release_manifests_abis_base_dir).is_ok(),
"Expected 'manifests/release/abis/base' to be non empty"
"Expected 'manifests/release/base/abis' to be non empty"
);

assert!(dev_manifest_toml.exists(), "Expected 'manifest.toml' to exist");
Expand All @@ -172,7 +176,7 @@ mod tests {

assert!(
fs::read_dir(&dev_manifests_abis_depl_dir).is_err(),
"Expected 'manifests/dev/abis/deployments' to be empty"
"Expected 'manifests/dev/deployment/abis' to be empty"
);
assert!(!dev_manifest_toml.exists(), "Expected 'manifest.toml' to not exist");
assert!(!dev_manifest_json.exists(), "Expected 'manifest.json' to not exist");
Expand Down Expand Up @@ -200,18 +204,19 @@ mod tests {
let target_dev_dir = temp_project_dir.join("target").join(dev_profile_name);
let target_release_dir = temp_project_dir.join("target").join(release_profile_name);

let dev_manifests_dir = temp_project_dir.join("manifests").join(dev_profile_name);
let release_manifests_dir = temp_project_dir.join("manifests").join(release_profile_name);
let dev_manifests_dir = temp_project_dir.join(MANIFESTS_DIR).join(dev_profile_name);
let release_manifests_dir = temp_project_dir.join(MANIFESTS_DIR).join(release_profile_name);

let dev_manifests_base_dir = dev_manifests_dir.join("base");
let dev_manifests_abis_base_dir = dev_manifests_dir.join("abis").join("base");
let release_manifests_base_dir = release_manifests_dir.join("base");
let release_manifests_abis_base_dir = release_manifests_dir.join("abis").join("base");
let dev_manifests_base_dir = dev_manifests_dir.join(BASE_DIR);
let dev_manifests_abis_base_dir = dev_manifests_base_dir.join(ABIS_DIR);
let release_manifests_base_dir = release_manifests_dir.join(BASE_DIR);
let release_manifests_abis_base_dir = release_manifests_base_dir.join(ABIS_DIR);

let dev_manifests_abis_depl_dir = dev_manifests_dir.join("abis").join("deployments");
let dev_manifests_deploy_dir = dev_manifests_dir.join(DEPLOYMENT_DIR);
let dev_manifests_abis_depl_dir = dev_manifests_deploy_dir.join(ABIS_DIR);

let dev_manifest_toml = dev_manifests_dir.join("manifest").with_extension("toml");
let dev_manifest_json = dev_manifests_dir.join("manifest").with_extension("json");
let dev_manifest_toml = dev_manifests_deploy_dir.join("manifest").with_extension("toml");
let dev_manifest_json = dev_manifests_deploy_dir.join("manifest").with_extension("json");

assert!(fs::read_dir(target_dev_dir).is_err(), "Expected 'target/dev' to be empty");
assert!(fs::read_dir(target_release_dir).is_err(), "Expected 'target/release' to be empty");
Expand All @@ -222,11 +227,11 @@ mod tests {
);
assert!(
fs::read_dir(dev_manifests_abis_base_dir).is_err(),
"Expected 'manifests/dev/abis/base' to be empty"
"Expected 'manifests/dev/base/abis' to be empty"
);
assert!(
fs::read_dir(&dev_manifests_abis_depl_dir).is_ok(),
"Expected 'manifests/dev/abis/deployments' to not be empty"
"Expected 'manifests/dev/deployment/abis' to not be empty"
);

assert!(
Expand All @@ -235,7 +240,7 @@ mod tests {
);
assert!(
fs::read_dir(release_manifests_abis_base_dir).is_err(),
"Expected 'manifests/release/abis/base' to be empty"
"Expected 'manifests/release/base/abis' to be empty"
);

assert!(dev_manifest_toml.exists(), "Expected 'manifest.toml' to exist");
Expand All @@ -246,7 +251,7 @@ mod tests {

assert!(
fs::read_dir(&dev_manifests_abis_depl_dir).is_err(),
"Expected 'manifests/dev/abis/deployments' to be empty"
"Expected 'manifests/dev/deployment/abis' to be empty"
);
assert!(!dev_manifest_toml.exists(), "Expected 'manifest.toml' to not exist");
assert!(!dev_manifest_json.exists(), "Expected 'manifest.json' to not exist");
Expand Down
4 changes: 3 additions & 1 deletion bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ impl MigrateArgs {
None
};

let profile_name =
ws.current_profile().expect("Scarb profile expected to be defined.").to_string();
let manifest_dir = ws.manifest_path().parent().unwrap().to_path_buf();
if !manifest_dir.join(MANIFESTS_DIR).exists() {
if !manifest_dir.join(MANIFESTS_DIR).join(profile_name).exists() {
return Err(anyhow!("Build project using `sozo build` first"));
}

Expand Down
4 changes: 2 additions & 2 deletions crates/benches/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{anyhow, bail, Context, Ok, Result};
use clap::Parser;
use dojo_lang::compiler::DojoCompiler;
use dojo_lang::plugin::CairoPluginRepository;
use dojo_world::manifest::{DeploymentManifest, DEPLOYMENTS_DIR, MANIFESTS_DIR};
use dojo_world::manifest::{DeploymentManifest, DEPLOYMENT_DIR, MANIFESTS_DIR};
use futures::executor::block_on;
use katana_runner::KatanaRunner;
use scarb::compiler::CompilerRepository;
Expand Down Expand Up @@ -97,7 +97,7 @@ async fn prepare_migration_args(args: SozoArgs) -> Result<Felt> {
let manifest_dir = manifest_path.parent().unwrap();

let manifest = DeploymentManifest::load_from_path(
&manifest_dir.join(MANIFESTS_DIR).join("dev").join(DEPLOYMENTS_DIR).with_extension("toml"),
&manifest_dir.join(MANIFESTS_DIR).join("dev").join(DEPLOYMENT_DIR).with_extension("toml"),
)
.expect("failed to load manifest");

Expand Down
20 changes: 11 additions & 9 deletions crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,12 @@ fn update_files(
) -> anyhow::Result<()> {
let profile_name =
ws.current_profile().expect("Scarb profile expected to be defined.").to_string();
let profile_dir = Utf8PathBuf::new().join(MANIFESTS_DIR).join(profile_name);
let relative_manifest_dir = Utf8PathBuf::new().join(MANIFESTS_DIR).join(profile_name);

// relative path to manifests and abi
let base_manifests_dir = Utf8PathBuf::new().join(relative_manifest_dir).join(BASE_DIR);
let base_abis_dir = Utf8PathBuf::new().join(&base_manifests_dir).join(ABIS_DIR);

let relative_manifests_dir = Utf8PathBuf::new().join(&profile_dir).join(BASE_DIR);
let relative_abis_dir = Utf8PathBuf::new().join(&profile_dir).join(ABIS_DIR).join(BASE_DIR);
let manifest_dir = ws.manifest_path().parent().unwrap().to_path_buf();

fn get_compiled_artifact_from_map<'a>(
Expand All @@ -247,8 +249,8 @@ fn update_files(
let (hash, class) = get_compiled_artifact_from_map(&compiled_artifacts, qualified_path)?;
let filename = naming::get_filename_from_tag(tag);
write_manifest_and_abi(
&relative_manifests_dir,
&relative_abis_dir,
&base_manifests_dir,
&base_abis_dir,
&manifest_dir,
&mut Manifest::new(
// abi path will be written by `write_manifest`
Expand Down Expand Up @@ -334,8 +336,8 @@ fn update_files(

for (_, (manifest, class, module_id)) in contracts.iter_mut() {
write_manifest_and_abi(
&relative_manifests_dir.join(CONTRACTS_DIR),
&relative_abis_dir.join(CONTRACTS_DIR),
&base_manifests_dir.join(CONTRACTS_DIR),
&base_abis_dir.join(CONTRACTS_DIR),
&manifest_dir,
manifest,
&class.abi,
Expand All @@ -360,8 +362,8 @@ fn update_files(

for (_, (manifest, class, module_id)) in models.iter_mut() {
write_manifest_and_abi(
&relative_manifests_dir.join(MODELS_DIR),
&relative_abis_dir.join(MODELS_DIR),
&base_manifests_dir.join(MODELS_DIR),
&base_abis_dir.join(MODELS_DIR),
&manifest_dir,
manifest,
&class.abi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kind = "DojoContract"
class_hash = "0x7468fbf6e47eb66fd898a4a68bbe801560fdd42b0d6909ec4f75fb38c613702"
original_class_hash = "0x7468fbf6e47eb66fd898a4a68bbe801560fdd42b0d6909ec4f75fb38c613702"
base_class_hash = "0x0"
abi = "manifests/dev/abis/base/contracts/ccf-cairo_v240-8d921297.json"
abi = "manifests/dev/base/abis/contracts/ccf-cairo_v240-8d921297.json"
reads = []
writes = []
computed = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kind = "DojoContract"
class_hash = "0x4bbfcdc8d95ecfa332201e21f615e98b4d12e08f77d176761e32bb34e3bc333"
original_class_hash = "0x4bbfcdc8d95ecfa332201e21f615e98b4d12e08f77d176761e32bb34e3bc333"
base_class_hash = "0x0"
abi = "manifests/dev/abis/base/contracts/ccf-cairo_v260-465ec7fe.json"
abi = "manifests/dev/base/abis/contracts/ccf-cairo_v260-465ec7fe.json"
reads = []
writes = []
computed = []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x22f3e55b61d86c2ac5239fa3b3b8761f26b9a5c0b5f61ddbd5d756ced498b46"
original_class_hash = "0x22f3e55b61d86c2ac5239fa3b3b8761f26b9a5c0b5f61ddbd5d756ced498b46"
abi = "manifests/dev/abis/base/dojo-base.json"
abi = "manifests/dev/base/abis/dojo-base.json"
tag = "dojo-base"
manifest_name = "dojo-base"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x1498dd1197805ec05d37da956d0fc568023a4c25578b0523b4f4f0d0e4f16c2"
original_class_hash = "0x1498dd1197805ec05d37da956d0fc568023a4c25578b0523b4f4f0d0e4f16c2"
abi = "manifests/dev/abis/base/dojo-world.json"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
10 changes: 5 additions & 5 deletions crates/dojo-world/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub const WORLD_QUALIFIED_PATH: &str = "dojo::world::world";
pub const BASE_QUALIFIED_PATH: &str = "dojo::base::base";

pub const MANIFESTS_DIR: &str = "manifests";
pub const DEPLOYMENT_DIR: &str = "deployment";
pub const TARGET_DIR: &str = "target";
pub const BASE_DIR: &str = "base";
pub const OVERLAYS_DIR: &str = "overlays";
pub const DEPLOYMENTS_DIR: &str = "deployments";
pub const ABIS_DIR: &str = "abis";

pub const CONTRACTS_DIR: &str = "contracts";
Expand Down Expand Up @@ -366,25 +366,25 @@ impl DeploymentManifest {
Ok(())
}

pub fn write_to_path_json(&self, path: &Utf8PathBuf, profile_dir: &Utf8PathBuf) -> Result<()> {
pub fn write_to_path_json(&self, path: &Utf8PathBuf, root_dir: &Utf8PathBuf) -> Result<()> {
fs::create_dir_all(path.parent().unwrap())?;

// Embedding ABIs into the manifest.
let mut manifest_with_abis = self.clone();

if let Some(abi_format) = &manifest_with_abis.world.inner.abi {
manifest_with_abis.world.inner.abi = Some(abi_format.to_embed(profile_dir)?);
manifest_with_abis.world.inner.abi = Some(abi_format.to_embed(root_dir)?);
}

for contract in &mut manifest_with_abis.contracts {
if let Some(abi_format) = &contract.inner.abi {
contract.inner.abi = Some(abi_format.to_embed(profile_dir)?);
contract.inner.abi = Some(abi_format.to_embed(root_dir)?);
}
}

for model in &mut manifest_with_abis.models {
if let Some(abi_format) = &model.inner.abi {
model.inner.abi = Some(abi_format.to_embed(profile_dir)?);
model.inner.abi = Some(abi_format.to_embed(root_dir)?);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-world/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Result<DojoMetadata>

let manifest_dir = ws.manifest_path().parent().unwrap().to_path_buf();
let manifest_dir = manifest_dir.join(MANIFESTS_DIR).join(profile.as_str());
let abi_dir = manifest_dir.join(ABIS_DIR).join(BASE_DIR);
let abi_dir = manifest_dir.join(BASE_DIR).join(ABIS_DIR);
let source_dir = ws.target_dir().path_existent().unwrap();
let source_dir = source_dir.join(profile.as_str());

Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-world/src/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async fn get_full_dojo_metadata_from_workspace() {
let manifest_dir = manifest_dir.join(MANIFESTS_DIR).join(profile.as_str());
let target_dir = ws.target_dir().path_existent().unwrap();
let target_dir = target_dir.join(profile.as_str());
let abis_dir = manifest_dir.join(ABIS_DIR).join(BASE_DIR);
let abis_dir = manifest_dir.join(BASE_DIR).join(ABIS_DIR);

let dojo_metadata =
dojo_metadata_from_workspace(&ws).expect("No current package with dojo metadata found.");
Expand Down
Loading

0 comments on commit 7f5514d

Please sign in to comment.