Add README.md generation to leo init

This commit is contained in:
raychu86 2020-08-16 19:48:25 -07:00
parent 953de07b56
commit 9428a6a41a
7 changed files with 104 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use crate::{
};
use leo_package::{
inputs::*,
root::{Gitignore, Manifest},
root::{Gitignore, Manifest, README},
source::{LibFile, MainFile, SourceDirectory},
};
@ -58,6 +58,9 @@ impl CLI for InitCommand {
// Create the .gitignore file
Gitignore::new().write_to(&path)?;
// Create the README.md file
README::new(&package_name).write_to(&path)?;
// Create the source directory
SourceDirectory::create(&path)?;

View File

@ -63,6 +63,9 @@ pub enum CLIError {
#[error("{}", _0)]
PublishError(PublishError),
#[error("{}", _0)]
READMEError(READMEError),
#[error("{}", _0)]
RunError(RunError),
@ -215,6 +218,13 @@ impl From<PublishError> for CLIError {
}
}
impl From<READMEError> for CLIError {
fn from(error: READMEError) -> Self {
log::error!("{}\n", error);
CLIError::READMEError(error)
}
}
impl From<RunError> for CLIError {
fn from(error: RunError) -> Self {
log::error!("{}\n", error);

View File

@ -4,5 +4,8 @@ pub use self::gitignore::*;
pub mod manifest;
pub use self::manifest::*;
pub mod readme;
pub use self::readme::*;
pub mod zip;
pub use self::zip::*;

View File

@ -0,0 +1,19 @@
use std::io;
#[derive(Debug, Error)]
pub enum READMEError {
#[error("{}: {}", _0, _1)]
Crate(&'static str, String),
#[error("creating: {}", _0)]
Creating(io::Error),
#[error("writing: {}", _0)]
Writing(io::Error),
}
impl From<std::io::Error> for READMEError {
fn from(error: std::io::Error) -> Self {
READMEError::Crate("std::io", format!("{}", error))
}
}

View File

@ -14,6 +14,9 @@ pub const MANIFEST_FILE_NAME: &str = "Leo.toml";
pub struct Package {
pub name: String,
pub version: String,
pub description: Option<String>,
pub license: Option<String>,
pub remote: Option<String>,
}
#[derive(Deserialize)]
@ -27,6 +30,9 @@ impl Manifest {
package: Package {
name: package_name.to_owned(),
version: "0.1.0".to_owned(),
description: None,
license: None,
remote: None,
},
}
}
@ -47,6 +53,18 @@ impl Manifest {
self.package.version.clone()
}
pub fn get_package_description(&self) -> Option<String> {
self.package.description.clone()
}
pub fn get_package_license(&self) -> Option<String> {
self.package.license.clone()
}
pub fn get_package_remote(&self) -> Option<String> {
self.package.remote.clone()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), ManifestError> {
let mut path = path.to_owned();
if path.is_dir() {

View File

@ -4,5 +4,8 @@ pub use self::gitignore::*;
pub mod manifest;
pub use self::manifest::*;
pub mod readme;
pub use self::readme::*;
pub mod zip;
pub use self::zip::*;

View File

@ -0,0 +1,47 @@
//! The `README.md` file.
use crate::errors::READMEError;
use serde::Deserialize;
use std::{fs::File, io::Write, path::PathBuf};
pub static README_FILE_NAME: &str = "README.md";
#[derive(Deserialize)]
pub struct README {
pub package_name: String,
}
impl README {
pub fn new(package_name: &str) -> Self {
Self {
package_name: package_name.to_string(),
}
}
pub fn package_name(&self) -> String {
self.package_name.clone()
}
pub fn exists_at(path: &PathBuf) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
path.push(PathBuf::from(README_FILE_NAME));
}
path.exists()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), READMEError> {
let mut path = path.to_owned();
if path.is_dir() {
path.push(PathBuf::from(README_FILE_NAME));
}
let mut file = File::create(&path)?;
Ok(file.write_all(self.template().as_bytes())?)
}
fn template(&self) -> String {
format!("# {}\n", self.package_name)
}
}