util/
new.rs

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
use error::{YabsError, YabsErrorKind};

use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

pub fn new_project(name: &str, lib: bool) -> Result<(), YabsError> {
    let dir = Path::new(name);
    if dir.exists() {
        bail!(YabsErrorKind::DirExists(dir.to_path_buf()));
    }
    fs::create_dir_all(dir)?;
    fs::create_dir_all(dir.join("src"))?;
    if lib {
        create_bin_files(&dir.to_path_buf(), name)?;
    }
    Ok(create_bin_files(&dir.to_path_buf(), name)?)
}

pub fn create_bin_files(path: &PathBuf, name: &str) -> Result<(), YabsError> {
    fs::File::create(path.join(format!("{}.toml", name)))
        .and_then(|mut file| {
            Ok(file.write_all(format!("[project]\n\
                name = \"{0}\"\n\
                version = \"0.0.1\"\n\
                target = [\"{0}\"]\n\
                file-extensions = [\"cpp\"]\n\
                compiler = \"g++\"\n\
                compiler-flags = []\n\
                src = [\n\
                \t\"src/main.cpp\"\n\
                ]\n\
                libraries = []\n\
                library-directories = []\n\
                include = []\n\
                linker-flags = []\n\
                ignore = []\n\
                before-script = []\n\
                after-script = []\n\n\
                [[bin]]\n\
                name = \"{0}\"\n\
                path = \"./src/main.cpp\"
                ",
                                      name)
                                  .as_bytes())?)
        })?;
    fs::File::create(path.join("src/main.cpp"))
        .and_then(|mut src| {
                      Ok(src.write_all(b"#include <iostream>\n\n\
            int main() {\n\
            \treturn 0;\n\
            }")?)
                  })?;
    Ok(())
}