/ vm / package / clean.rs
clean.rs
  1  // Copyright (c) 2025-2026 ACDC Network
  2  // This file is part of the alphavm library.
  3  //
  4  // Alpha Chain | Delta Chain Protocol
  5  // International Monetary Graphite.
  6  //
  7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
  8  // They built world-class ZK infrastructure. We installed the EASY button.
  9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
 10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
 11  //
 12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
 13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
 14  // No rights reserved. No permission required. No warranty. No refunds.
 15  //
 16  // https://creativecommons.org/publicdomain/zero/1.0/
 17  // SPDX-License-Identifier: CC0-1.0
 18  
 19  use super::*;
 20  
 21  impl<N: Network> Package<N> {
 22      /// Removes the build directory for the package.
 23      pub fn clean(directory: &Path) -> Result<()> {
 24          // Ensure the directory path exists.
 25          ensure!(directory.exists(), "The program directory does not exist: {}", directory.display());
 26          // Ensure the manifest file exists.
 27          ensure!(
 28              Manifest::<N>::exists_at(directory),
 29              "Missing '{}' at '{}'",
 30              Manifest::<N>::file_name(),
 31              directory.display()
 32          );
 33          // Ensure the main program file exists.
 34          ensure!(
 35              AlphaFile::<N>::main_exists_at(directory),
 36              "Missing '{}' at '{}'",
 37              AlphaFile::<N>::main_file_name(),
 38              directory.display()
 39          );
 40  
 41          // Prepare the build directory.
 42          let build_directory = directory.join("build");
 43          // Remove the build directory if it exists.
 44          if build_directory.exists() {
 45              std::fs::remove_dir_all(&build_directory)?;
 46          }
 47  
 48          Ok(())
 49      }
 50  }
 51  
 52  #[cfg(test)]
 53  mod tests {
 54      use super::*;
 55  
 56      type CurrentNetwork = alphavm_console::network::MainnetV0;
 57      type CurrentAlpha = alphavm_circuit::network::AlphaV0;
 58  
 59      #[test]
 60      fn test_clean() {
 61          // Samples a new package at a temporary directory.
 62          let (directory, package) = crate::package::test_helpers::sample_token_package();
 63  
 64          // Ensure the build directory does *not* exist.
 65          assert!(!package.build_directory().exists());
 66          // Clean the package.
 67          Package::<CurrentNetwork>::clean(&directory).unwrap();
 68          // Ensure the build directory still does *not* exist.
 69          assert!(!package.build_directory().exists());
 70  
 71          // Build the package.
 72          package.build::<CurrentAlpha>().unwrap();
 73  
 74          // Ensure the build directory exists.
 75          assert!(package.build_directory().exists());
 76          // Clean the package.
 77          Package::<CurrentNetwork>::clean(&directory).unwrap();
 78          // Ensure the build directory does *not* exist.
 79          assert!(!package.build_directory().exists());
 80  
 81          // Proactively remove the temporary directory (to conserve space).
 82          std::fs::remove_dir_all(directory).unwrap();
 83      }
 84  
 85      #[test]
 86      fn test_clean_with_import() {
 87          // Samples a new package at a temporary directory.
 88          let (directory, package) = crate::package::test_helpers::sample_wallet_package();
 89  
 90          // Ensure the build directory does *not* exist.
 91          assert!(!package.build_directory().exists());
 92          // Clean the package.
 93          Package::<CurrentNetwork>::clean(&directory).unwrap();
 94          // Ensure the build directory still does *not* exist.
 95          assert!(!package.build_directory().exists());
 96  
 97          // Build the package.
 98          package.build::<CurrentAlpha>().unwrap();
 99  
100          // Ensure the build directory exists.
101          assert!(package.build_directory().exists());
102          // Clean the package.
103          Package::<CurrentNetwork>::clean(&directory).unwrap();
104          // Ensure the build directory does *not* exist.
105          assert!(!package.build_directory().exists());
106  
107          // Proactively remove the temporary directory (to conserve space).
108          std::fs::remove_dir_all(directory).unwrap();
109      }
110  }