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.

104 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 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. (@arg config: -c --config +takes_value "Path to a config file other than config.toml")
  32. (@subcommand init =>
  33. (about: "Create a new Gutenberg project")
  34. (@arg name: +required "Name of the project. Will create a directory with that name in the current directory")
  35. )
  36. (@subcommand build =>
  37. (about: "Builds the site")
  38. )
  39. (@subcommand serve =>
  40. (about: "Serve the site. Rebuild and reload on change automatically")
  41. (@arg interface: "Interface to bind on (default to 127.0.0.1)")
  42. (@arg port: "Which port to use (default to 1111)")
  43. )
  44. ).get_matches();
  45. let config_file = matches.value_of("config").unwrap_or("config.toml");
  46. match matches.subcommand() {
  47. ("init", Some(matches)) => {
  48. match cmd::create_new_project(matches.value_of("name").unwrap()) {
  49. Ok(()) => {
  50. println!("Project created");
  51. },
  52. Err(e) => {
  53. println!("Error: {}", e);
  54. ::std::process::exit(1);
  55. },
  56. };
  57. },
  58. ("build", Some(_)) => {
  59. println!("Building site");
  60. let start = Instant::now();
  61. match cmd::build(&config_file) {
  62. Ok(()) => {
  63. report_elapsed_time(start);
  64. },
  65. Err(e) => {
  66. println!("Failed to build the site");
  67. println!("Error: {}", e);
  68. for e in e.iter().skip(1) {
  69. println!("Reason: {}", e)
  70. }
  71. ::std::process::exit(1);
  72. },
  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. match cmd::serve(interface, port, &config_file) {
  79. Ok(()) => (),
  80. Err(e) => {
  81. println!("Error: {}", e);
  82. for e in e.iter().skip(1) {
  83. println!("Reason: {}", e)
  84. }
  85. ::std::process::exit(1);
  86. },
  87. };
  88. },
  89. _ => unreachable!(),
  90. }
  91. }