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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::build::*;
use crate::channel::*;
use crate::err::SdResult;

use chrono::Local;
use std::env;
use std::process::Command;

use std::cell::RefCell;
use std::collections::HashMap;

#[derive(Default, Debug)]
pub struct SystemEnv {
    map: HashMap<ShadowConst, RefCell<ConstVal>>,
}

const BUILD_OS: ShadowConst = "BUILD_OS";
const RUST_VERSION: ShadowConst = "RUST_VERSION";
const RUST_CHANNEL: ShadowConst = "RUST_CHANNEL";
const CARGO_VERSION: ShadowConst = "CARGO_VERSION";
const CARGO_LOCK: ShadowConst = "CARGO_LOCK";
// const CARGO_TREE: &str = "CARGO_TREE";

impl SystemEnv {
    pub fn new() -> HashMap<ShadowConst, RefCell<ConstVal>> {
        let mut env = SystemEnv::default();
        env.map.insert(
            BUILD_OS,
            RefCell::new(ConstVal {
                desc: "display build system os".to_string(),
                v: format!("{}-{}", env::consts::OS, env::consts::ARCH),
                t: ConstType::Str,
            }),
        );

        env.map.insert(
            RUST_CHANNEL,
            ConstVal::new("display build system rust channel"),
        );
        env.map.insert(
            RUST_VERSION,
            ConstVal::new("display build system rust version"),
        );
        env.map.insert(
            CARGO_VERSION,
            ConstVal::new("display build system cargo version"),
        );
        env.map.insert(
            CARGO_LOCK,
            ConstVal::new("display build project dependence cargo lock detail"),
        );

        if let Err(e) = env.init() {
            println!("{}", e.to_string());
        }
        env.map
    }

    fn init(&mut self) -> SdResult<()> {
        let update_val = |c: ShadowConst, v: String| {
            if let Some(c) = self.map.get(c) {
                let mut val = c.borrow_mut();
                val.t = ConstType::Str;
                val.v = v.to_string();
            }
        };
        if let Ok(out) = Command::new("rustup").arg("default").output() {
            update_val(
                RUST_CHANNEL,
                String::from_utf8(out.stdout)?.trim().to_string(),
            );
        }

        if let Ok(out) = Command::new("rustc").arg("-V").output() {
            update_val(
                RUST_VERSION,
                String::from_utf8(out.stdout)?.trim().to_string(),
            );
        }

        if let Ok(out) = Command::new("cargo").arg("-V").output() {
            update_val(
                CARGO_VERSION,
                String::from_utf8(out.stdout)?.trim().to_string(),
            );
        }
        Ok(())
    }
}

#[derive(Default, Debug)]
pub struct Project {
    map: HashMap<ShadowConst, RefCell<ConstVal>>,
}

const PROJECT_NAME: ShadowConst = "PROJECT_NAME";
const BUILD_TIME: ShadowConst = "BUILD_TIME";
const BUILD_RUST_CHANNEL: ShadowConst = "BUILD_RUST_CHANNEL";

impl Project {
    pub fn new() -> HashMap<ShadowConst, RefCell<ConstVal>> {
        let mut project = Project::default();
        project.map.insert(
            BUILD_TIME,
            RefCell::new(ConstVal {
                desc: "display project build time".to_string(),
                v: Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                t: ConstType::Str,
            }),
        );
        project.map.insert(
            BUILD_RUST_CHANNEL,
            RefCell::new(ConstVal {
                desc: "display project build by rust channel [debug or release]".to_string(),
                v: build_channel().to_string(),
                t: ConstType::Str,
            }),
        );
        project
            .map
            .insert(PROJECT_NAME, ConstVal::new("display project name"));

        if let (Some(v), Some(c)) = (option_env!("CARGO_PKG_NAME"), project.map.get(PROJECT_NAME)) {
            let mut val = c.borrow_mut();
            val.t = ConstType::Str;
            val.v = v.to_string();
        }

        project.map
    }
}