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

dev: make DojoMetadata to handle metadata specific to dojo easier #707

Merged
merged 7 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 2 additions & 6 deletions crates/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ impl AuthArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values
env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata)
} else {
None
};
Expand Down
8 changes: 2 additions & 6 deletions crates/sozo/src/commands/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,9 @@ impl ComponentArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values
env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata)
} else {
None
};
Expand Down
8 changes: 2 additions & 6 deletions crates/sozo/src/commands/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ impl EventsArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values
env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata)
} else {
None
};
Expand Down
8 changes: 2 additions & 6 deletions crates/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ impl ExecuteArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values
env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata)
} else {
None
};
Expand Down
12 changes: 3 additions & 9 deletions crates/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ impl MigrateArgs {
scarb::ops::compile(&ws)?;
}

let mut env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

// If there is an environment-specific metadata, use that, otherwise use the
// workspace's default environment metadata.
env_metadata = env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata);
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values

ws.config().tokio_handle().block_on(migration::execute(
self,
Expand Down
27 changes: 24 additions & 3 deletions crates/sozo/src/commands/options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
use scarb::core::Workspace;
use scarb::core::{ManifestMetadata, Workspace};
use toml::Value;

pub mod account;
pub mod starknet;
pub mod world;

pub(super) fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Option<Value> {
ws.current_package().ok()?.manifest.metadata.tool_metadata.as_ref()?.get("dojo").cloned()
pub(crate) fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Option<DojoMetadata> {
Some(ws.current_package().ok()?.manifest.metadata.dojo_metadata())
}

pub(crate) struct DojoMetadata {
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
env: Option<Value>,
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
}

impl DojoMetadata {
pub fn env(&self) -> Option<Value> {
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
self.env.clone()
}
}
trait MetadataExt {
fn dojo_metadata(&self) -> DojoMetadata;
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
}

impl MetadataExt for ManifestMetadata {
fn dojo_metadata(&self) -> DojoMetadata {
let dojo_metadata = self.tool_metadata.as_ref().and_then(|e| e.get("dojo")).cloned();
let env_metadata = dojo_metadata.and_then(|inner| inner.get("env").cloned());
DojoMetadata { env: env_metadata }
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
}
}
8 changes: 2 additions & 6 deletions crates/sozo/src/commands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ impl RegisterArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values
env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata)
} else {
None
};
Expand Down
8 changes: 2 additions & 6 deletions crates/sozo/src/commands/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,9 @@ impl SystemArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let env_metadata = dojo_metadata_from_workspace(&ws)
.and_then(|dojo_metadata| dojo_metadata.get("env").cloned());

let env_metadata = dojo_metadata_from_workspace(&ws).and_then(|inner| inner.env());
// TODO: Check the updated scarb way to read profile specific values
env_metadata
.as_ref()
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned())
.or(env_metadata)
} else {
None
};
Expand Down