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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::build::*;
use crate::date_time::now_date_time;
use crate::env::dep_source_replace::filter_cargo_tree;
use crate::err::SdResult;
use crate::Format;
use is_debug::build_channel;
use std::collections::BTreeMap;
use std::env;
use std::process::Command;
#[derive(Default, Debug)]
pub struct SystemEnv {
map: BTreeMap<ShadowConst, ConstVal>,
}
pub const BUILD_OS: ShadowConst = "BUILD_OS";
pub const RUST_VERSION: ShadowConst = "RUST_VERSION";
pub const RUST_CHANNEL: ShadowConst = "RUST_CHANNEL";
pub const CARGO_VERSION: ShadowConst = "CARGO_VERSION";
pub const CARGO_TREE: ShadowConst = "CARGO_TREE";
pub const BUILD_TARGET: ShadowConst = "BUILD_TARGET";
pub const BUILD_TARGET_ARCH: ShadowConst = "BUILD_TARGET_ARCH";
pub const CARGO_MANIFEST_DIR: ShadowConst = "CARGO_MANIFEST_DIR";
pub const PKG_VERSION: ShadowConst = "PKG_VERSION";
pub const PKG_DESCRIPTION: ShadowConst = "PKG_DESCRIPTION";
pub const PKG_VERSION_MAJOR: ShadowConst = "PKG_VERSION_MAJOR";
pub const PKG_VERSION_MINOR: ShadowConst = "PKG_VERSION_MINOR";
pub const PKG_VERSION_PATCH: ShadowConst = "PKG_VERSION_PATCH";
pub const PKG_VERSION_PRE: ShadowConst = "PKG_VERSION_PRE";
impl SystemEnv {
fn init(&mut self, std_env: &BTreeMap<String, String>) -> SdResult<()> {
let mut update_val = |c: ShadowConst, v: String| {
if let Some(mut val) = self.map.get_mut(c) {
val.t = ConstType::Str;
val.v = v;
}
};
if let Some(v) = std_env.get("RUSTUP_TOOLCHAIN") {
update_val(RUST_CHANNEL, v.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(),
);
}
if let Ok(out) = Command::new("cargo").arg("tree").output() {
let input = String::from_utf8(out.stdout)?;
if let Some(index) = input.find('\n') {
let lines =
filter_cargo_tree(input.get(index..).unwrap_or_default().split('\n').collect());
update_val(CARGO_TREE, lines);
}
}
if let Ok(_out) = Command::new("cargo")
.args(["metadata", "--format-version", "1"])
.output()
{
}
if let Some(v) = std_env.get("TARGET") {
update_val(BUILD_TARGET, v.to_string());
}
if let Some(v) = std_env.get("CARGO_CFG_TARGET_ARCH") {
update_val(BUILD_TARGET_ARCH, v.to_string());
}
if let Some(v) = std_env.get("CARGO_PKG_VERSION") {
update_val(PKG_VERSION, v.to_string());
}
if let Some(v) = std_env.get("CARGO_PKG_DESCRIPTION") {
update_val(PKG_DESCRIPTION, v.to_string());
}
if let Some(v) = std_env.get("CARGO_PKG_VERSION_MAJOR") {
update_val(PKG_VERSION_MAJOR, v.to_string());
}
if let Some(v) = std_env.get("CARGO_PKG_VERSION_MINOR") {
update_val(PKG_VERSION_MINOR, v.to_string());
}
if let Some(v) = std_env.get("CARGO_PKG_VERSION_PATCH") {
update_val(PKG_VERSION_PATCH, v.to_string());
}
if let Some(v) = std_env.get("CARGO_PKG_VERSION_PRE") {
update_val(PKG_VERSION_PRE, v.to_string());
}
if let Some(v) = std_env.get("CARGO_MANIFEST_DIR") {
update_val(CARGO_MANIFEST_DIR, v.to_string());
}
Ok(())
}
}
mod dep_source_replace {
use std::fs;
fn path_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}
const DEP_REPLACE_NONE: &str = "";
const DEP_REPLACE_PATH: &str = " (* path)";
const DEP_REPLACE_GIT: &str = " (* git)";
const DEP_REPLACE_REGISTRY: &str = " (* registry)";
pub fn filter_dep_source(input: &str) -> String {
let (val, index) = if let Some(index) = input.find(" (/") {
(DEP_REPLACE_PATH, index)
} else if let Some(index) = input.find(" (registry ") {
(DEP_REPLACE_REGISTRY, index)
} else if let Some(index) = input.find(" (http") {
(DEP_REPLACE_GIT, index)
} else if let Some(index) = input.find(" (https") {
(DEP_REPLACE_GIT, index)
} else if let Some(index) = input.find(" (ssh") {
(DEP_REPLACE_GIT, index)
} else if let (Some(start), Some(end)) = (input.find(" ("), input.find(')')) {
let path = input.get(start + 2..end).unwrap_or_default().trim();
if path_exists(path) {
(DEP_REPLACE_PATH, start)
} else {
(DEP_REPLACE_NONE, input.len())
}
} else {
(DEP_REPLACE_NONE, input.len())
};
format!("{}{}", &input.get(..index).unwrap_or_default(), val)
}
pub fn filter_cargo_tree(lines: Vec<&str>) -> String {
let mut tree = "\n".to_string();
for line in lines {
let val = filter_dep_source(line);
if tree.trim().is_empty() {
tree.push_str(&val);
} else {
tree = format!("{tree}\n{val}");
}
}
tree
}
}
pub fn new_system_env(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowConst, ConstVal> {
let mut env = SystemEnv::default();
env.map.insert(
BUILD_OS,
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_TREE,
ConstVal::new("display build cargo dependencies.It's used by rust version 1.44.0"),
);
env.map.insert(
BUILD_TARGET,
ConstVal::new("display build current project target"),
);
env.map.insert(
BUILD_TARGET_ARCH,
ConstVal::new("display build current project version arch"),
);
env.map.insert(
PKG_VERSION,
ConstVal::new("display build current project version"),
);
env.map.insert(
PKG_DESCRIPTION,
ConstVal::new("display build current project description"),
);
env.map.insert(
PKG_VERSION_MAJOR,
ConstVal::new("display build current project major version"),
);
env.map.insert(
PKG_VERSION_MINOR,
ConstVal::new("display build current project minor version"),
);
env.map.insert(
PKG_VERSION_PATCH,
ConstVal::new("display build current project patch version"),
);
env.map.insert(
PKG_VERSION_PRE,
ConstVal::new("display build current project preview version"),
);
env.map.insert(
CARGO_MANIFEST_DIR,
ConstVal::new("display build current build cargo manifest dir"),
);
if let Err(e) = env.init(std_env) {
println!("{e}");
}
env.map
}
#[derive(Default, Debug)]
pub struct Project {
map: BTreeMap<ShadowConst, ConstVal>,
}
const PROJECT_NAME: ShadowConst = "PROJECT_NAME";
const BUILD_TIME: ShadowConst = "BUILD_TIME";
const BUILD_TIME_2822: ShadowConst = "BUILD_TIME_2822";
const BUILD_TIME_3339: ShadowConst = "BUILD_TIME_3339";
const BUILD_RUST_CHANNEL: ShadowConst = "BUILD_RUST_CHANNEL";
pub fn build_time(project: &mut Project) {
let time = now_date_time();
project.map.insert(
BUILD_TIME,
ConstVal {
desc: "display project build time".to_string(),
v: time.human_format(),
t: ConstType::Str,
},
);
project.map.insert(
BUILD_TIME_2822,
ConstVal {
desc: "display project build time by rfc2822".to_string(),
v: time.to_rfc2822(),
t: ConstType::Str,
},
);
project.map.insert(
BUILD_TIME_3339,
ConstVal {
desc: "display project build time by rfc3399".to_string(),
v: time.to_rfc3339(),
t: ConstType::Str,
},
);
}
pub fn new_project(std_env: &BTreeMap<String, String>) -> BTreeMap<ShadowConst, ConstVal> {
let mut project = Project::default();
build_time(&mut project);
project.map.insert(
BUILD_RUST_CHANNEL,
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(mut val)) = (
std_env.get("CARGO_PKG_NAME"),
project.map.get_mut(PROJECT_NAME),
) {
val.t = ConstType::Str;
val.v = v.to_string();
}
project.map
}
#[cfg(test)]
mod tests {
use crate::env::dep_source_replace::filter_dep_source;
#[test]
fn test_filter_dep_source_none() {
let input = "shadow-rs v0.5.23";
let ret = filter_dep_source(input);
assert_eq!(input, ret)
}
#[test]
fn test_filter_dep_source_multi() {
let input = "shadow-rs v0.5.23 (*)";
let ret = filter_dep_source(input);
assert_eq!(input, ret)
}
#[test]
fn test_filter_dep_source_path() {
let input = "shadow-rs v0.5.23 (/Users/baoyachi/shadow-rs)";
let ret = filter_dep_source(input);
assert_eq!("shadow-rs v0.5.23 (* path)", ret)
}
#[test]
fn test_filter_dep_source_registry() {
let input = "shadow-rs v0.5.23 (registry `ssh://git@github.com/baoyachi/shadow-rs.git`)";
let ret = filter_dep_source(input);
assert_eq!("shadow-rs v0.5.23 (* registry)", ret)
}
#[test]
fn test_filter_dep_source_git_https() {
let input = "shadow-rs v0.5.23 (https://github.com/baoyachi/shadow-rs#13572c90)";
let ret = filter_dep_source(input);
assert_eq!("shadow-rs v0.5.23 (* git)", ret)
}
#[test]
fn test_filter_dep_source_git_http() {
let input = "shadow-rs v0.5.23 (http://github.com/baoyachi/shadow-rs#13572c90)";
let ret = filter_dep_source(input);
assert_eq!("shadow-rs v0.5.23 (* git)", ret)
}
#[test]
fn test_filter_dep_source_git() {
let input = "shadow-rs v0.5.23 (ssh://git@github.com/baoyachi/shadow-rs)";
let ret = filter_dep_source(input);
assert_eq!("shadow-rs v0.5.23 (* git)", ret)
}
#[test]
fn test_filter_dep_windows_path() {
let input = r#"shadow-rs v0.5.23 (FD:\a\shadow-rs\shadow-rs)"#;
let ret = filter_dep_source(input);
assert_eq!(input, ret)
}
}