Skip to content

Commit

Permalink
add cargo workflow for static buffer configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Sep 1, 2023
1 parent 11222f3 commit 48d9dcd
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![doc = include_str!("../README.md")]
#![deny(unused_crate_dependencies)]

use contract_metadata::ContractMetadata;
use which as _;

mod args;
Expand Down Expand Up @@ -370,6 +371,65 @@ fn exec_cargo_for_onchain_target(
Ok(())
}

/// Check if the `INK_STATIC_BUFFER_SIZE` is set.
/// If so, then checks if the current contract has already been compiled with a new value.
/// If not, or metadata is not present, we need to clean binaries and rebuilt.
fn check_buffer_size_invoke_cargo_clean(
crate_metadata: &CrateMetadata,
verbosity: &Verbosity,
) -> Result<()> {
if let Ok(buffer_size) = std::env::var("INK_STATIC_BUFFER_SIZE") {
let buffer_size_value: u64 = buffer_size
.parse()
.context("`INK_STATIC_BUFFER_SIZE` must have an integer value.")?;

let cargo = util::cargo_cmd(
"clean",
Vec::<&str>::new(),
crate_metadata.manifest_path.directory(),
*verbosity,
vec![],
);

if let Ok(metadata) = ContractMetadata::load(crate_metadata.metadata_path()) {
let contract_buffer_size = metadata
.abi
// get `spec` field
.get("spec")
.context("spec field should be present in ABI.")?
// get `environment` field
.get("environment")
.context("environment field should be present in ABI.")?
// get `staticBufferSize` field
.get("staticBufferSize")
.context("`staticBufferSize` must be specified.")?
// convert to u64
.as_u64()
.context("`staticBufferSize` value must be an integer.")?;

if contract_buffer_size == buffer_size_value {
verbose_eprintln!(
verbosity,
"{} {}",
"info:".green().bold(),
"Detected a configured buffer size, but the value is already specified."
.bold()
);
return Ok(())
}
}
verbose_eprintln!(
verbosity,
"{} {}",
"warning:".yellow().bold(),
"Detected a change in the configured buffer size. Rebuilding the project."
.bold()
);
invoke_cargo_and_scan_for_error(cargo)?;
}
Ok(())
}

/// Executes the supplied cargo command, reading the output and scanning for known errors.
/// Writes the captured stderr back to stderr and maintains the cargo tty progress bar.
fn invoke_cargo_and_scan_for_error(cargo: duct::Expression) -> Result<()> {
Expand Down Expand Up @@ -880,6 +940,7 @@ fn local_build(
"[==]".bold(),
"Building cargo project".bright_green().bold()
);
check_buffer_size_invoke_cargo_clean(crate_metadata, verbosity)?;
exec_cargo_for_onchain_target(
crate_metadata,
"build",
Expand Down

0 comments on commit 48d9dcd

Please sign in to comment.