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

feat(sozo): add system command #468

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions crates/sozo/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::commands::build::BuildArgs;
use crate::commands::component::ComponentArgs;
use crate::commands::init::InitArgs;
use crate::commands::migrate::MigrateArgs;
use crate::commands::system::SystemArgs;
use crate::commands::test::TestArgs;

#[derive(Parser)]
Expand Down Expand Up @@ -48,6 +49,8 @@ pub enum Commands {
Test(TestArgs),

Component(ComponentArgs),

System(SystemArgs),
}

impl SozoArgs {
Expand Down
2 changes: 2 additions & 0 deletions crates/sozo/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) mod component;
pub(crate) mod init;
pub(crate) mod migrate;
pub(crate) mod options;
pub(crate) mod system;
pub(crate) mod test;

pub fn run(command: Commands, config: &Config) -> Result<()> {
Expand All @@ -17,5 +18,6 @@ pub fn run(command: Commands, config: &Config) -> Result<()> {
Commands::Build(args) => args.run(config),
Commands::Migrate(args) => args.run(config),
Commands::Component(args) => args.run(config),
Commands::System(args) => args.run(config),
}
}
65 changes: 65 additions & 0 deletions crates/sozo/src/commands/system.rs
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))
}
}
1 change: 1 addition & 0 deletions crates/sozo/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod component;
pub mod migration;
pub mod system;
66 changes: 66 additions & 0 deletions crates/sozo/src/ops/system.rs
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(())
}