Update initialize to verify first, then initialize package

This commit is contained in:
howardwu 2020-08-29 00:24:13 -07:00
parent 9884320dd6
commit d67b5109d4
12 changed files with 107 additions and 62 deletions

View File

@ -58,17 +58,12 @@ impl CLI for InitCommand {
.to_string_lossy()
.to_string();
// Verify the directory exists
// Verify the directory does not exist
if !path.exists() {
return Err(InitError::DirectoryDoesNotExist(path.as_os_str().to_owned()).into());
}
// Verify a manifest file does not already exist
if LeoPackage::exists_at(&path) {
return Err(InitError::PackageAlreadyExists(path.as_os_str().to_owned()).into());
}
LeoPackage::create(&package_name, options, &path)?;
LeoPackage::initialize(&package_name, options, &path)?;
tracing::info!("Successfully initialized package \"{}\"\n", package_name);

View File

@ -85,7 +85,7 @@ impl CLI for NewCommand {
fs::create_dir_all(&path)
.map_err(|error| NewError::CreatingRootDirectory(path.as_os_str().to_owned(), error))?;
LeoPackage::create(&package_name, options.1, &path)?;
LeoPackage::initialize(&package_name, options.1, &path)?;
tracing::info!("Successfully initialized package \"{}\"\n", package_name);

View File

@ -29,9 +29,6 @@ pub enum InitError {
#[error("{}", _0)]
ManifestError(#[from] ManifestError),
#[error("package at path {:?} already exists", _0)]
PackageAlreadyExists(OsString),
#[error("package name is missing - {:?}", _0)]
ProjectNameInvalid(OsString),
}

View File

@ -29,9 +29,6 @@ pub enum NewError {
#[error("{}", _0)]
ManifestError(#[from] ManifestError),
#[error("package at path {:?} already exists", _0)]
PackageAlreadyExists(OsString),
#[error("package name is missing - {:?}", _0)]
ProjectNameInvalid(OsString),
}

View File

@ -1,4 +1,4 @@
use std::io;
use std::{ffi::OsString, io};
#[derive(Debug, Error)]
pub enum PackageError {
@ -8,6 +8,9 @@ pub enum PackageError {
#[error("`{}` creating: {}", _0, _1)]
Creating(&'static str, io::Error),
#[error("{:?} at path {:?} already exists", _0, _1)]
FileAlreadyExists(String, OsString),
#[error("`{}` metadata: {}", _0, _1)]
Removing(&'static str, io::Error),
}
@ -20,66 +23,66 @@ impl From<std::io::Error> for PackageError {
impl From<crate::errors::GitignoreError> for PackageError {
fn from(error: crate::errors::GitignoreError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::InputFileError> for PackageError {
fn from(error: crate::errors::InputFileError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::InputsDirectoryError> for PackageError {
fn from(error: crate::errors::InputsDirectoryError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::ImportsDirectoryError> for PackageError {
fn from(error: crate::errors::ImportsDirectoryError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::OutputsDirectoryError> for PackageError {
fn from(error: crate::errors::OutputsDirectoryError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::READMEError> for PackageError {
fn from(error: crate::errors::READMEError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::SourceDirectoryError> for PackageError {
fn from(error: crate::errors::SourceDirectoryError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::StateFileError> for PackageError {
fn from(error: crate::errors::StateFileError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::LibFileError> for PackageError {
fn from(error: crate::errors::LibFileError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::ManifestError> for PackageError {
fn from(error: crate::errors::ManifestError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}
impl From<crate::errors::MainFileError> for PackageError {
fn from(error: crate::errors::MainFileError) -> Self {
PackageError::Crate("leo_package", format!("{}", error))
PackageError::Crate("leo-package", format!("{}", error))
}
}

View File

@ -39,6 +39,10 @@ impl InputFile {
}
}
pub fn filename(self) -> String {
format!("{}{}", self.package_name, INPUT_FILE_EXTENSION)
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
let path = self.setup_file_path(path);
path.exists()

View File

@ -39,6 +39,10 @@ impl StateFile {
}
}
pub fn filename(self) -> String {
format!("{}{}", self.package_name, STATE_FILE_EXTENSION)
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
let path = self.setup_file_path(path);
path.exists()

View File

@ -32,9 +32,9 @@ use std::path::PathBuf;
pub struct LeoPackage;
impl LeoPackage {
/// Creates a Leo package at the given path.
pub fn create(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
package::Package::create_package(package_name, is_lib, path)
/// Initalizes a Leo package at the given path.
pub fn initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
package::Package::initialize(package_name, is_lib, path)
}
/// Returns `true` if a Leo package exists at the given path.

View File

@ -32,48 +32,81 @@ impl Package {
}
/// Creates a package at the given path
pub fn create_package(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
// Create the manifest file
Manifest::new(&package_name).write_to(&path)?;
pub fn initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
// First, verify that this directory is not already initialized as a Leo package.
{
// Verify the manifest file does not already exist.
if Manifest::exists_at(&path) {
return Err(PackageError::FileAlreadyExists(Manifest::filename(), path.as_os_str().to_owned()).into());
}
// Create the .gitignore file
Gitignore::new().write_to(&path)?;
if is_lib {
// Verify the library file does not exist.
if LibFile::exists_at(&path) {
return Err(
PackageError::FileAlreadyExists(LibFile::filename(), path.as_os_str().to_owned()).into(),
);
}
} else {
// Verify the input file does not exist.
let input_file = InputFile::new(&package_name);
if input_file.exists_at(&path) {
return Err(
PackageError::FileAlreadyExists(input_file.filename(), path.as_os_str().to_owned()).into(),
);
}
// Create the README.md file
README::new(&package_name).write_to(&path)?;
// Verify the state file does not exist.
let state_file = StateFile::new(&package_name);
if state_file.exists_at(&path) {
return Err(
PackageError::FileAlreadyExists(state_file.filename(), path.as_os_str().to_owned()).into(),
);
}
// Create the source directory
SourceDirectory::create(&path)?;
// Verify the main file does not exist.
if MainFile::exists_at(&path) {
return Err(
PackageError::FileAlreadyExists(MainFile::filename(), path.as_os_str().to_owned()).into(),
);
}
}
}
// Next, initialize this directory as a Leo package.
{
// Create the manifest file.
Manifest::new(&package_name).write_to(&path)?;
// Create a new library or binary file
// Verify that the .gitignore file does not exist.
if !Gitignore::exists_at(&path) {
// Create the .gitignore file.
Gitignore::new().write_to(&path)?;
}
if is_lib {
// Verify the library file does not exist
if !LibFile::exists_at(&path) {
// Create the library file in the source directory
// Verify that the README.md file does not exist.
if !README::exists_at(&path) {
// Create the README.md file.
README::new(package_name).write_to(&path)?;
}
// Create the source directory.
SourceDirectory::create(&path)?;
// Create a new library or binary file.
if is_lib {
// Create the library file in the source directory.
LibFile::new(&package_name).write_to(&path)?;
}
} else {
// Create the input directory
InputsDirectory::create(&path)?;
} else {
// Create the input directory.
InputsDirectory::create(&path)?;
// Verify the input file does not exist
let input_file = InputFile::new(&package_name);
if !input_file.exists_at(&path) {
// Create the input file in the inputs directory
input_file.write_to(&path)?;
}
// Create the input file in the inputs directory.
InputFile::new(&package_name).write_to(&path)?;
// Verify the state file does not exist
let state_file = StateFile::new(&package_name);
if !state_file.exists_at(&path) {
// Create the state file in the inputs directory
state_file.write_to(&path)?;
}
// Create the state file in the inputs directory.
StateFile::new(&package_name).write_to(&path)?;
// Verify the main file does not exist
if !MainFile::exists_at(&path) {
// Create the main file in the source directory
// Create the main file in the source directory.
MainFile::new(&package_name).write_to(&path)?;
}
}

View File

@ -45,6 +45,10 @@ impl Manifest {
}
}
pub fn filename() -> String {
MANIFEST_FILE_NAME.to_string()
}
pub fn exists_at(path: &PathBuf) -> bool {
let mut path = path.to_owned();
if path.is_dir() {

View File

@ -35,6 +35,10 @@ impl LibFile {
}
}
pub fn filename() -> String {
LIB_FILE_NAME.to_string()
}
pub fn exists_at(path: &PathBuf) -> bool {
let mut path = path.to_owned();
if path.is_dir() {

View File

@ -35,6 +35,10 @@ impl MainFile {
}
}
pub fn filename() -> String {
MAIN_FILE_NAME.to_string()
}
pub fn exists_at(path: &PathBuf) -> bool {
let mut path = path.to_owned();
if path.is_dir() {