Skip to content

Commit

Permalink
Create new project
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Prouillet committed Dec 6, 2016
0 parents commit 021b8ea
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[*.rs]
end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 4
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.idea/
257 changes: 257 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "gutenberg"
version = "0.1.0"
authors = ["Vincent Prouillet <[email protected]>"]
license = "MIT"
readme = "README.md"
description = "Static site generator"
homepage = "https://github.com/Keats/gutenberg"
repository = "https://github.com/Keats/gutenberg"
keywords = ["static", "site", "generator", "blog"]

[dependencies]
error-chain = "0.7"
clap = "2.19"
clippy = {version = "~0.0.103", optional = true}

[dependencies.toml]
version = "0.2"
default-features = false
features = ["serde"]

[features]
default = []
dev = ["clippy"]

3 changes: 3 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod new;

pub use self::new::create_new_project;
41 changes: 41 additions & 0 deletions src/cmd/new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

use std::io::prelude::*;
use std::fs::{create_dir, File};
use std::path::Path;

use errors::{Result, ErrorKind};


const CONFIG: &'static str = r#"
title = "My site"
base_url = "https://replace-this-with-your-url.com"
"#;


pub fn create_new_project<P: AsRef<Path>>(name: P) -> Result<()> {
let path = name.as_ref();
// Better error message than the rust default
if path.exists() && path.is_dir() {
return Err(ErrorKind::FolderExists(path.to_string_lossy().to_string()).into());
}

// main folder
create_dir(path)?;
create_file(path.join("config.toml"), CONFIG.trim_left())?;

// content folder
create_dir(path.join("content"))?;

// layouts folder
create_dir(path.join("layouts"))?;

create_dir(path.join("static"))?;

Ok(())
}

fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
let mut file = File::create(&path)?;
file.write_all(content.as_bytes())?;
Ok(())
}
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// use std::path::Path;

// use errors::{Result};


#[derive(Debug, PartialEq)]
pub struct Config {
pub title: String,
pub base_url: String,
}


//impl Config {
// pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
// Ok(())
// }
//}
13 changes: 13 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

error_chain! {
foreign_links {
Io(::std::io::Error);
}

errors {
FolderExists(name: String) {
description("folder already exists")
display("Folder '{}' already exists.", name)
}
}
}
Loading

0 comments on commit 021b8ea

Please sign in to comment.