From c1243a2e74c609755e1e0e1e2e3454af35b1395b Mon Sep 17 00:00:00 2001 From: howardwu Date: Sun, 28 Jun 2020 18:53:09 -0700 Subject: [PATCH] Adds the command --- compiler/src/compiler.rs | 2 +- leo/commands/clean.rs | 53 ++++++++++++++++++++++++++++ leo/commands/mod.rs | 3 ++ leo/errors/files/checksum.rs | 3 ++ leo/errors/files/proof.rs | 3 ++ leo/errors/files/proving_key.rs | 3 ++ leo/errors/files/verification_key.rs | 3 ++ leo/files/checksum.rs | 12 +++++++ leo/files/proof.rs | 12 +++++++ leo/files/proving_key.rs | 12 +++++++ leo/files/verification_key.rs | 12 +++++++ leo/main.rs | 2 ++ 12 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 leo/commands/clean.rs diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 312ab9c3cf..4b5ea8be63 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -127,7 +127,7 @@ impl> ConstraintSynthesizer for Compil })?; // Write results to file or something - // log::info!("{}", result); + log::info!("{}", result); Ok(()) } diff --git a/leo/commands/clean.rs b/leo/commands/clean.rs new file mode 100644 index 0000000000..cd0f620a6c --- /dev/null +++ b/leo/commands/clean.rs @@ -0,0 +1,53 @@ +use crate::{ + cli::*, + cli_types::*, + commands::BuildCommand, + errors::CLIError, + files::{ChecksumFile, Manifest, ProofFile, ProvingKeyFile, VerificationKeyFile}, +}; + +use clap::ArgMatches; +use std::{convert::TryFrom, env::current_dir}; + +#[derive(Debug)] +pub struct CleanCommand; + +impl CLI for CleanCommand { + type Options = (); + type Output = (); + + const ABOUT: AboutType = "Clean the outputs directory"; + const ARGUMENTS: &'static [ArgumentType] = &[]; + const FLAGS: &'static [FlagType] = &[]; + const NAME: NameType = "clean"; + const OPTIONS: &'static [OptionType] = &[]; + const SUBCOMMANDS: &'static [SubCommandType] = &[]; + + #[cfg_attr(tarpaulin, skip)] + fn parse(_arguments: &ArgMatches) -> Result { + Ok(()) + } + + #[cfg_attr(tarpaulin, skip)] + fn output(options: Self::Options) -> Result { + let (_program, _checksum_differs) = BuildCommand::output(options)?; + + // Get the package name + let path = current_dir()?; + let package_name = Manifest::try_from(&path)?.get_package_name(); + + // Remove the checksum from the outputs directory + ChecksumFile::new(&package_name).remove(&path)?; + + // Remove the proving key from the outputs directory + ProvingKeyFile::new(&package_name).remove(&path)?; + + // Remove the verification key from the outputs directory + VerificationKeyFile::new(&package_name).remove(&path)?; + + // Remove the proof from the outputs directory + ProofFile::new(&package_name).remove(&path)?; + + Ok(()) + } +} diff --git a/leo/commands/mod.rs b/leo/commands/mod.rs index 14bc6c664d..152ebb158a 100644 --- a/leo/commands/mod.rs +++ b/leo/commands/mod.rs @@ -1,6 +1,9 @@ pub mod build; pub use self::build::*; +pub mod clean; +pub use self::clean::*; + pub mod deploy; pub use self::deploy::*; diff --git a/leo/errors/files/checksum.rs b/leo/errors/files/checksum.rs index 4fed871e41..65fe78c9e6 100644 --- a/leo/errors/files/checksum.rs +++ b/leo/errors/files/checksum.rs @@ -11,6 +11,9 @@ pub enum ChecksumFileError { #[error("Cannot read from the provided file path - {:?}", _0)] FileReadError(PathBuf), + #[error("Cannot remove the provided file - {:?}", _0)] + FileRemovalError(PathBuf), + #[error("writing: {}", _0)] Writing(io::Error), } diff --git a/leo/errors/files/proof.rs b/leo/errors/files/proof.rs index fe7785cc5a..e0e8b1b022 100644 --- a/leo/errors/files/proof.rs +++ b/leo/errors/files/proof.rs @@ -11,6 +11,9 @@ pub enum ProofFileError { #[error("Cannot read from the provided file path - {:?}", _0)] FileReadError(PathBuf), + #[error("Cannot remove the provided file - {:?}", _0)] + FileRemovalError(PathBuf), + #[error("writing: {}", _0)] Writing(io::Error), } diff --git a/leo/errors/files/proving_key.rs b/leo/errors/files/proving_key.rs index e24bfc6fa8..ef9481551a 100644 --- a/leo/errors/files/proving_key.rs +++ b/leo/errors/files/proving_key.rs @@ -11,6 +11,9 @@ pub enum ProvingKeyFileError { #[error("Cannot read from the provided file path - {:?}", _0)] FileReadError(PathBuf), + #[error("Cannot remove the provided file - {:?}", _0)] + FileRemovalError(PathBuf), + #[error("writing: {}", _0)] Writing(io::Error), } diff --git a/leo/errors/files/verification_key.rs b/leo/errors/files/verification_key.rs index 1003af52ab..ffa3d34a61 100644 --- a/leo/errors/files/verification_key.rs +++ b/leo/errors/files/verification_key.rs @@ -11,6 +11,9 @@ pub enum VerificationKeyFileError { #[error("Cannot read from the provided file path - {:?}", _0)] FileReadError(PathBuf), + #[error("Cannot remove the provided file - {:?}", _0)] + FileRemovalError(PathBuf), + #[error("Verification key file was corrupted")] IncorrectVerificationKey, diff --git a/leo/files/checksum.rs b/leo/files/checksum.rs index d06ab14abc..a8d9053021 100644 --- a/leo/files/checksum.rs +++ b/leo/files/checksum.rs @@ -45,6 +45,18 @@ impl ChecksumFile { Ok(()) } + /// Removes the checksum at the given path if it exists. Returns `true` on success, + /// `false` if the file doesn't exist, and `Error` if the file system fails during operation. + pub fn remove(&self, path: &PathBuf) -> Result { + let path = self.setup_file_path(path); + if !path.exists() { + return Ok(false); + } + + fs::remove_file(&path).map_err(|_| ChecksumFileError::FileRemovalError(path.clone()))?; + Ok(true) + } + fn setup_file_path(&self, path: &PathBuf) -> PathBuf { let mut path = path.to_owned(); if path.is_dir() { diff --git a/leo/files/proof.rs b/leo/files/proof.rs index 0e6fa5578e..75e4f22680 100644 --- a/leo/files/proof.rs +++ b/leo/files/proof.rs @@ -48,6 +48,18 @@ impl ProofFile { Ok(()) } + /// Removes the proof at the given path if it exists. Returns `true` on success, + /// `false` if the file doesn't exist, and `Error` if the file system fails during operation. + pub fn remove(&self, path: &PathBuf) -> Result { + let path = self.setup_file_path(path); + if !path.exists() { + return Ok(false); + } + + fs::remove_file(&path).map_err(|_| ProofFileError::FileRemovalError(path.clone()))?; + Ok(true) + } + fn setup_file_path(&self, path: &PathBuf) -> PathBuf { let mut path = path.to_owned(); if path.is_dir() { diff --git a/leo/files/proving_key.rs b/leo/files/proving_key.rs index 082eedd51f..4c62a5cea9 100644 --- a/leo/files/proving_key.rs +++ b/leo/files/proving_key.rs @@ -45,6 +45,18 @@ impl ProvingKeyFile { Ok(()) } + /// Removes the proving key at the given path if it exists. Returns `true` on success, + /// `false` if the file doesn't exist, and `Error` if the file system fails during operation. + pub fn remove(&self, path: &PathBuf) -> Result { + let path = self.setup_file_path(path); + if !path.exists() { + return Ok(false); + } + + fs::remove_file(&path).map_err(|_| ProvingKeyFileError::FileRemovalError(path.clone()))?; + Ok(true) + } + fn setup_file_path(&self, path: &PathBuf) -> PathBuf { let mut path = path.to_owned(); if path.is_dir() { diff --git a/leo/files/verification_key.rs b/leo/files/verification_key.rs index a74328c82b..6087e2d999 100644 --- a/leo/files/verification_key.rs +++ b/leo/files/verification_key.rs @@ -45,6 +45,18 @@ impl VerificationKeyFile { Ok(()) } + /// Removes the verification key at the given path if it exists. Returns `true` on success, + /// `false` if the file doesn't exist, and `Error` if the file system fails during operation. + pub fn remove(&self, path: &PathBuf) -> Result { + let path = self.setup_file_path(path); + if !path.exists() { + return Ok(false); + } + + fs::remove_file(&path).map_err(|_| VerificationKeyFileError::FileRemovalError(path.clone()))?; + Ok(true) + } + fn setup_file_path(&self, path: &PathBuf) -> PathBuf { let mut path = path.to_owned(); if path.is_dir() { diff --git a/leo/main.rs b/leo/main.rs index b6c14f883a..f11932b0b4 100644 --- a/leo/main.rs +++ b/leo/main.rs @@ -31,6 +31,7 @@ fn main() -> Result<(), CLIError> { RunCommand::new().display_order(8), PublishCommand::new().display_order(9), DeployCommand::new().display_order(10), + CleanCommand::new().display_order(11), ]) .set_term_width(0) .get_matches(); @@ -47,6 +48,7 @@ fn main() -> Result<(), CLIError> { ("run", Some(arguments)) => RunCommand::process(arguments), ("publish", Some(arguments)) => PublishCommand::process(arguments), ("deploy", Some(arguments)) => DeployCommand::process(arguments), + ("clean", Some(arguments)) => CleanCommand::process(arguments), _ => unreachable!(), } }