Skip to content

Commit

Permalink
Merge fb5e799 into d55b673
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed May 10, 2016
2 parents d55b673 + fb5e799 commit a2503ab
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ extern crate log;
pub use cobalt::build;
pub use error::Error;
pub use config::Config;
pub use new::create_new_project;

// modules
mod cobalt;
mod config;
pub mod error;
mod document;
mod new;
23 changes: 22 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use log::{LogRecord, LogLevelFilter};
use env_logger::LogBuilder;
use nickel::{Nickel, Options as NickelOptions, StaticFilesHandler};
use ghp::import_dir;
use cobalt::create_new_project;

use notify::{RecommendedWatcher, Error, Watcher};
use std::sync::mpsc::channel;
Expand All @@ -30,7 +31,8 @@ fn print_version() {
}

fn print_usage(opts: Options) {
let usage = concat!("\n\tbuild -- build the cobalt project at the source dir",
let usage = concat!("\n\tnew -- create a new cobalt project",
"\n\tbuild -- build the cobalt project at the source dir",
"\n\tserve -- build and serve the cobalt project at the source dir",
"\n\twatch -- build, serve, and watch the project at the source dir",
"\n\timport -- moves the contents of the dest folder to the gh-pages branch");
Expand Down Expand Up @@ -208,6 +210,25 @@ fn main() {
import(&config, &branch, &message);
}

"new" => {
if matches.free.len() == 2 {
let dest = matches.free[1].clone();

match create_new_project(&dest) {
Ok(_) => info!("Created new project at {}", dest),
Err(e) => {
error!("{}", e);
error!("Could not create a new cobalt project");
std::process::exit(1);
}
}
} else {
println!("cobalt new DIRECTORY");
print_usage(opts);
std::process::exit(1);
}
}

_ => {
print_usage(opts);
return;
Expand Down
125 changes: 125 additions & 0 deletions src/new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#![allow(non_upper_case_globals)]

const cobalt_yml: &'static [u8] = b"name: cobalt blog
source: \".\"
dest: \"build\"
ignore:
- ./.git/*
- ./build/*
";

const default_liquid: &'static [u8] = b"<!DOCTYPE html>
<html>
<head>
<meta charset=\"utf-8\">
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
{% if is_post %}
<title>{{ title }}</title>
{% else %}
<title>Cobalt.rs Blog</title>
{% endif %}
<!-- Latest compiled and minified CSS -->
<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\" integrity=\"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7\" crossorigin=\"anonymous\">
<!-- Optional theme -->
<!--<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css\" integrity=\"sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r\" crossorigin=\"anonymous\">-->
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>
<!-- Latest compiled and minified JavaScript -->
<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\" integrity=\"sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS\" crossorigin=\"anonymous\"></script>
</head>
<body>
<div>
{% if is_post %}
{% include '_layouts/post.liquid' %}
{% else %}
{{ content }}
{% endif %}
</div>
</body>
</html>
";

const post_liquid: &'static [u8] = b"<div>
<h2>{{ title }}</h2>
<p>
{{content}}
</p>
</div>
";

const post_1_md: &'static [u8] = b"extends: default.liquid
title: First Post
date: 14 January 2016 21:00:30 -0500
---
# This is our first Post!
Welcome to the first post ever on cobalt.rs!
";

const index_liquid: &'static [u8] = b"extends: default.liquid
---
<div >
<h2>Blog!</h2>
<!--<br />-->
<div>
{% for post in posts %}
<div>
<h4>{{post.title}}</h4>
<h4><a href=\"{{post.path}}\">{{ post.title }}</a></h4>
</div>
{% endfor %}
</div>
</div>
";

use std::path::Path;
use std::fs::{DirBuilder, OpenOptions};
use std::io::Write;
use error::Result;

pub fn create_new_project<P: AsRef<Path>>(dest: P) -> Result<()> {
let dest = dest.as_ref();

try!(create_folder(&dest));

try!(create_file(&dest.join(".coblat.yml"), cobalt_yml));
try!(create_file(&dest.join("index.liquid"), index_liquid));

try!(create_folder(&dest.join("_layouts")));
try!(create_file(&dest.join("_layouts/default.liquid"), default_liquid));
try!(create_file(&dest.join("_layouts/post.liquid"), post_liquid));

try!(create_folder(&dest.join("_posts")));
try!(create_file(&dest.join("_posts/post-1.md"), post_1_md));

Ok(())
}

fn create_folder<P: AsRef<Path>>(path: P) -> Result<()> {
trace!("Creating folder {:?}", &path.as_ref());

try!(DirBuilder::new()
.recursive(true)
.create(path));

Ok(())
}

fn create_file<P: AsRef<Path>>(name: P, content: &[u8]) -> Result<()> {
trace!("Creating file {:?}", &name.as_ref());

let mut file = try!(OpenOptions::new()
.write(true)
.create(true)
.open(name));

try!(file.write_all(content));

Ok(())
}

0 comments on commit a2503ab

Please sign in to comment.