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
132
133
134
135
use std::io::Write;
use std::process::{Command, Stdio};
use std::time::{SystemTime, UNIX_EPOCH};
pub fn run(template: &str){
let args :Vec<String> = std::env::args().collect();
if !is_runtime(&args){
let mut content_template = "
=============== z1info extended data ====================
| Extended data added to binary file through z1info.
|--------------- z1info parameter ----------------------
| {z1_info}
|--------------- git info ------------------------------
| commit id: {git_info}
|--------------- build time ----------------------------
| {build_time}
=========================================================
";
if "z1template"!=template {
content_template = template;
}
let mut content = str::replace(content_template,"{z1_info}",&args[args.len()-1]);
content = str::replace(&content[..],"{git_info}",&get_commit_id());
content = str::replace(&content[..],"{build_time}",&format!("{}",get_current_timestamp()));
let mut file = std::fs::File::create("z1info_tmp").unwrap();
file.write_all(content.as_bytes()).expect("write z1info failed");
} else {
println!("{}",include_str!("../z1info_tmp"));
}
}
fn is_runtime(args: &Vec<String>)->bool{
let lenght = args.len();
if 0 < lenght {
let flag = "z1info=";
let flag_length = flag.len();
let last_length = args[lenght-1].len();
if last_length >= flag_length {
return !(flag == &args[lenght-1][0..flag_length]);
}
}
return true;
}
fn get_commit_id()->String{
let cmd_str = "git rev-parse HEAD";
let out_str: String;
if cfg!(target_os = "windows") {
let output = Command::new("cmd")
.arg("/c")
.arg(&cmd_str)
.stdout(Stdio::piped())
.output()
.expect(&format!("cmd exec error!"));
out_str = format!("{}", String::from_utf8_lossy(&output.stdout));
} else {
let output= Command::new("sh")
.arg("/c")
.arg(&cmd_str)
.stdout(Stdio::piped())
.output()
.expect(&format!("sh exec error!"));
out_str = format!("{}", String::from_utf8_lossy(&output.stdout));
};
let ret_end = str::replace(&out_str[..],"\n","");
return ret_end;
}
fn get_current_timestamp()->u64{
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
return since_the_epoch.as_secs();
}