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.

69 lines
2.2KB

  1. use std::fs::{canonicalize, create_dir};
  2. use std::path::Path;
  3. use errors::Result;
  4. use utils::fs::create_file;
  5. use console;
  6. use prompt::{ask_bool, ask_url};
  7. const CONFIG: &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 Zola
  14. highlight_code = %HIGHLIGHT%
  15. # Whether to build a search index to be used later on by a JavaScript library
  16. build_search_index = %SEARCH%
  17. [extra]
  18. # Put all your custom variables here
  19. "#;
  20. pub fn create_new_project(name: &str) -> Result<()> {
  21. let path = Path::new(name);
  22. // Better error message than the rust default
  23. if path.exists() && path.is_dir() {
  24. bail!("Folder `{}` already exists", path.to_string_lossy().to_string());
  25. }
  26. create_dir(path)?;
  27. console::info("Welcome to Zola!");
  28. let base_url = ask_url("> What is the URL of your site?", "https://example.com")?;
  29. let compile_sass = ask_bool("> Do you want to enable Sass compilation?", true)?;
  30. let highlight = ask_bool("> Do you want to enable syntax highlighting?", false)?;
  31. let search = ask_bool("> Do you want to build a search index of the content?", false)?;
  32. let config = CONFIG
  33. .trim_left()
  34. .replace("%BASE_URL%", &base_url)
  35. .replace("%COMPILE_SASS%", &format!("{}", compile_sass))
  36. .replace("%SEARCH%", &format!("{}", search))
  37. .replace("%HIGHLIGHT%", &format!("{}", highlight));
  38. create_file(&path.join("config.toml"), &config)?;
  39. create_dir(path.join("content"))?;
  40. create_dir(path.join("templates"))?;
  41. create_dir(path.join("static"))?;
  42. create_dir(path.join("themes"))?;
  43. if compile_sass {
  44. create_dir(path.join("sass"))?;
  45. }
  46. println!();
  47. console::success(&format!("Done! Your site was created in {:?}", canonicalize(path).unwrap()));
  48. println!();
  49. console::info(
  50. "Get started by moving into the directory and using the built-in server: `zola serve`",
  51. );
  52. println!("Visit https://www.getzola.org for the full documentation.");
  53. Ok(())
  54. }