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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::{
    fs::{read_to_string, File},
    path::{Path, PathBuf},
    process::Stdio,
};

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
    pub collections: Vec<String>,
}

pub struct Collection {
    pub path: PathBuf,
    pub name: String,
}

pub fn get_collections() -> Vec<Collection> {
    let cwd: PathBuf = std::env::current_dir().unwrap();
    let cwd_string = cwd.to_str().unwrap();
    let znap_file_path = format!("{}/Znap.toml", cwd_string);
    let znap_file = read_to_string(znap_file_path).expect("Should have been able to read the file");
    let config: Config = toml::from_str(&znap_file).unwrap();
    let collections_dir_path = cwd.join("collections");
    let collections: Vec<Collection> = config
        .collections
        .iter()
        .map(|collection| Collection {
            path: collections_dir_path.join(collection),
            name: collection.clone(),
        })
        .collect();

    collections
}

pub fn write_file(path: &Path, content: &String) {
    let mut file = File::create(&path).unwrap();
    file.write_all(content.as_bytes()).unwrap();
}

pub fn build_collection(collection_name: String) -> Result<()> {
    let exit = std::process::Command::new("cargo")
        .arg("build")
        .arg("-p")
        .arg(collection_name)
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow::format_err!("{}", e.to_string()))?;

    if !exit.status.success() {
        std::process::exit(exit.status.code().unwrap_or(1));
    }

    Ok(())
}