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
use crate::python::PYTHON_CONFIG;
use origen::core::term::*;
use origen::STATUS;
use std::env;
use std::io::stdout;
use std::io::Write;
use std::path::Path;
use std::process::Command;

pub fn run() {
    let orig_dir = env::current_dir().expect("Couldn't read the PWD");

    if STATUS.is_origen_present {
        starting("rust/origen ... ");
        cd(&STATUS.origen_wksp_root.join("rust").join("origen"));

        cargo_fmt();

        starting("rust/pyapi ... ");
        cd(&STATUS.origen_wksp_root.join("rust").join("pyapi"));

        cargo_fmt();
    }

    let root = match STATUS.is_origen_present {
        true => {
            starting("python_app ... ");
            STATUS.origen_wksp_root.join("test_apps").join("python_app")
        }
        false => {
            starting("formatting ... ");
            origen::app().unwrap().root.clone()
        }
    };

    cd(&root);

    py_fmt(&root);

    if STATUS.is_origen_present {
        starting("python ... ");
        let dir = &STATUS.origen_wksp_root.join("python");
        py_fmt(&dir);
    }

    let _ = env::set_current_dir(&orig_dir);
}

fn starting(job: &str) {
    print!("{}", job);
    let _ = stdout().flush();
}

// Returns true if no problems
fn py_fmt(dir: &Path) {
    let res = PYTHON_CONFIG
        .poetry_command()
        .arg("run")
        .arg("yapf")
        .arg("--in-place")
        .arg("--recursive")
        .arg(&format!("{}", dir.display()))
        .status();

    if let Ok(stat) = res {
        if stat.success() {
            greenln("YES");
            return;
        }
    }
    redln("NO");
}

// Returns true if no problems
fn cargo_fmt() {
    let res = Command::new("cargo").arg("fmt").status();
    if let Ok(stat) = res {
        if stat.success() {
            greenln("YES");
            return;
        }
    }
    redln("NO");
}

pub fn cd(dir: &Path) {
    env::set_current_dir(&dir).expect(&format!("Couldn't cd to '{}'", dir.display()));
}