You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2KB

  1. #[macro_use]
  2. extern crate clap;
  3. #[macro_use]
  4. extern crate error_chain;
  5. extern crate gutenberg;
  6. extern crate chrono;
  7. extern crate term_painter;
  8. extern crate staticfile;
  9. extern crate iron;
  10. extern crate mount;
  11. extern crate notify;
  12. extern crate ws;
  13. use std::time::Instant;
  14. use chrono::Duration;
  15. use gutenberg::errors::Error;
  16. mod cmd;
  17. mod console;
  18. /// Print the time elapsed rounded to 1 decimal
  19. fn report_elapsed_time(instant: Instant) {
  20. let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
  21. if duration_ms < 1000.0 {
  22. console::success(&format!("Done in {}ms.\n", duration_ms));
  23. } else {
  24. let duration_sec = duration_ms / 1000.0;
  25. console::success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
  26. }
  27. }
  28. ////Display an error message, the actual error and then exits if requested
  29. fn unravel_errors(message: &str, error: Error, exit: bool) {
  30. console::error(message);
  31. console::error(&format!("Error: {}", error));
  32. for e in error.iter().skip(1) {
  33. console::error(&format!("Reason: {}", e));
  34. }
  35. if exit {
  36. ::std::process::exit(1);
  37. }
  38. }
  39. fn main() {
  40. let matches = clap_app!(Gutenberg =>
  41. (version: crate_version!())
  42. (author: "Vincent Prouillet")
  43. (about: "Static site generator")
  44. (@setting SubcommandRequiredElseHelp)
  45. (@arg config: -c --config +takes_value "Path to a config file other than config.toml")
  46. (@subcommand init =>
  47. (about: "Create a new Gutenberg project")
  48. (@arg name: +required "Name of the project. Will create a directory with that name in the current directory")
  49. )
  50. (@subcommand build =>
  51. (about: "Builds the site")
  52. )
  53. (@subcommand serve =>
  54. (about: "Serve the site. Rebuild and reload on change automatically")
  55. (@arg interface: "Interface to bind on (default to 127.0.0.1)")
  56. (@arg port: "Which port to use (default to 1111)")
  57. )
  58. ).get_matches();
  59. let config_file = matches.value_of("config").unwrap_or("config.toml");
  60. match matches.subcommand() {
  61. ("init", Some(matches)) => {
  62. match cmd::create_new_project(matches.value_of("name").unwrap()) {
  63. Ok(()) => console::success("Project created"),
  64. Err(e) => unravel_errors("Failed to create the project", e, true),
  65. };
  66. },
  67. ("build", Some(_)) => {
  68. console::info("Building site...");
  69. let start = Instant::now();
  70. match cmd::build(&config_file) {
  71. Ok(()) => report_elapsed_time(start),
  72. Err(e) => unravel_errors("Failed to build the site", e, true),
  73. };
  74. },
  75. ("serve", Some(matches)) => {
  76. let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
  77. let port = matches.value_of("port").unwrap_or("1111");
  78. console::info("Building site...");
  79. match cmd::serve(interface, port, &config_file) {
  80. Ok(()) => (),
  81. Err(e) => unravel_errors("Failed to build the site", e, true),
  82. };
  83. },
  84. _ => unreachable!(),
  85. }
  86. }