Skip to content

Commit

Permalink
refactor: clean up bin/sozo (#1828)
Browse files Browse the repository at this point in the history
* refactor: move verify_cairo_version_compatibility to utils module

* refactor: move around more things to appropriate places

* refactor: make bin/sozo more consistent

* fix: paths in test
  • Loading branch information
lambda-0x authored Apr 14, 2024
1 parent 995dd8d commit 371c121
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 335 deletions.
61 changes: 3 additions & 58 deletions bin/sozo/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
use anyhow::Result;
use camino::Utf8PathBuf;
use clap::{Parser, Subcommand};
use clap::Parser;
use scarb::compiler::Profile;
use scarb_ui::Verbosity;
use smol_str::SmolStr;
use tracing::level_filters::LevelFilter;
use tracing_log::AsTrace;

use crate::commands::auth::AuthArgs;
use crate::commands::build::BuildArgs;
use crate::commands::call::CallArgs;
use crate::commands::clean::CleanArgs;
use crate::commands::completions::CompletionsArgs;
use crate::commands::dev::DevArgs;
use crate::commands::events::EventsArgs;
use crate::commands::execute::ExecuteArgs;
use crate::commands::init::InitArgs;
use crate::commands::migrate::MigrateArgs;
use crate::commands::model::ModelArgs;
use crate::commands::register::RegisterArgs;
use crate::commands::test::TestArgs;

fn generate_version() -> String {
const DOJO_VERSION: &str = env!("CARGO_PKG_VERSION");
let scarb_version = scarb::version::get().version;
let scarb_sierra_version = scarb::version::get().sierra.version;
let scarb_cairo_version = scarb::version::get().cairo.version;

let version_string = format!(
"{}\nscarb: {}\ncairo: {}\nsierra: {}",
DOJO_VERSION, scarb_version, scarb_cairo_version, scarb_sierra_version,
);
version_string
}
use crate::commands::Commands;
use crate::utils::generate_version;

#[derive(Parser)]
#[command(author, version=generate_version(), about, long_about = None)]
Expand Down Expand Up @@ -63,37 +39,6 @@ pub struct SozoArgs {
pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
#[command(about = "Build the world, generating the necessary artifacts for deployment")]
Build(BuildArgs),
#[command(about = "Initialize a new project")]
Init(InitArgs),
#[command(about = "Remove generated artifacts, manifests and abis")]
Clean(CleanArgs),
#[command(about = "Run a migration, declaring and deploying contracts as necessary to \
update the world")]
Migrate(Box<MigrateArgs>),
#[command(about = "Developer mode: watcher for building and migration")]
Dev(DevArgs),
#[command(about = "Test the project's smart contracts")]
Test(TestArgs),
#[command(about = "Execute a world's system")]
Execute(ExecuteArgs),
#[command(about = "Call a world's system")]
Call(CallArgs),
#[command(about = "Interact with a worlds models")]
Model(ModelArgs),
#[command(about = "Register new models")]
Register(RegisterArgs),
#[command(about = "Queries world events")]
Events(EventsArgs),
#[command(about = "Manage world authorization")]
Auth(AuthArgs),
#[command(about = "Generate shell completion file for specified shell")]
Completions(CompletionsArgs),
}

impl SozoArgs {
pub fn ui_verbosity(&self) -> Verbosity {
let filter = self.verbose.log_level_filter().as_trace();
Expand Down
106 changes: 53 additions & 53 deletions bin/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,59 @@ pub struct AuthArgs {
pub command: AuthCommand,
}

#[derive(Debug, Subcommand)]
pub enum AuthCommand {
#[command(about = "Grant an auth role.")]
Grant {
#[command(subcommand)]
kind: AuthKind,

#[command(flatten)]
world: WorldOptions,

#[command(flatten)]
starknet: StarknetOptions,

#[command(flatten)]
account: AccountOptions,

#[command(flatten)]
transaction: TransactionOptions,
},
#[command(about = "Revoke an auth role.")]
Revoke {
#[command(subcommand)]
kind: AuthKind,

#[command(flatten)]
world: WorldOptions,

#[command(flatten)]
starknet: StarknetOptions,

#[command(flatten)]
account: AccountOptions,

#[command(flatten)]
transaction: TransactionOptions,
},
}

impl AuthArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = utils::load_metadata_from_config(config)?;

match self.command {
AuthCommand::Grant { kind, world, starknet, account, transaction } => config
.tokio_handle()
.block_on(grant(world, account, starknet, env_metadata, kind, transaction)),
AuthCommand::Revoke { kind, world, starknet, account, transaction } => config
.tokio_handle()
.block_on(revoke(world, account, starknet, env_metadata, kind, transaction)),
}
}
}

#[derive(Debug, Subcommand)]
pub enum AuthKind {
#[command(about = "Grant a contract permission to write to a model.")]
Expand Down Expand Up @@ -82,59 +135,6 @@ pub async fn revoke(
}
}

#[derive(Debug, Subcommand)]
pub enum AuthCommand {
#[command(about = "Grant an auth role.")]
Grant {
#[command(subcommand)]
kind: AuthKind,

#[command(flatten)]
world: WorldOptions,

#[command(flatten)]
starknet: StarknetOptions,

#[command(flatten)]
account: AccountOptions,

#[command(flatten)]
transaction: TransactionOptions,
},
#[command(about = "Revoke an auth role.")]
Revoke {
#[command(subcommand)]
kind: AuthKind,

#[command(flatten)]
world: WorldOptions,

#[command(flatten)]
starknet: StarknetOptions,

#[command(flatten)]
account: AccountOptions,

#[command(flatten)]
transaction: TransactionOptions,
},
}

impl AuthArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = utils::load_metadata_from_config(config)?;

match self.command {
AuthCommand::Grant { kind, world, starknet, account, transaction } => config
.tokio_handle()
.block_on(grant(world, account, starknet, env_metadata, kind, transaction)),
AuthCommand::Revoke { kind, world, starknet, account, transaction } => config
.tokio_handle()
.block_on(revoke(world, account, starknet, env_metadata, kind, transaction)),
}
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down
2 changes: 1 addition & 1 deletion bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dojo_lang::scarb_internal::compile_workspace;
use scarb::core::{Config, TargetKind};
use scarb::ops::CompileOpts;

#[derive(Args, Debug)]
#[derive(Debug, Args)]
pub struct BuildArgs {
#[arg(long)]
#[arg(help = "Generate Typescript bindings.")]
Expand Down
30 changes: 15 additions & 15 deletions bin/sozo/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ pub struct CleanArgs {
}

impl CleanArgs {
pub fn clean_manifests_abis(&self, root_dir: &Utf8PathBuf, profile_name: &str) -> Result<()> {
let dirs = vec![
root_dir.join(MANIFESTS_DIR).join(profile_name).join(BASE_DIR),
root_dir.join(MANIFESTS_DIR).join(profile_name).join(ABIS_DIR).join(BASE_DIR),
];

for d in dirs {
if d.exists() {
fs::remove_dir_all(d)?;
}
}

Ok(())
}

pub fn run(self, config: &Config) -> Result<()> {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;

Expand All @@ -55,4 +40,19 @@ impl CleanArgs {

Ok(())
}

pub fn clean_manifests_abis(&self, root_dir: &Utf8PathBuf, profile_name: &str) -> Result<()> {
let dirs = vec![
root_dir.join(MANIFESTS_DIR).join(profile_name).join(BASE_DIR),
root_dir.join(MANIFESTS_DIR).join(profile_name).join(ABIS_DIR).join(BASE_DIR),
];

for d in dirs {
if d.exists() {
fs::remove_dir_all(d)?;
}
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion bin/sozo/src/commands/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap_complete::{generate, Shell};

use crate::args::SozoArgs;

#[derive(Args, Debug)]
#[derive(Debug, Args)]
pub struct CompletionsArgs {
shell: Shell,
}
Expand Down
Loading

0 comments on commit 371c121

Please sign in to comment.