Skip to content

Commit

Permalink
fix: remove spinner from lib
Browse files Browse the repository at this point in the history
Signed-off-by: Lohachov Mykhailo <[email protected]>
  • Loading branch information
aoyako committed Dec 4, 2024
1 parent b7b783c commit da9f63e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 40 deletions.
3 changes: 2 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 crates/iroha_wasm_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ eyre = { workspace = true }
serde_json = { workspace = true, features = ["std"] }
sha256 = "1.5.0"
path-absolutize = { workspace = true }
strum = { workspace = true, features = ["derive", "std"] }
wasm-opt = "0.116.1"

clap = { workspace = true, features = ["derive"] }
Expand Down
73 changes: 38 additions & 35 deletions crates/iroha_wasm_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,24 @@ use path_absolutize::Absolutize;

/// Current toolchain used to build smartcontracts
const TOOLCHAIN: &str = "+nightly-2024-09-09";
const OPTIMIZED_PROFILE: &str = "deploy";

/// Build profile for smartcontracts
#[derive(Debug, Copy, Clone, Eq, PartialEq, strum::Display, strum::EnumString, Default)]
#[strum(serialize_all = "snake_case")]
pub enum Profile {
/// Applies release optimization
#[default]
Release,
/// Applies release and size optimizations
Deploy,
}

impl Profile {
/// Checks whether profile uses optimizations from wasm-opt
pub fn is_optimized(profile: Self) -> bool {
return profile == Profile::Deploy;
}
}

/// WASM Builder for smartcontracts (e.g. triggers and executors).
///
Expand Down Expand Up @@ -46,22 +63,22 @@ pub struct Builder<'path, 'out_dir> {
/// Flag controlling whether to show output of the build process
show_output: bool,
/// Build profile
profile: String,
profile: Profile,
}

impl<'path, 'out_dir> Builder<'path, 'out_dir> {
/// Initialize [`Builder`] with path to smartcontract.
///
/// `relative_path` should be relative to `CARGO_MANIFEST_DIR`.
pub fn new<P>(relative_path: &'path P, profile: &str) -> Self
pub fn new<P>(relative_path: &'path P, profile: Profile) -> Self
where
P: AsRef<Path> + ?Sized,
{
Self {
path: relative_path.as_ref(),
out_dir: None,
show_output: false,
profile: profile.to_string(),
profile: profile,
}
}

Expand Down Expand Up @@ -103,6 +120,17 @@ impl<'path, 'out_dir> Builder<'path, 'out_dir> {
///
/// Will also return error if ran on workspace and not on the concrete package.
pub fn build(self) -> Result<Output> {
let optimize = Profile::is_optimized(self.profile);
let output = self.into_internal()?.build()?;
if optimize {
output.optimize()
} else {
Ok(output)
}
}

#[doc(hidden)]
pub fn build_unoptimized(self) -> Result<Output> {
self.into_internal()?.build()
}

Expand All @@ -117,7 +145,7 @@ impl<'path, 'out_dir> Builder<'path, 'out_dir> {
|out_dir| Ok(Cow::Borrowed(out_dir)),
)?,
show_output: self.show_output,
profile: self.profile.clone(),
profile: self.profile,
})
}

Expand Down Expand Up @@ -171,7 +199,7 @@ mod internal {
pub absolute_path: PathBuf,
pub out_dir: Cow<'out_dir, Path>,
pub show_output: bool,
pub profile: String,
pub profile: Profile,
}

impl Builder<'_> {
Expand All @@ -186,41 +214,16 @@ mod internal {

pub fn build(self) -> Result<Output> {
let absolute_path = self.absolute_path.clone();
let optimize = self.profile == OPTIMIZED_PROFILE;
let show_output = self.show_output;
let optimize = Profile::is_optimized(self.profile);
let output = self.build_smartcontract().wrap_err_with(|| {
format!(
"Failed to build the smartcontract at path: {}",
absolute_path.display()
)
})?;

if optimize {
let sp = if show_output && std::env::var("CI").is_err() {
Some(spinoff::Spinner::new_with_stream(
spinoff::spinners::Binary,
"Optimizing the output",
None,
spinoff::Streams::Stderr,
))
} else {
None
};

match output.optimize() {
Ok(optimized) => {
if let Some(mut sp) = sp {
sp.success("Output is optimized");
}
Ok(optimized)
}
err => {
if let Some(mut sp) = sp {
sp.fail("Optimization failed");
}
err
}
}
output.optimize()
} else {
Ok(output)
}
Expand Down Expand Up @@ -270,7 +273,7 @@ mod internal {
let full_out_dir = self
.out_dir
.join("wasm32-unknown-unknown")
.join(self.profile.clone());
.join(self.profile.to_string());
let wasm_file = full_out_dir.join(package_name).with_extension("wasm");

let previous_hash = if wasm_file.exists() {
Expand Down
40 changes: 36 additions & 4 deletions crates/iroha_wasm_builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;

use clap::{Args, Parser};
use color_eyre::eyre::{eyre, Context};
use iroha_wasm_builder::Builder;
use iroha_wasm_builder::{Builder, Profile};
use owo_colors::OwoColorize;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -42,24 +42,56 @@ fn main() -> color_eyre::Result<()> {
common: CommonArgs { path },
profile,
} => {
let builder = Builder::new(&path, &profile).show_output();
let profile: Profile = profile.parse().expect("Invalid profile provided");
let builder = Builder::new(&path, profile).show_output();
builder.check()?;
}
Cli::Build {
common: CommonArgs { path },
out_file,
profile,
} => {
let builder = Builder::new(&path, &profile).show_output();
let profile: Profile = profile.parse().expect("Invalid profile provided");
let builder = Builder::new(&path, profile).show_output();

let output = {
// not showing the spinner here, cargo does a progress bar for us
match builder.build() {
match builder.build_unoptimized() {
Ok(output) => output,
err => err?,
}
};

let output = if Profile::is_optimized(profile) {
let sp = if std::env::var("CI").is_err() {
Some(spinoff::Spinner::new_with_stream(
spinoff::spinners::Binary,
"Optimizing the output",
None,
spinoff::Streams::Stderr,
))
} else {
None
};

match output.optimize() {
Ok(optimized) => {
if let Some(mut sp) = sp {
sp.success("Output is optimized");
}
optimized
}
err => {
if let Some(mut sp) = sp {
sp.fail("Optimization failed");
}
err?
}
}
} else {
output
};

std::fs::copy(output.wasm_file_path(), &out_file).wrap_err_with(|| {
eyre!(
"Failed to write the resulting file into {}",
Expand Down

0 comments on commit da9f63e

Please sign in to comment.