Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Jul 24, 2024
1 parent 87b754b commit f6eef30
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 121 deletions.
4 changes: 2 additions & 2 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl BuildArgs {
};

let dojo_metadata = if packages.len() == 1 {
let package = packages.iter().next().unwrap();
dojo_metadata_from_package(&package, &ws)?
let package = packages.first().unwrap();
dojo_metadata_from_package(package, &ws)?
} else {
dojo_metadata_from_workspace(&ws)?
};
Expand Down
2 changes: 2 additions & 0 deletions bin/sozo/src/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ impl TestArgs {
opts.include_target_kinds.is_empty()
|| opts.include_target_kinds.contains(&cu.main_component().target_kind())
})
// TODOL: Need to find how to filter from packages with the compilation unit. We need something
// implementing PackagesSource trait.
.collect::<Vec<_>>();

for unit in compilation_units {
Expand Down
131 changes: 12 additions & 119 deletions crates/dojo-world/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ use std::collections::HashMap;
use std::io::Cursor;
use std::path::PathBuf;

use anyhow::{anyhow, Context, Result};
use camino::Utf8PathBuf;
use anyhow::{Context, Result};
use ipfs_api_backend_hyper::{IpfsApi, IpfsClient, TryFromUri};
use regex::Regex;
use scarb::core::{ManifestMetadata, Package, TargetKind, Workspace};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::json;
use url::Url;

use crate::contracts::naming;
use crate::manifest::{BaseManifest, CONTRACTS_DIR, MODELS_DIR, WORLD_CONTRACT_TAG};

const LOG_TARGET: &str = "dojo-world::metadata";
const LOG_TARGET: &str = "dojo_world::metadata";

#[cfg(test)]
#[path = "metadata_test.rs"]
Expand All @@ -29,24 +25,6 @@ pub const MANIFESTS_DIR: &str = "manifests";
pub const ABIS_DIR: &str = "abis";
pub const BASE_DIR: &str = "base";

fn build_artifact_from_filename(
abi_dir: &Utf8PathBuf,
source_dir: &Utf8PathBuf,
filename: &str,
) -> ArtifactMetadata {
let abi_file = abi_dir.join(format!("{filename}.json"));
let src_file = source_dir.join(format!("{filename}.cairo"));

ArtifactMetadata {
abi: if abi_file.exists() { Some(Uri::File(abi_file.into_std_path_buf())) } else { None },
source: if src_file.exists() {
Some(Uri::File(src_file.into_std_path_buf()))
} else {
None
},
}
}

/// Get the default namespace from the workspace.
///
/// # Arguments
Expand Down Expand Up @@ -101,6 +79,12 @@ pub fn project_to_world_metadata(m: ProjectWorldMetadata) -> WorldMetadata {
pub fn dojo_metadata_from_package(package: &Package, ws: &Workspace<'_>) -> Result<DojoMetadata> {
tracing::debug!(target: LOG_TARGET, package_id = package.id.to_string(), "Collecting Dojo metadata from package.");

if package.target(&TargetKind::new("lib")).is_some()
|| package.target(&TargetKind::new("dojo")).is_none()
{
return Ok(DojoMetadata::default());
}

let project_metadata = package
.manifest
.metadata
Expand All @@ -122,10 +106,9 @@ pub fn dojo_metadata_from_package(package: &Package, ws: &Workspace<'_>) -> Resu
pub fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Result<DojoMetadata> {
let dojo_packages: Vec<Package> = ws
.members()
.into_iter()
.filter(|package| {
package.target(&TargetKind::new("dojo")).is_some()
&& !package.target(&TargetKind::new("lib")).is_some()
&& package.target(&TargetKind::new("lib")).is_none()
})
.collect();

Expand All @@ -140,105 +123,15 @@ pub fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Result<DojoMetadata>
1 => {
let dojo_package =
dojo_packages.into_iter().next().expect("Package must exist as len is 1.");
Ok(dojo_metadata_from_package(&dojo_package, &ws)?)
Ok(dojo_metadata_from_package(&dojo_package, ws)?)
}
_ => {
return Err(anyhow::anyhow!(
Err(anyhow::anyhow!(
"Multiple packages with dojo target found in workspace. Please specify a package \
using --package option or maybe one of them must be declared as a [lib]."
));
}
}
}

/// Collect metadata from the project configuration and from the workspace.
///
/// # Arguments
/// `ws`: the workspace.
///
/// # Returns
/// A [`DojoMetadata`] object containing all Dojo metadata.
pub fn dojo_metadata_from_workspace_old(ws: &Workspace<'_>) -> Result<DojoMetadata> {
let profile = ws.config().profile();

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(BASE_DIR).join(ABIS_DIR);
let source_dir = ws.target_dir().path_existent().unwrap();
let source_dir = source_dir.join(profile.as_str());

let project_metadata = if let Ok(current_package) = ws.current_package() {
current_package
.manifest
.metadata
.dojo()
.with_context(|| format!("Error parsing manifest file `{}`", ws.manifest_path()))?
} else {
// On workspaces, dojo metadata are not accessible because if no current package is defined
// (being the only package or using --package).
return Err(anyhow!(
"No current package with dojo metadata found, virtual manifest in workspace are not \
supported. Until package compilation is supported, you will have to provide the path \
to the Scarb.toml file using the --manifest-path option."
));
};

let mut dojo_metadata = DojoMetadata {
env: project_metadata.env.clone(),
skip_migration: project_metadata.skip_migration.clone(),
..Default::default()
};

let world_artifact = build_artifact_from_filename(
&abi_dir,
&source_dir,
&naming::get_filename_from_tag(WORLD_CONTRACT_TAG),
);

// inialize Dojo world metadata with world metadata coming from project configuration
dojo_metadata.world = project_to_world_metadata(project_metadata.world);
dojo_metadata.world.artifacts = world_artifact;

// load models and contracts metadata
if manifest_dir.join(BASE_DIR).exists() {
if let Ok(manifest) = BaseManifest::load_from_path(&manifest_dir.join(BASE_DIR)) {
for model in manifest.models {
let tag = model.inner.tag.clone();
let abi_model_dir = abi_dir.join(MODELS_DIR);
let source_model_dir = source_dir.join(MODELS_DIR);
dojo_metadata.resources_artifacts.insert(
tag.clone(),
ResourceMetadata {
name: tag.clone(),
artifacts: build_artifact_from_filename(
&abi_model_dir,
&source_model_dir,
&naming::get_filename_from_tag(&tag),
),
},
);
}

for contract in manifest.contracts {
let tag = contract.inner.tag.clone();
let abi_contract_dir = abi_dir.join(CONTRACTS_DIR);
let source_contract_dir = source_dir.join(CONTRACTS_DIR);
dojo_metadata.resources_artifacts.insert(
tag.clone(),
ResourceMetadata {
name: tag.clone(),
artifacts: build_artifact_from_filename(
&abi_contract_dir,
&source_contract_dir,
&naming::get_filename_from_tag(&tag),
),
},
);
}
))
}
}

Ok(dojo_metadata)
}

/// Metadata coming from project configuration (Scarb.toml)
Expand Down

0 comments on commit f6eef30

Please sign in to comment.