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.

91 lines
2.7KB

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