-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
faet(sozo): add
system
command (#468)
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use anyhow::Result; | ||
use clap::{Args, Subcommand}; | ||
use scarb::core::Config; | ||
|
||
use super::options::dojo_metadata_from_workspace; | ||
use super::options::starknet::StarknetOptions; | ||
use super::options::world::WorldOptions; | ||
use crate::ops::system; | ||
|
||
#[derive(Debug, Args)] | ||
pub struct SystemArgs { | ||
#[command(subcommand)] | ||
command: SystemCommands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum SystemCommands { | ||
#[command(about = "Get the class hash of a system.")] | ||
Get { | ||
#[arg(help = "The name of the system.")] | ||
name: String, | ||
|
||
#[command(flatten)] | ||
world: WorldOptions, | ||
|
||
#[command(flatten)] | ||
starknet: StarknetOptions, | ||
}, | ||
|
||
#[command(alias = "dep")] | ||
#[command(about = "Retrieve the component dependencies of a system.")] | ||
Dependency { | ||
#[arg(help = "The name of the system.")] | ||
name: String, | ||
|
||
#[arg(short = 'j', long = "json")] | ||
#[arg(help_heading = "Display options")] | ||
to_json: bool, | ||
|
||
#[command(flatten)] | ||
world: WorldOptions, | ||
|
||
#[command(flatten)] | ||
starknet: StarknetOptions, | ||
}, | ||
} | ||
|
||
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()); | ||
|
||
env_metadata | ||
.as_ref() | ||
.and_then(|env_metadata| env_metadata.get(ws.config().profile().as_str()).cloned()) | ||
.or(env_metadata) | ||
} else { | ||
None | ||
}; | ||
|
||
config.tokio_handle().block_on(system::execute(self.command, env_metadata)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod component; | ||
pub mod migration; | ||
pub mod system; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use anyhow::Result; | ||
use dojo_world::world::WorldContractReader; | ||
use starknet::core::types::{BlockId, BlockTag}; | ||
use toml::Value; | ||
use yansi::Paint; | ||
|
||
use crate::commands::system::SystemCommands; | ||
|
||
pub async fn execute(command: SystemCommands, env_metadata: Option<Value>) -> Result<()> { | ||
match command { | ||
SystemCommands::Get { name, world, starknet } => { | ||
let world_address = world.address(env_metadata.as_ref())?; | ||
let provider = starknet.provider(env_metadata.as_ref())?; | ||
|
||
let world = WorldContractReader::new(world_address, &provider); | ||
let system = world.system(&name, BlockId::Tag(BlockTag::Pending)).await?; | ||
|
||
println!("{:#x}", system.class_hash()) | ||
} | ||
|
||
SystemCommands::Dependency { name, to_json, world, starknet } => { | ||
let world_address = world.address(env_metadata.as_ref())?; | ||
let provider = starknet.provider(env_metadata.as_ref())?; | ||
|
||
let world = WorldContractReader::new(world_address, &provider); | ||
let system = world.system(&name, BlockId::Tag(BlockTag::Pending)).await?; | ||
|
||
let deps = system.dependencies(BlockId::Tag(BlockTag::Pending)).await?; | ||
|
||
if to_json { | ||
println!("{}", serde_json::to_string_pretty(&deps)?); | ||
} else { | ||
let read = deps | ||
.iter() | ||
.enumerate() | ||
.filter_map(|(i, d)| { | ||
if d.read { Some(format!("{}.{}", i + 1, d.name.clone())) } else { None } | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let write = deps | ||
.iter() | ||
.enumerate() | ||
.filter_map(|(i, d)| { | ||
if d.write { Some(format!("{}. {}", i + 1, d.name.clone())) } else { None } | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let output = format!( | ||
r"{} | ||
{} | ||
{} | ||
{}", | ||
Paint::new("Read:").bold().underline(), | ||
read.join("\n"), | ||
Paint::new("Write:").bold().underline(), | ||
write.join("\n"), | ||
); | ||
|
||
println!("{output}") | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |