From 01ad7a219dcf9795092f7ab33b70762c88644fcc Mon Sep 17 00:00:00 2001 From: howardwu Date: Sat, 16 May 2020 19:40:16 -0700 Subject: [PATCH] Adds gitignore file, migrates manifest file under files directory --- leo/commands/build.rs | 3 +- leo/commands/init.rs | 6 ++-- leo/commands/new.rs | 6 ++-- leo/commands/prove.rs | 3 +- leo/commands/setup.rs | 3 +- leo/errors/cli.rs | 9 ++++++ leo/errors/files/gitignore.rs | 19 +++++++++++++ leo/errors/{ => files}/manifest.rs | 0 leo/errors/files/mod.rs | 6 ++++ leo/errors/mod.rs | 3 -- leo/files/gitignore.rs | 45 ++++++++++++++++++++++++++++++ leo/{ => files}/manifest.rs | 20 ++++++------- leo/files/mod.rs | 6 ++++ leo/lib.rs | 1 - 14 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 leo/errors/files/gitignore.rs rename leo/errors/{ => files}/manifest.rs (100%) create mode 100644 leo/files/gitignore.rs rename leo/{ => files}/manifest.rs (72%) diff --git a/leo/commands/build.rs b/leo/commands/build.rs index 2afbc786b1..21f8339cf7 100644 --- a/leo/commands/build.rs +++ b/leo/commands/build.rs @@ -1,7 +1,6 @@ use crate::directories::{source::SOURCE_DIRECTORY_NAME, OutputsDirectory}; use crate::errors::{BuildError, CLIError}; -use crate::files::{ChecksumFile, MainFile, MAIN_FILE_NAME}; -use crate::manifest::Manifest; +use crate::files::{ChecksumFile, MainFile, Manifest, MAIN_FILE_NAME}; use crate::{cli::*, cli_types::*}; use leo_compiler::compiler::Compiler; diff --git a/leo/commands/init.rs b/leo/commands/init.rs index ba5ad01071..2ef866cb3d 100644 --- a/leo/commands/init.rs +++ b/leo/commands/init.rs @@ -1,7 +1,6 @@ use crate::directories::{InputsDirectory, SourceDirectory}; use crate::errors::{CLIError, InitError}; -use crate::files::MainFile; -use crate::manifest::Manifest; +use crate::files::{Gitignore, MainFile, Manifest}; use crate::{cli::*, cli_types::*}; use clap::ArgMatches; @@ -54,6 +53,9 @@ impl CLI for InitCommand { // Create the manifest file Manifest::new(&package_name).write_to(&path)?; + // Create the .gitignore file + Gitignore::new().write_to(&path)?; + // Create the source directory SourceDirectory::create(&path)?; diff --git a/leo/commands/new.rs b/leo/commands/new.rs index 1100eb6e6c..e5b7cb963f 100644 --- a/leo/commands/new.rs +++ b/leo/commands/new.rs @@ -1,7 +1,6 @@ use crate::directories::{InputsDirectory, SourceDirectory}; use crate::errors::{CLIError, NewError}; -use crate::files::MainFile; -use crate::manifest::Manifest; +use crate::files::{Gitignore, MainFile, Manifest}; use crate::{cli::*, cli_types::*}; use clap::ArgMatches; @@ -67,6 +66,9 @@ impl CLI for NewCommand { // Create the manifest file Manifest::new(&package_name).write_to(&path)?; + // Create the .gitignore file + Gitignore::new().write_to(&path)?; + // Create the source directory SourceDirectory::create(&path)?; diff --git a/leo/commands/prove.rs b/leo/commands/prove.rs index 64c75f74a1..1923952d5b 100644 --- a/leo/commands/prove.rs +++ b/leo/commands/prove.rs @@ -1,8 +1,7 @@ use crate::{cli::*, cli_types::*}; use crate::commands::SetupCommand; use crate::errors::CLIError; -use crate::files::ProofFile; -use crate::manifest::Manifest; +use crate::files::{Manifest, ProofFile}; use snarkos_algorithms::snark::{create_random_proof, Proof}; use snarkos_curves::bls12_377::Bls12_377; diff --git a/leo/commands/setup.rs b/leo/commands/setup.rs index ff53eca61c..eb4c3e4cc3 100644 --- a/leo/commands/setup.rs +++ b/leo/commands/setup.rs @@ -1,8 +1,7 @@ use crate::{cli::*, cli_types::*}; use crate::commands::BuildCommand; use crate::errors::{CLIError, VerificationKeyFileError}; -use crate::files::{ProvingKeyFile, VerificationKeyFile}; -use crate::manifest::Manifest; +use crate::files::{Manifest, ProvingKeyFile, VerificationKeyFile}; use leo_compiler::compiler::Compiler; use snarkos_algorithms::snark::{ diff --git a/leo/errors/cli.rs b/leo/errors/cli.rs index be0a8f3cfd..34f85ae53d 100644 --- a/leo/errors/cli.rs +++ b/leo/errors/cli.rs @@ -11,6 +11,9 @@ pub enum CLIError { #[error("{}", _0)] ChecksumFileError(ChecksumFileError), + #[error("{}", _0)] + GitignoreError(GitignoreError), + #[error("{}", _0)] InitError(InitError), @@ -57,6 +60,12 @@ impl From for CLIError { } } +impl From for CLIError { + fn from(error: GitignoreError) -> Self { + CLIError::GitignoreError(error) + } +} + impl From for CLIError { fn from(error: InitError) -> Self { CLIError::InitError(error) diff --git a/leo/errors/files/gitignore.rs b/leo/errors/files/gitignore.rs new file mode 100644 index 0000000000..4b1da2af73 --- /dev/null +++ b/leo/errors/files/gitignore.rs @@ -0,0 +1,19 @@ +use std::io; + +#[derive(Debug, Error)] +pub enum GitignoreError { + #[error("{}: {}", _0, _1)] + Crate(&'static str, String), + + #[error("creating: {}", _0)] + Creating(io::Error), + + #[error("writing: {}", _0)] + Writing(io::Error), +} + +impl From for GitignoreError { + fn from(error: std::io::Error) -> Self { + GitignoreError::Crate("std::io", format!("{}", error)) + } +} diff --git a/leo/errors/manifest.rs b/leo/errors/files/manifest.rs similarity index 100% rename from leo/errors/manifest.rs rename to leo/errors/files/manifest.rs diff --git a/leo/errors/files/mod.rs b/leo/errors/files/mod.rs index 25f48461c9..556bb337ef 100644 --- a/leo/errors/files/mod.rs +++ b/leo/errors/files/mod.rs @@ -1,9 +1,15 @@ pub mod checksum; pub use self::checksum::*; +pub mod gitignore; +pub use self::gitignore::*; + pub mod main; pub use self::main::*; +pub mod manifest; +pub use self::manifest::*; + pub mod proof; pub use self::proof::*; diff --git a/leo/errors/mod.rs b/leo/errors/mod.rs index d642b7be32..64f660cbab 100644 --- a/leo/errors/mod.rs +++ b/leo/errors/mod.rs @@ -9,6 +9,3 @@ pub use self::directory::*; pub mod files; pub use self::files::*; - -pub mod manifest; -pub use self::manifest::*; diff --git a/leo/files/gitignore.rs b/leo/files/gitignore.rs new file mode 100644 index 0000000000..a1fd47ce96 --- /dev/null +++ b/leo/files/gitignore.rs @@ -0,0 +1,45 @@ +//! The `.gitignore` file. + +use crate::errors::GitignoreError; + +use serde::Deserialize; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +pub static GITIGNORE_FILE_NAME: &str = ".gitignore"; + +#[derive(Deserialize)] +pub struct Gitignore; + +impl Gitignore { + pub fn new() -> Self { + Self + } + + pub fn exists_at(path: &PathBuf) -> bool { + let mut path = path.to_owned(); + if path.is_dir() { + path.push(PathBuf::from(GITIGNORE_FILE_NAME)); + } + path.exists() + } + + pub fn write_to(self, path: &PathBuf) -> Result<(), GitignoreError> { + let mut path = path.to_owned(); + if path.is_dir() { + path.push(PathBuf::from(GITIGNORE_FILE_NAME)); + } + + let mut file = File::create(&path)?; + Ok(file.write_all(self.template().as_bytes())?) + } + + fn template(&self) -> String { + format!( + r#"/output +/.leo +"#, + ) + } +} diff --git a/leo/manifest.rs b/leo/files/manifest.rs similarity index 72% rename from leo/manifest.rs rename to leo/files/manifest.rs index 1724140def..277f4acb66 100644 --- a/leo/manifest.rs +++ b/leo/files/manifest.rs @@ -7,7 +7,7 @@ use std::io::Read; use std::io::Write; use std::path::PathBuf; -pub static FILE_NAME_DEFAULT: &str = "Leo.toml"; +pub static MANIFEST_FILE_NAME: &str = "Leo.toml"; #[derive(Deserialize)] pub struct Package { @@ -33,7 +33,7 @@ impl Manifest { pub fn exists_at(path: &PathBuf) -> bool { let mut path = path.to_owned(); if path.is_dir() { - path.push(PathBuf::from(FILE_NAME_DEFAULT)); + path.push(PathBuf::from(MANIFEST_FILE_NAME)); } path.exists() } @@ -45,13 +45,13 @@ impl Manifest { pub fn write_to(self, path: &PathBuf) -> Result<(), ManifestError> { let mut path = path.to_owned(); if path.is_dir() { - path.push(PathBuf::from(FILE_NAME_DEFAULT)); + path.push(PathBuf::from(MANIFEST_FILE_NAME)); } let mut file = File::create(&path) - .map_err(|error| ManifestError::Creating(FILE_NAME_DEFAULT, error))?; + .map_err(|error| ManifestError::Creating(MANIFEST_FILE_NAME, error))?; file.write_all(self.template().as_bytes()) - .map_err(|error| ManifestError::Writing(FILE_NAME_DEFAULT, error)) + .map_err(|error| ManifestError::Writing(MANIFEST_FILE_NAME, error)) } fn template(&self) -> String { @@ -71,21 +71,21 @@ impl TryFrom<&PathBuf> for Manifest { fn try_from(path: &PathBuf) -> Result { let mut path = path.to_owned(); if path.is_dir() { - path.push(PathBuf::from(FILE_NAME_DEFAULT)); + path.push(PathBuf::from(MANIFEST_FILE_NAME)); } let mut file = - File::open(path).map_err(|error| ManifestError::Opening(FILE_NAME_DEFAULT, error))?; + File::open(path).map_err(|error| ManifestError::Opening(MANIFEST_FILE_NAME, error))?; let size = file .metadata() - .map_err(|error| ManifestError::Metadata(FILE_NAME_DEFAULT, error))? + .map_err(|error| ManifestError::Metadata(MANIFEST_FILE_NAME, error))? .len() as usize; let mut buffer = String::with_capacity(size); file.read_to_string(&mut buffer) - .map_err(|error| ManifestError::Reading(FILE_NAME_DEFAULT, error))?; + .map_err(|error| ManifestError::Reading(MANIFEST_FILE_NAME, error))?; Ok(toml::from_str(&buffer) - .map_err(|error| ManifestError::Parsing(FILE_NAME_DEFAULT, error))?) + .map_err(|error| ManifestError::Parsing(MANIFEST_FILE_NAME, error))?) } } diff --git a/leo/files/mod.rs b/leo/files/mod.rs index 25f48461c9..556bb337ef 100644 --- a/leo/files/mod.rs +++ b/leo/files/mod.rs @@ -1,9 +1,15 @@ pub mod checksum; pub use self::checksum::*; +pub mod gitignore; +pub use self::gitignore::*; + pub mod main; pub use self::main::*; +pub mod manifest; +pub use self::manifest::*; + pub mod proof; pub use self::proof::*; diff --git a/leo/lib.rs b/leo/lib.rs index abd38d0f5e..5478ccca01 100644 --- a/leo/lib.rs +++ b/leo/lib.rs @@ -9,4 +9,3 @@ pub mod directories; pub mod errors; pub mod files; pub mod logger; -pub mod manifest;