use std::{ env, fs::{self, File}, io::{self, Write}, path::PathBuf, process::Command, }; use convert_case::{Case, Casing}; use crate::data::Data; use super::{TITLE_FILE, CHAPTER, MAIN_FILE}; const NEEDED_DIRECTORYS: &'static [&'static str] = &["build", "content", "headers", "references", "resources"]; pub fn generate_new_project( name: String, first_chapter: String, //first_section: String, preamble_path: PathBuf, resource_path: PathBuf, ) -> io::Result<()> { fs::create_dir(&name).unwrap(); env::set_current_dir(&name).unwrap(); let project_root = env::current_dir().unwrap(); for directory in NEEDED_DIRECTORYS { fs::create_dir(directory).unwrap(); } // content env::set_current_dir(project_root.join("content")).unwrap(); fs::create_dir(&first_chapter.to_case(Case::Snake)).unwrap(); fs::create_dir("static").unwrap(); env::set_current_dir("static").unwrap(); let mut title_file = File::create("title.tex").unwrap(); title_file.write_all(TITLE_FILE.as_bytes()).unwrap(); env::set_current_dir(project_root.join("content").join(&first_chapter.to_case(Case::Snake))).unwrap(); fs::create_dir("sections").unwrap(); let mut chapter_file = File::create("chapter_01.tex").unwrap(); chapter_file .write_all( CHAPTER .replace("REPLACEMENT_CHAPTER", &first_chapter) .as_bytes(), ) .unwrap(); //env::set_current_dir("sections").unwrap(); //let mut section_file = File::create(format!("{}.tex", &first_section)).unwrap(); //section_file // .write_all(SECTION.as_bytes()) // .unwrap(); // headers env::set_current_dir(project_root.join("headers")).unwrap(); File::create("preamble_local.tex").unwrap(); fs::copy(fs::canonicalize(preamble_path).unwrap(), "preamble.tex").unwrap(); // resources env::set_current_dir(project_root.join("resources")).unwrap(); fs::canonicalize(resource_path) .unwrap() .read_dir() .unwrap() .map(|path| path.expect("The provided path should work")) .for_each(|path| { fs::copy(path.path(), path.file_name()).unwrap(); }); // root files env::set_current_dir(project_root).unwrap(); let mut main_file = File::create("main.tex").unwrap(); main_file .write_all( MAIN_FILE .replace("REPLACEMENT_CHAPTER", &first_chapter.to_case(Case::Snake)) .as_bytes(), ) .unwrap(); let data_file = Data { last_chapter: crate::data::LastChapter { user_name: first_chapter.to_case(Case::Snake), number: 1, }, }; let mut lpm_file = File::create("lpm.toml").unwrap(); lpm_file .write_all(toml::to_string(&data_file).unwrap().as_bytes()) .unwrap(); Command::new("git") .arg("init") .output() .expect("failed to execute process"); let mut lpm_file = File::create(".gitignore").unwrap(); lpm_file .write_all(b"/build") .unwrap(); Ok(()) }