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.

98 lines
2.9KB

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