|
17 | 17 | #![doc = include_str!("../README.md")] |
18 | 18 | #![deny(unused_crate_dependencies)] |
19 | 19 |
|
| 20 | +use contract_metadata::ContractMetadata; |
20 | 21 | use which as _; |
21 | 22 |
|
22 | 23 | mod args; |
@@ -370,6 +371,81 @@ fn exec_cargo_for_onchain_target( |
370 | 371 | Ok(()) |
371 | 372 | } |
372 | 373 |
|
| 374 | +/// Check if the `INK_STATIC_BUFFER_SIZE` is set. |
| 375 | +/// If so, then checks if the current contract has already been compiled with a new value. |
| 376 | +/// If not, or metadata is not present, we need to clean binaries and rebuild. |
| 377 | +fn check_buffer_size_invoke_cargo_clean( |
| 378 | + crate_metadata: &CrateMetadata, |
| 379 | + verbosity: &Verbosity, |
| 380 | +) -> Result<()> { |
| 381 | + if let Ok(buffer_size) = std::env::var("INK_STATIC_BUFFER_SIZE") { |
| 382 | + let buffer_size_value: u64 = buffer_size |
| 383 | + .parse() |
| 384 | + .context("`INK_STATIC_BUFFER_SIZE` must have an integer value.")?; |
| 385 | + |
| 386 | + let extract_buffer_size = |metadata_path: PathBuf| -> Result<u64> { |
| 387 | + let size = ContractMetadata::load(metadata_path) |
| 388 | + .context("Metadata is not present")? |
| 389 | + .abi |
| 390 | + // get `spec` field |
| 391 | + .get("spec") |
| 392 | + .context("spec field should be present in ABI.")? |
| 393 | + // get `environment` field |
| 394 | + .get("environment") |
| 395 | + .context("environment field should be present in ABI.")? |
| 396 | + // get `staticBufferSize` field |
| 397 | + .get("staticBufferSize") |
| 398 | + .context("`staticBufferSize` must be specified.")? |
| 399 | + // convert to u64 |
| 400 | + .as_u64() |
| 401 | + .context("`staticBufferSize` value must be an integer.")?; |
| 402 | + |
| 403 | + Ok(size) |
| 404 | + }; |
| 405 | + |
| 406 | + let cargo = util::cargo_cmd( |
| 407 | + "clean", |
| 408 | + Vec::<&str>::new(), |
| 409 | + crate_metadata.manifest_path.directory(), |
| 410 | + *verbosity, |
| 411 | + vec![], |
| 412 | + ); |
| 413 | + |
| 414 | + match extract_buffer_size(crate_metadata.metadata_path()) { |
| 415 | + Ok(contract_buffer_size) if contract_buffer_size == buffer_size_value => { |
| 416 | + verbose_eprintln!( |
| 417 | + verbosity, |
| 418 | + "{} {}", |
| 419 | + "info:".green().bold(), |
| 420 | + "Detected a configured buffer size, but the value is already specified." |
| 421 | + .bold() |
| 422 | + ); |
| 423 | + } |
| 424 | + Ok(_) => { |
| 425 | + verbose_eprintln!( |
| 426 | + verbosity, |
| 427 | + "{} {}", |
| 428 | + "warning:".yellow().bold(), |
| 429 | + "Detected a change in the configured buffer size. Rebuilding the project." |
| 430 | + .bold() |
| 431 | + ); |
| 432 | + invoke_cargo_and_scan_for_error(cargo)?; |
| 433 | + } |
| 434 | + Err(_) => { |
| 435 | + verbose_eprintln!( |
| 436 | + verbosity, |
| 437 | + "{} {}", |
| 438 | + "warning:".yellow().bold(), |
| 439 | + "Cannot find the previous size of the static buffer. Rebuilding the project." |
| 440 | + .bold() |
| 441 | + ); |
| 442 | + invoke_cargo_and_scan_for_error(cargo)?; |
| 443 | + } |
| 444 | + } |
| 445 | + } |
| 446 | + Ok(()) |
| 447 | +} |
| 448 | + |
373 | 449 | /// Executes the supplied cargo command, reading the output and scanning for known errors. |
374 | 450 | /// Writes the captured stderr back to stderr and maintains the cargo tty progress bar. |
375 | 451 | fn invoke_cargo_and_scan_for_error(cargo: duct::Expression) -> Result<()> { |
@@ -880,6 +956,7 @@ fn local_build( |
880 | 956 | "[==]".bold(), |
881 | 957 | "Building cargo project".bright_green().bold() |
882 | 958 | ); |
| 959 | + check_buffer_size_invoke_cargo_clean(crate_metadata, verbosity)?; |
883 | 960 | exec_cargo_for_onchain_target( |
884 | 961 | crate_metadata, |
885 | 962 | "build", |
|
0 commit comments