aboutsummaryrefslogtreecommitdiffstats
path: root/src/new/project.rs
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2023-06-18 16:32:47 +0200
committerSoispha <soispha@vhack.eu>2023-06-18 16:32:47 +0200
commit369bf07cbb36d8e72025014bfd4ddc52d9049fde (patch)
tree545b32762ec8af521484f6c520dc568edaf8a2f7 /src/new/project.rs
downloadlpm-369bf07cbb36d8e72025014bfd4ddc52d9049fde.zip
Chore: Initial commit
Diffstat (limited to '')
-rw-r--r--src/new/project.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/new/project.rs b/src/new/project.rs
new file mode 100644
index 0000000..e04d138
--- /dev/null
+++ b/src/new/project.rs
@@ -0,0 +1,110 @@
+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(())
+}