Skip to content

Commit

Permalink
Merge branch 'main' into bump-devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm authored Apr 17, 2024
2 parents dac21ff + b7eab85 commit 3c5d2d3
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 6 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/release-dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ jobs:
runs-on: ubuntu-latest
container:
image: ghcr.io/dojoengine/dojo-dev:3153a80
env:
VERSION: ""
steps:
# Workaround described here: https://github.com/actions/checkout/issues/760
- uses: actions/checkout@v3
- run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
- run: |
version=${{ inputs.version }}
version=${version#v}
cargo release version ${{ inputs.version }} --execute --no-confirm && cargo release replace --execute --no-confirm
VERSION=${{ inputs.version }}
VERSION=${VERSION#v}
cargo release version $VERSION --execute --no-confirm && cargo release replace --execute --no-confirm
- id: version_info
run: |
cargo install cargo-get
Expand Down
44 changes: 43 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prettytable-rs = "0.10.0"
anyhow.workspace = true
async-trait.workspace = true
cairo-lang-compiler.workspace = true
Expand Down
100 changes: 98 additions & 2 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;
use dojo_bindgen::{BuiltinPlugins, PluginManager};
use dojo_lang::scarb_internal::compile_workspace;
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::{format, Cell, Row, Table};
use scarb::core::{Config, TargetKind};
use scarb::ops::CompileOpts;
use sozo_ops::statistics::{get_contract_statistics_for_dir, ContractStatistics};

#[derive(Debug, Args)]
pub struct BuildArgs {
Expand All @@ -22,6 +25,9 @@ pub struct BuildArgs {
#[arg(long)]
#[arg(help = "Output directory.", default_value = "bindings")]
pub bindings_output: String,

#[arg(long, help = "Display statistics about the compiled contracts")]
pub stats: bool,
}

impl BuildArgs {
Expand All @@ -44,6 +50,14 @@ impl BuildArgs {
builtin_plugins.push(BuiltinPlugins::Unity);
}

if self.stats {
let target_dir = &compile_info.target_dir;
let contracts_statistics = get_contract_statistics_for_dir(target_dir)
.context(format!("Error getting contracts stats"))?;
let table = create_stats_table(contracts_statistics);
table.printstd()
}

// Custom plugins are always empty for now.
let bindgen = PluginManager {
profile_name: compile_info.profile_name,
Expand All @@ -65,11 +79,41 @@ impl BuildArgs {
}
}

fn create_stats_table(contracts_statistics: Vec<ContractStatistics>) -> Table {
let mut table = Table::new();
table.set_format(*FORMAT_NO_LINESEP_WITH_TITLE);

// Add table headers
table.set_titles(Row::new(vec![
Cell::new_align("Contract", format::Alignment::CENTER),
Cell::new_align("Bytecode size (felts)", format::Alignment::CENTER),
Cell::new_align("Class size (bytes)", format::Alignment::CENTER),
]));

for contract_stats in contracts_statistics {
// Add table rows
let contract_name = contract_stats.contract_name;
let number_felts = contract_stats.number_felts;
let file_size = contract_stats.file_size;

table.add_row(Row::new(vec![
Cell::new_align(&contract_name, format::Alignment::LEFT),
Cell::new_align(format!("{}", number_felts).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", file_size).as_str(), format::Alignment::RIGHT),
]));
}

table
}

#[cfg(test)]
mod tests {
use dojo_test_utils::compiler::build_test_config;
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::{format, Cell, Row, Table};
use sozo_ops::statistics::ContractStatistics;

use super::BuildArgs;
use super::{create_stats_table, BuildArgs};

#[test]
fn build_example_with_typescript_and_unity_bindings() {
Expand All @@ -80,8 +124,60 @@ mod tests {
typescript: true,
unity: true,
typescript_v2: true,
stats: true,
};
let result = build_args.run(&config);
assert!(result.is_ok());
}

#[test]
fn test_create_stats_table() {
// Arrange
let contracts_statistics = vec![
ContractStatistics {
contract_name: "Test1".to_string(),
number_felts: 33,
file_size: 33,
},
ContractStatistics {
contract_name: "Test2".to_string(),
number_felts: 43,
file_size: 24,
},
ContractStatistics {
contract_name: "Test3".to_string(),
number_felts: 36,
file_size: 12,
},
];

let mut expected_table = Table::new();
expected_table.set_format(*FORMAT_NO_LINESEP_WITH_TITLE);
expected_table.set_titles(Row::new(vec![
Cell::new_align("Contract", format::Alignment::CENTER),
Cell::new_align("Bytecode size (felts)", format::Alignment::CENTER),
Cell::new_align("Class size (bytes)", format::Alignment::CENTER),
]));
expected_table.add_row(Row::new(vec![
Cell::new_align("Test1", format::Alignment::LEFT),
Cell::new_align(format!("{}", 33).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", 33).as_str(), format::Alignment::RIGHT),
]));
expected_table.add_row(Row::new(vec![
Cell::new_align("Test2", format::Alignment::LEFT),
Cell::new_align(format!("{}", 43).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", 24).as_str(), format::Alignment::RIGHT),
]));
expected_table.add_row(Row::new(vec![
Cell::new_align("Test3", format::Alignment::LEFT),
Cell::new_align(format!("{}", 36).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", 12).as_str(), format::Alignment::RIGHT),
]));

// Act
let table = create_stats_table(contracts_statistics);

// Assert
assert_eq!(table, expected_table, "Tables mismatch")
}
}
1 change: 1 addition & 0 deletions crates/sozo/ops/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod execute;
pub mod migration;
pub mod model;
pub mod register;
pub mod statistics;
pub mod utils;

#[cfg(test)]
Expand Down
Loading

0 comments on commit 3c5d2d3

Please sign in to comment.