Skip to content

Commit

Permalink
fix(sozo): remove generate overlay command (#2168)
Browse files Browse the repository at this point in the history
* update `generate-overlays` subcommand for new changes

* fix formatting

* fix: remove generate overlay command

* fix: remove oldly generated files

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
lambda-0x and glihm authored Jul 10, 2024
1 parent 0f4c089 commit 6683f0f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 81 deletions.
10 changes: 0 additions & 10 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ pub enum MigrateCommand {
#[command(flatten)]
transaction: TransactionOptions,
},
#[command(about = "Generate overlays file.")]
GenerateOverlays,
}

impl MigrateArgs {
Expand All @@ -67,13 +65,6 @@ impl MigrateArgs {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let dojo_metadata = dojo_metadata_from_workspace(&ws)?;

// This variant is tested before the match on `self.command` to avoid
// having the need to spin up a Katana to generate the files.
if let MigrateCommand::GenerateOverlays = self.command {
trace!("Generating overlays.");
return migration::generate_overlays(&ws);
}

let env_metadata = if config.manifest_path().exists() {
dojo_metadata.env().cloned()
} else {
Expand Down Expand Up @@ -127,7 +118,6 @@ impl MigrateArgs {
)
.await
}),
_ => unreachable!("other case handled above."),
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions crates/dojo-world/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl OverlayManifest {
///
/// - `world` and `base` manifest are written to root of the folder.
/// - `contracts` and `models` are written to their respective directories.
pub fn write_to_path_nested(&self, path: &Utf8PathBuf) -> Result<(), AbstractManifestError> {
pub fn write_to_path(&self, path: &Utf8PathBuf) -> Result<(), AbstractManifestError> {
fs::create_dir_all(path)?;

if let Some(ref world) = self.world {
Expand All @@ -296,14 +296,8 @@ impl OverlayManifest {
fs::write(file_name, base)?;
}

overlay_to_path::<OverlayDojoContract>(
&path.join(CONTRACTS_DIR),
self.contracts.as_slice(),
|c| c.tag.clone(),
)?;
overlay_to_path::<OverlayDojoModel>(&path.join(MODELS_DIR), self.models.as_slice(), |m| {
m.tag.clone()
})?;
overlay_to_path::<OverlayDojoContract>(path, self.contracts.as_slice(), |c| c.tag.clone())?;
overlay_to_path::<OverlayDojoModel>(path, self.models.as_slice(), |m| m.tag.clone())?;
Ok(())
}

Expand Down
64 changes: 2 additions & 62 deletions crates/sozo/ops/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::fs;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use anyhow::{anyhow, bail, Result};
use dojo_world::contracts::WorldContract;
use dojo_world::manifest::{
BaseManifest, OverlayClass, OverlayDojoContract, OverlayDojoModel, OverlayManifest,
BASE_CONTRACT_TAG, BASE_DIR, MANIFESTS_DIR, OVERLAYS_DIR, WORLD_CONTRACT_TAG,
};
use dojo_world::manifest::{BASE_DIR, MANIFESTS_DIR, OVERLAYS_DIR};
use dojo_world::metadata::get_default_namespace_from_ws;
use dojo_world::migration::world::WorldDiff;
use dojo_world::migration::{DeployOutput, TxnConfig, UpgradeOutput};
Expand Down Expand Up @@ -231,59 +227,3 @@ enum ContractDeploymentOutput {
enum ContractUpgradeOutput {
Output(UpgradeOutput),
}

pub fn generate_overlays(ws: &Workspace<'_>) -> Result<()> {
let profile_name =
ws.current_profile().expect("Scarb profile expected to be defined.").to_string();

// its path to a file so `parent` should never return `None`
let root_dir = ws.manifest_path().parent().unwrap().to_path_buf();
let manifest_base_dir = root_dir.join(MANIFESTS_DIR).join(&profile_name).join(BASE_DIR);
let overlay_dir = root_dir.join(OVERLAYS_DIR).join(&profile_name);

let base_manifest = BaseManifest::load_from_path(&manifest_base_dir)?;

let default_overlay = OverlayManifest {
world: Some(OverlayClass {
tag: WORLD_CONTRACT_TAG.to_string(),
original_class_hash: None,
}),
base: Some(OverlayClass { tag: BASE_CONTRACT_TAG.to_string(), original_class_hash: None }),
contracts: base_manifest
.contracts
.iter()
.map(|c| OverlayDojoContract { tag: c.inner.tag.clone(), ..Default::default() })
.collect::<Vec<_>>(),
models: base_manifest
.models
.iter()
.map(|m| OverlayDojoModel { tag: m.inner.tag.clone(), ..Default::default() })
.collect::<Vec<_>>(),
};

if overlay_dir.exists() {
// read existing OverlayManifest from path
let mut overlay_manifest = OverlayManifest::load_from_path(&overlay_dir, &base_manifest)
.with_context(|| "Failed to load OverlayManifest from path.")?;

// merge them to get OverlayManifest which contains all the contracts and models from base
// manifests
overlay_manifest.merge(default_overlay);

// to avoid duplicated overlay manifests, existing overlays must be removed before being
// rewritten by `overlay_manifest.write_to_path_nested()`
fs::remove_dir_all(&overlay_dir)?;
fs::create_dir_all(&overlay_dir)?;

overlay_manifest
.write_to_path_nested(&overlay_dir)
.with_context(|| "Failed to write OverlayManifest to path.")?;
} else {
fs::create_dir_all(&overlay_dir)?;
default_overlay
.write_to_path_nested(&overlay_dir)
.with_context(|| "Failed to write OverlayManifest to path.")?;
}

Ok(())
}

0 comments on commit 6683f0f

Please sign in to comment.