Skip to content

Commit

Permalink
Print USC compilation details (#2631)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2403 

## Introduced changes

<!-- A brief description of the changes -->

- Print information about compiling Sierra as a spinner message
- Skip printing the path for unsaved files

## Checklist

<!-- Make sure all of these are complete -->

- [X] Linked relevant issue
- [X] Updated relevant documentation
- [X] Added relevant tests
- [X] Performed self-review of the code
- [X] Added changes to `CHANGELOG.md`
  • Loading branch information
ddoktorski authored Nov 14, 2024
1 parent d38bc8c commit 867c645
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
#### Added

- `generate_random_felt()` for generating (pseudo) random felt value.
- Printing information about compiling Sierra using `universal-sierra-compiler`

### Cast

Expand Down
1 change: 1 addition & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ async-trait = "0.1.83"
serde_path_to_error = "0.1.16"
wiremock = "0.6.0"
const-hex = "1.13.1"
indicatif = "0.17.8"
1 change: 1 addition & 0 deletions crates/universal-sierra-compiler-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ num-bigint.workspace = true
cairo-lang-casm.workspace = true
cairo-lang-sierra.workspace = true
camino.workspace = true
indicatif.workspace = true
7 changes: 6 additions & 1 deletion crates/universal-sierra-compiler-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use std::io::Write;
use std::str::from_utf8;
use tempfile::Builder;

use crate::spinner::spawn_spinner_message;
pub use command::*;
use shared::command::CommandExt;

mod command;
mod spinner;

pub type CasmCodeOffset = usize;
pub type CasmInstructionIdx = usize;
Expand Down Expand Up @@ -70,8 +72,9 @@ pub fn compile_sierra_at_path<T: UniversalSierraCompilerOutput>(
sierra_file_path: &Utf8Path,
sierra_type: &SierraType,
) -> Result<T> {
let mut usc_command = UniversalSierraCompilerCommand::new();
let spinner = spawn_spinner_message(sierra_file_path)?;

let mut usc_command = UniversalSierraCompilerCommand::new();
let usc_output = usc_command
.inherit_stderr()
.args(vec![
Expand All @@ -87,6 +90,8 @@ pub fn compile_sierra_at_path<T: UniversalSierraCompilerOutput>(
Contact us if it doesn't help",
)?;

spinner.finish_and_clear();

Ok(T::convert(from_utf8(&usc_output.stdout)?.to_string()))
}

Expand Down
30 changes: 30 additions & 0 deletions crates/universal-sierra-compiler-api/src/spinner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use anyhow::Result;
use camino::Utf8Path;
use indicatif::{ProgressBar, ProgressStyle};
use std::env;
use std::time::Duration;

pub fn spawn_spinner_message(sierra_file_path: &Utf8Path) -> Result<ProgressBar> {
let spinner = ProgressBar::new_spinner();
spinner.set_style(ProgressStyle::with_template("\n{spinner} {msg}\n")?);
spinner.enable_steady_tick(Duration::from_millis(100));

// Skip printing path when compiling unsaved sierra
// which occurs during test execution for some cheatcodes e.g. `replace_bytecode`
let message = if is_temp_file(sierra_file_path)? {
"Compiling Sierra to Casm".to_string()
} else {
format!(
"Compiling Sierra to Casm ({})",
sierra_file_path.canonicalize_utf8()?
)
};
spinner.set_message(message);

Ok(spinner)
}

fn is_temp_file(file_path: &Utf8Path) -> Result<bool> {
let temp_dir = env::temp_dir().canonicalize()?;
Ok(file_path.canonicalize()?.starts_with(temp_dir))
}

0 comments on commit 867c645

Please sign in to comment.