2020-01-21 19:52:24 +00:00
|
|
|
use std::env;
|
2020-10-03 14:43:02 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-03-03 08:12:40 +00:00
|
|
|
use std::time::Instant;
|
2017-03-25 06:52:51 +00:00
|
|
|
|
2018-10-19 14:33:11 +00:00
|
|
|
use utils::net::{get_available_port, port_is_available};
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
mod cli;
|
2016-12-06 05:51:33 +00:00
|
|
|
mod cmd;
|
2017-03-25 06:52:51 +00:00
|
|
|
mod console;
|
2017-07-27 09:24:43 +00:00
|
|
|
mod prompt;
|
2016-12-06 06:55:17 +00:00
|
|
|
|
2016-12-06 05:51:33 +00:00
|
|
|
fn main() {
|
2017-07-15 04:24:31 +00:00
|
|
|
let matches = cli::build_cli().get_matches();
|
2016-12-06 05:51:33 +00:00
|
|
|
|
2020-01-21 19:52:24 +00:00
|
|
|
let root_dir = match matches.value_of("root").unwrap() {
|
|
|
|
"." => env::current_dir().unwrap(),
|
2020-08-16 16:39:04 +00:00
|
|
|
path => PathBuf::from(path)
|
|
|
|
.canonicalize()
|
2020-09-21 19:43:02 +00:00
|
|
|
.unwrap_or_else(|_| panic!("Cannot find root directory: {}", path)),
|
2020-01-21 19:52:24 +00:00
|
|
|
};
|
2020-05-23 09:55:45 +00:00
|
|
|
let config_file = match matches.value_of("config") {
|
|
|
|
Some(path) => PathBuf::from(path),
|
|
|
|
None => root_dir.join("config.toml"),
|
|
|
|
};
|
2017-03-25 04:18:15 +00:00
|
|
|
|
2016-12-06 05:51:33 +00:00
|
|
|
match matches.subcommand() {
|
2016-12-19 07:58:03 +00:00
|
|
|
("init", Some(matches)) => {
|
2020-06-18 19:15:46 +00:00
|
|
|
let force = matches.is_present("force");
|
|
|
|
match cmd::create_new_project(matches.value_of("name").unwrap(), force) {
|
2017-07-27 09:24:43 +00:00
|
|
|
Ok(()) => (),
|
2017-05-12 14:10:21 +00:00
|
|
|
Err(e) => {
|
|
|
|
console::unravel_errors("Failed to create the project", &e);
|
|
|
|
::std::process::exit(1);
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2016-12-06 08:27:03 +00:00
|
|
|
};
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2017-10-05 01:56:13 +00:00
|
|
|
("build", Some(matches)) => {
|
2017-03-25 06:52:51 +00:00
|
|
|
console::info("Building site...");
|
2017-03-03 08:12:40 +00:00
|
|
|
let start = Instant::now();
|
2020-10-03 14:43:02 +00:00
|
|
|
let output_dir = matches.value_of("output_dir").map(|output_dir| Path::new(output_dir));
|
2019-08-24 20:23:08 +00:00
|
|
|
match cmd::build(
|
2020-01-21 19:52:24 +00:00
|
|
|
&root_dir,
|
2020-05-23 09:55:45 +00:00
|
|
|
&config_file,
|
2019-08-24 20:23:08 +00:00
|
|
|
matches.value_of("base_url"),
|
2020-10-03 14:43:02 +00:00
|
|
|
output_dir,
|
2019-08-24 20:23:08 +00:00
|
|
|
matches.is_present("drafts"),
|
|
|
|
) {
|
2017-05-12 14:10:21 +00:00
|
|
|
Ok(()) => console::report_elapsed_time(start),
|
|
|
|
Err(e) => {
|
|
|
|
console::unravel_errors("Failed to build the site", &e);
|
|
|
|
::std::process::exit(1);
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2016-12-06 05:51:33 +00:00
|
|
|
};
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
("serve", Some(matches)) => {
|
|
|
|
let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
|
2018-10-19 14:33:11 +00:00
|
|
|
let mut port: u16 = match matches.value_of("port").unwrap().parse() {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => {
|
|
|
|
console::error("The request port needs to be an integer");
|
|
|
|
::std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2019-06-21 18:02:50 +00:00
|
|
|
let watch_only = matches.is_present("watch_only");
|
2019-07-04 21:42:37 +00:00
|
|
|
let open = matches.is_present("open");
|
2019-08-24 20:23:08 +00:00
|
|
|
let include_drafts = matches.is_present("drafts");
|
2020-08-16 16:39:04 +00:00
|
|
|
let fast = matches.is_present("fast");
|
2019-06-21 18:02:50 +00:00
|
|
|
|
2018-10-19 14:33:11 +00:00
|
|
|
// Default one
|
2019-06-21 18:02:50 +00:00
|
|
|
if port != 1111 && !watch_only && !port_is_available(port) {
|
2018-10-19 14:33:11 +00:00
|
|
|
console::error("The requested port is not available");
|
|
|
|
::std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
2019-07-19 09:10:28 +00:00
|
|
|
if !watch_only && !port_is_available(port) {
|
2018-10-19 14:33:11 +00:00
|
|
|
port = if let Some(p) = get_available_port(1111) {
|
|
|
|
p
|
|
|
|
} else {
|
|
|
|
console::error("No port available.");
|
|
|
|
::std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 14:43:02 +00:00
|
|
|
let output_dir = matches.value_of("output_dir").map(|output_dir| Path::new(output_dir));
|
2018-02-02 16:18:07 +00:00
|
|
|
let base_url = matches.value_of("base_url").unwrap();
|
2017-03-25 06:52:51 +00:00
|
|
|
console::info("Building site...");
|
2019-08-24 20:23:08 +00:00
|
|
|
match cmd::serve(
|
2020-01-21 19:52:24 +00:00
|
|
|
&root_dir,
|
2019-08-24 20:23:08 +00:00
|
|
|
interface,
|
|
|
|
port,
|
2020-10-03 14:43:02 +00:00
|
|
|
output_dir,
|
2019-08-24 20:23:08 +00:00
|
|
|
base_url,
|
2020-05-23 09:55:45 +00:00
|
|
|
&config_file,
|
2019-08-24 20:23:08 +00:00
|
|
|
watch_only,
|
|
|
|
open,
|
|
|
|
include_drafts,
|
2020-08-16 16:39:04 +00:00
|
|
|
fast,
|
2019-08-24 20:23:08 +00:00
|
|
|
) {
|
2017-03-06 10:35:56 +00:00
|
|
|
Ok(()) => (),
|
2017-05-12 14:10:21 +00:00
|
|
|
Err(e) => {
|
2017-05-20 15:00:41 +00:00
|
|
|
console::unravel_errors("", &e);
|
2017-05-12 14:10:21 +00:00
|
|
|
::std::process::exit(1);
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
};
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2019-05-27 12:05:07 +00:00
|
|
|
("check", Some(matches)) => {
|
|
|
|
console::info("Checking site...");
|
|
|
|
let start = Instant::now();
|
|
|
|
match cmd::check(
|
2020-01-21 19:52:24 +00:00
|
|
|
&root_dir,
|
2020-05-23 09:55:45 +00:00
|
|
|
&config_file,
|
2019-05-27 12:05:07 +00:00
|
|
|
matches.value_of("base_path"),
|
|
|
|
matches.value_of("base_url"),
|
2019-08-24 20:23:08 +00:00
|
|
|
matches.is_present("drafts"),
|
2019-05-27 12:05:07 +00:00
|
|
|
) {
|
|
|
|
Ok(()) => console::report_elapsed_time(start),
|
|
|
|
Err(e) => {
|
|
|
|
console::unravel_errors("Failed to check the site", &e);
|
|
|
|
::std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-12-06 05:51:33 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|