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
use std::process::Command;

/// Substrate registry
pub struct Registry(pub String);

impl Registry {
    /// New registry
    pub fn new() -> Registry {
        println!("Check substrate status...");
        println!(">{}", "-".repeat(42));
        let mut substrate = dirs::home_dir().expect("Could not find home directory");
        substrate.push(".substrate");

        let registry = substrate.to_string_lossy().to_owned();
        let mut git = Command::new("git");
        if substrate.exists() {
            git.args(vec!["-C", &registry, "pull", "origin", "master"])
                .status()
                .expect(&format!("Exec git pull failed in {}", &registry));
        } else {
            git.args(vec![
                "clone",
                "https://github.com/paritytech/substrate.git",
                &registry,
            ])
            .status()
            .expect(&format!("Exec git clone failed in {}", &registry));
        }

        println!("{}<", "-".repeat(42));
        Registry(registry.to_string())
    }

    /// list substrate tags
    pub fn list_tags(&self) -> Vec<String> {
        String::from_utf8_lossy(
            &Command::new("git")
                .args(vec!["-C", &self.0, "tag", "--list"])
                .output()
                .unwrap()
                .stdout,
        )
        .to_string()
        .split("\n")
        .collect::<Vec<&str>>()
        .iter()
        .filter(|t| t.starts_with('v'))
        .map(|t| t.to_string())
        .collect()
    }
}