Skip to content

Commit

Permalink
[feat]: add --outfile arg for wasm cli
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Balashov <[email protected]>
  • Loading branch information
0x009922 committed Aug 29, 2023
1 parent 7e451be commit ac96b53
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
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.

5 changes: 5 additions & 0 deletions wasm_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ impl Output {

Ok(wasm_data)
}

/// Get the file path of the underlying WASM
pub fn wasm_file_path(&self) -> &PathBuf {
&self.wasm_file
}
}

// TODO: Remove cargo invocation (#2152)
Expand Down
1 change: 1 addition & 0 deletions wasm_builder_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ iroha_wasm_builder.workspace = true
clap = { workspace = true, features = ["derive"] }
color-eyre.workspace = true
spinoff = { workspace = true, features = ["binary", "dots12"] }
owo-colors = { workspace = true, features = ["supports-colors"] }
4 changes: 2 additions & 2 deletions wasm_builder_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ iroha_wasm_builder_cli check path/to/project
**Build the smartcontract:**

```bash
iroha_wasm_builder_cli build path/to/project
iroha_wasm_builder_cli build path/to/project --outfile ./smartcontract.wasm
```

**Build with options:**

```bash
iroha_wasm_builder_cli build path/to/project --optimize --format
iroha_wasm_builder_cli build path/to/project --optimize --format --outfile ./smartcontract.wasm
```
24 changes: 16 additions & 8 deletions wasm_builder_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::{
};

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

#[derive(Parser, Debug)]
#[command(name = "iroha_wasm_builder_cli", version, author)]
Expand All @@ -26,6 +27,9 @@ enum Cli {
/// Optimize WASM output.
#[arg(long)]
optimize: bool,
/// Where to store the output WASM. If the file exists, it will be overwritten.
#[arg(long)]
outfile: PathBuf,
},
}

Expand All @@ -47,6 +51,7 @@ fn main() -> color_eyre::Result<()> {
common: CommonArgs { path },
format,
optimize,
outfile,
} => {
let builder = Builder::new(&path);
let builder = if format { builder.format() } else { builder };
Expand Down Expand Up @@ -93,14 +98,17 @@ fn main() -> color_eyre::Result<()> {
output
};

let bytes = output
.into_bytes()
.wrap_err("Failed to fetch the bytes of the output smartcontract")?;
std::fs::copy(output.wasm_file_path(), &outfile).wrap_err_with(|| {
eyre!(
"Failed to write the resulting file into {}",
outfile.display()
)
})?;

let mut writer = BufWriter::new(stdout());
writer
.write_all(&bytes)
.wrap_err("Failed to output the bytes")?;
eprintln!(
"✓ File is written into {}",
outfile.display().green().bold()
);
}
}

Expand Down

0 comments on commit ac96b53

Please sign in to comment.