summary refs log tree commit diff stats
path: root/src/new/project.rs
blob: 56edead646b1fb5055da0b1b74f66f4d4c80edf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::{
    env,
    fs::{self, File},
    io::{self, Write},
    path::PathBuf,
    process::Command,
};

use convert_case::{Case, Casing};

use crate::data::Data;

use super::{CHAPTER, MAIN_FILE, TITLE_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(())
}