/ vm / package / clean.rs
clean.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the alphavm library.
  3  
  4  // Licensed under the Apache License, Version 2.0 (the "License");
  5  // you may not use this file except in compliance with the License.
  6  // You may obtain a copy of the License at:
  7  
  8  // http://www.apache.org/licenses/LICENSE-2.0
  9  
 10  // Unless required by applicable law or agreed to in writing, software
 11  // distributed under the License is distributed on an "AS IS" BASIS,
 12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  // See the License for the specific language governing permissions and
 14  // limitations under the License.
 15  
 16  use super::*;
 17  
 18  impl<N: Network> Package<N> {
 19      /// Removes the build directory for the package.
 20      pub fn clean(directory: &Path) -> Result<()> {
 21          // Ensure the directory path exists.
 22          ensure!(directory.exists(), "The program directory does not exist: {}", directory.display());
 23          // Ensure the manifest file exists.
 24          ensure!(
 25              Manifest::<N>::exists_at(directory),
 26              "Missing '{}' at '{}'",
 27              Manifest::<N>::file_name(),
 28              directory.display()
 29          );
 30          // Ensure the main program file exists.
 31          ensure!(
 32              AlphaFile::<N>::main_exists_at(directory),
 33              "Missing '{}' at '{}'",
 34              AlphaFile::<N>::main_file_name(),
 35              directory.display()
 36          );
 37  
 38          // Prepare the build directory.
 39          let build_directory = directory.join("build");
 40          // Remove the build directory if it exists.
 41          if build_directory.exists() {
 42              std::fs::remove_dir_all(&build_directory)?;
 43          }
 44  
 45          Ok(())
 46      }
 47  }
 48  
 49  #[cfg(test)]
 50  mod tests {
 51      use super::*;
 52  
 53      type CurrentNetwork = alphavm_console::network::MainnetV0;
 54      type CurrentAlpha = alphavm_circuit::network::AlphaV0;
 55  
 56      #[test]
 57      fn test_clean() {
 58          // Samples a new package at a temporary directory.
 59          let (directory, package) = crate::package::test_helpers::sample_token_package();
 60  
 61          // Ensure the build directory does *not* exist.
 62          assert!(!package.build_directory().exists());
 63          // Clean the package.
 64          Package::<CurrentNetwork>::clean(&directory).unwrap();
 65          // Ensure the build directory still does *not* exist.
 66          assert!(!package.build_directory().exists());
 67  
 68          // Build the package.
 69          package.build::<CurrentAlpha>().unwrap();
 70  
 71          // Ensure the build directory exists.
 72          assert!(package.build_directory().exists());
 73          // Clean the package.
 74          Package::<CurrentNetwork>::clean(&directory).unwrap();
 75          // Ensure the build directory does *not* exist.
 76          assert!(!package.build_directory().exists());
 77  
 78          // Proactively remove the temporary directory (to conserve space).
 79          std::fs::remove_dir_all(directory).unwrap();
 80      }
 81  
 82      #[test]
 83      fn test_clean_with_import() {
 84          // Samples a new package at a temporary directory.
 85          let (directory, package) = crate::package::test_helpers::sample_wallet_package();
 86  
 87          // Ensure the build directory does *not* exist.
 88          assert!(!package.build_directory().exists());
 89          // Clean the package.
 90          Package::<CurrentNetwork>::clean(&directory).unwrap();
 91          // Ensure the build directory still does *not* exist.
 92          assert!(!package.build_directory().exists());
 93  
 94          // Build the package.
 95          package.build::<CurrentAlpha>().unwrap();
 96  
 97          // Ensure the build directory exists.
 98          assert!(package.build_directory().exists());
 99          // Clean the package.
100          Package::<CurrentNetwork>::clean(&directory).unwrap();
101          // Ensure the build directory does *not* exist.
102          assert!(!package.build_directory().exists());
103  
104          // Proactively remove the temporary directory (to conserve space).
105          std::fs::remove_dir_all(directory).unwrap();
106      }
107  }