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.

64 lines
2.0KB

  1. use std::fs::{create_dir, canonicalize};
  2. use std::path::Path;
  3. use errors::Result;
  4. use utils::fs::create_file;
  5. use prompt::{ask_bool, ask_url};
  6. use console;
  7. const CONFIG: &'static str = r#"
  8. # The URL the site will be built for
  9. base_url = "%BASE_URL%"
  10. # Whether to automatically compile all Sass files in the sass directory
  11. compile_sass = %COMPILE_SASS%
  12. # Whether to do syntax highlighting
  13. # Theme can be customised by setting the `highlight_theme` variable to a theme supported by Gutenberg
  14. highlight_code = %HIGHLIGHT%
  15. [extra]
  16. # Put all your custom variables here
  17. "#;
  18. pub fn create_new_project(name: &str) -> Result<()> {
  19. let path = Path::new(name);
  20. // Better error message than the rust default
  21. if path.exists() && path.is_dir() {
  22. bail!("Folder `{}` already exists", path.to_string_lossy().to_string());
  23. }
  24. create_dir(path)?;
  25. console::info("Welcome to Gutenberg!");
  26. let base_url = ask_url("> What is the URL of your site?", "https://example.com")?;
  27. let compile_sass = ask_bool("> Do you want to enable Sass compilation?", true)?;
  28. let highlight = ask_bool("> Do you want to enable syntax highlighting?", false)?;
  29. let config = CONFIG
  30. .trim_left()
  31. .replace("%BASE_URL%", &base_url)
  32. .replace("%COMPILE_SASS%", &format!("{}", compile_sass))
  33. .replace("%HIGHLIGHT%", &format!("{}", highlight));
  34. create_file(&path.join("config.toml"), &config)?;
  35. create_dir(path.join("content"))?;
  36. create_dir(path.join("templates"))?;
  37. create_dir(path.join("static"))?;
  38. if compile_sass {
  39. create_dir(path.join("sass"))?;
  40. }
  41. println!();
  42. console::success(&format!("Done! Your site was created in {:?}", canonicalize(path).unwrap()));
  43. println!();
  44. console::info("Get started by using the built-in server: `gutenberg serve`");
  45. println!("There is no built-in theme so you will see a white page.");
  46. println!("Visit https://github.com/Keats/gutenberg for the full documentation.");
  47. Ok(())
  48. }