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.

42 lines
957B

  1. use std::io::prelude::*;
  2. use std::fs::{create_dir, File};
  3. use std::path::Path;
  4. use errors::{Result, ErrorKind};
  5. const CONFIG: &'static str = r#"
  6. title = "My site"
  7. base_url = "https://replace-this-with-your-url.com"
  8. "#;
  9. pub fn create_new_project<P: AsRef<Path>>(name: P) -> Result<()> {
  10. let path = name.as_ref();
  11. // Better error message than the rust default
  12. if path.exists() && path.is_dir() {
  13. return Err(ErrorKind::FolderExists(path.to_string_lossy().to_string()).into());
  14. }
  15. // main folder
  16. create_dir(path)?;
  17. create_file(path.join("config.toml"), CONFIG.trim_left())?;
  18. // content folder
  19. create_dir(path.join("content"))?;
  20. // layouts folder
  21. create_dir(path.join("layouts"))?;
  22. create_dir(path.join("static"))?;
  23. Ok(())
  24. }
  25. fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
  26. let mut file = File::create(&path)?;
  27. file.write_all(content.as_bytes())?;
  28. Ok(())
  29. }