zola/src/bin/gutenberg.rs

101 lines
3.2 KiB
Rust
Raw Normal View History

2017-02-23 08:34:57 +00:00
#[macro_use]
extern crate clap;
#[macro_use]
extern crate error_chain;
2017-03-08 00:13:50 +00:00
extern crate gutenberg;
extern crate chrono;
2017-03-25 06:52:51 +00:00
extern crate term_painter;
2017-03-06 10:35:56 +00:00
extern crate staticfile;
extern crate iron;
extern crate mount;
extern crate notify;
extern crate ws;
2016-12-06 05:51:33 +00:00
use std::time::Instant;
2017-03-25 06:52:51 +00:00
use chrono::Duration;
2017-03-25 06:52:51 +00:00
use gutenberg::errors::Error;
2016-12-06 05:51:33 +00:00
mod cmd;
2017-03-25 06:52:51 +00:00
mod console;
2016-12-06 06:55:17 +00:00
2016-12-06 05:51:33 +00:00
2017-03-25 06:52:51 +00:00
/// Print the time elapsed rounded to 1 decimal
2017-03-10 11:39:58 +00:00
fn report_elapsed_time(instant: Instant) {
let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
if duration_ms < 1000.0 {
2017-03-25 06:52:51 +00:00
console::success(&format!("Done in {}ms.\n", duration_ms));
2017-03-10 11:39:58 +00:00
} else {
let duration_sec = duration_ms / 1000.0;
2017-03-25 06:52:51 +00:00
console::success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
2017-03-10 11:39:58 +00:00
}
}
2017-03-25 06:52:51 +00:00
////Display an error message, the actual error and then exits if requested
2017-03-25 06:59:12 +00:00
fn unravel_errors(message: &str, error: &Error, exit: bool) {
2017-03-25 06:52:51 +00:00
console::error(message);
console::error(&format!("Error: {}", error));
for e in error.iter().skip(1) {
console::error(&format!("Reason: {}", e));
}
if exit {
::std::process::exit(1);
}
}
2016-12-06 05:51:33 +00:00
fn main() {
2017-02-23 08:34:57 +00:00
let matches = clap_app!(Gutenberg =>
2016-12-06 05:51:33 +00:00
(version: crate_version!())
(author: "Vincent Prouillet")
(about: "Static site generator")
(@setting SubcommandRequiredElseHelp)
(@arg config: -c --config +takes_value "Path to a config file other than config.toml")
2017-02-23 08:34:57 +00:00
(@subcommand init =>
2016-12-06 05:51:33 +00:00
(about: "Create a new Gutenberg project")
(@arg name: +required "Name of the project. Will create a directory with that name in the current directory")
)
2016-12-06 08:27:03 +00:00
(@subcommand build =>
(about: "Builds the site")
)
(@subcommand serve =>
(about: "Serve the site. Rebuild and reload on change automatically")
(@arg interface: "Interface to bind on (default to 127.0.0.1)")
(@arg port: "Which port to use (default to 1111)")
)
2016-12-06 05:51:33 +00:00
).get_matches();
let config_file = matches.value_of("config").unwrap_or("config.toml");
2016-12-06 05:51:33 +00:00
match matches.subcommand() {
2016-12-19 07:58:03 +00:00
("init", Some(matches)) => {
2016-12-06 05:51:33 +00:00
match cmd::create_new_project(matches.value_of("name").unwrap()) {
2017-03-25 06:52:51 +00:00
Ok(()) => console::success("Project created"),
2017-03-25 06:59:12 +00:00
Err(e) => unravel_errors("Failed to create the project", &e, true),
2016-12-06 08:27:03 +00:00
};
},
("build", Some(_)) => {
2017-03-25 06:52:51 +00:00
console::info("Building site...");
let start = Instant::now();
2017-03-25 06:59:12 +00:00
match cmd::build(config_file) {
2017-03-25 06:52:51 +00:00
Ok(()) => report_elapsed_time(start),
2017-03-25 06:59:12 +00:00
Err(e) => unravel_errors("Failed to build the site", &e, true),
2016-12-06 05:51:33 +00:00
};
},
("serve", Some(matches)) => {
let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
let port = matches.value_of("port").unwrap_or("1111");
2017-03-25 06:52:51 +00:00
console::info("Building site...");
2017-03-25 06:59:12 +00:00
match cmd::serve(interface, port, config_file) {
2017-03-06 10:35:56 +00:00
Ok(()) => (),
2017-03-25 06:59:12 +00:00
Err(e) => unravel_errors("Failed to build the site", &e, true),
};
},
2016-12-06 05:51:33 +00:00
_ => unreachable!(),
}
}