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
|
//! Utility functions for the UI
use std::collections::BTreeMap;
use std::io::Write;
use std::path::Path;
use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use handlebars::Handlebars;
use itertools::Itertools;
use crate::package::Package;
pub fn package_repo_cleanness_check(repo_path: &Path) -> Result<()> {
if !crate::util::git::repo_is_clean(&repo_path)? {
error!("Repository not clean, refusing to go on: {}", repo_path.display());
Err(anyhow!("Repository not clean, refusing to go on: {}", repo_path.display()))
} else {
Ok(())
}
}
pub fn print_packages<'a, I>(out: &mut dyn Write,
format: &str,
iter: I,
print_runtime_deps: bool,
print_build_deps: bool,
print_sys_deps: bool,
print_sys_runtime_deps: bool)
-> Result<()>
where I: Iterator<Item = &'a Package>
{
let mut hb = Handlebars::new();
hb.register_template_string("package", format)?;
for (i, package) in iter.enumerate() {
print_package(out,
&hb,
i,
package,
print_runtime_deps,
print_build_deps,
print_sys_deps,
print_sys_runtime_deps
)?;
}
Ok(())
}
fn print_package(out: &mut dyn Write,
hb: &Handlebars,
i: usize,
package: &Package,
print_runtime_deps: bool,
print_build_deps: bool,
print_sys_deps: bool,
print_sys_runtime_deps: bool
)
-> Result<()>
{
let mut data = BTreeMap::new();
data.insert("i", serde_json::Value::Number(serde_json::Number::from(i)));
data.insert("p", serde_json::to_value(package)?);
// This is an ugly hack. Because the `data` is a <String, String>, we do only insert the flag
// if it is set, because handlebars renders a non-present value as false
if print_runtime_deps {
data.insert("print_runtime_deps", serde_json::Value::Bool(print_runtime_deps));
}
if print_build_deps {
data.insert("print_build_deps", serde_json::Value::Bool(print_build_deps));
}
if print_sys_deps {
data.insert("print_system_deps", serde_json::Value::Bool(print_sys_deps));
}
if print_sys_runtime_deps {
data.insert("print_system_runtime_deps", serde_json::Value::Bool(print_sys_runtime_deps));
}
hb.render("package", &data)
.map_err(Error::from)
.and_then(|r| writeln!(out, "{}", r).map_err(Error::from))
}
|