summaryrefslogtreecommitdiffstats
path: root/src/ui.rs
blob: 59fd90c0fa8b3ec6b0dfa66dd98b08eddcad8964 (plain)
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
//! 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 log::error;
use handlebars::Handlebars;

use crate::package::Package;
use crate::package::ScriptBuilder;
use crate::package::Shebang;
use crate::config::Configuration;

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,
                             config: &Configuration,
                             print_runtime_deps: bool,
                             print_build_deps: bool,
                             print_sources: bool,
                             print_dependencies: bool,
                             print_patches: bool,
                             print_env: bool,
                             print_flags: bool,
                             print_deny_images: bool,
                             print_phases: bool,
                             print_script: 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,
                      config,
                      print_runtime_deps,
                      print_build_deps,
                      print_sources,
                      print_dependencies,
                      print_patches,
                      print_env,
                      print_flags,
                      print_deny_images,
                      print_phases,
                      print_script
                      )?;
    }

    Ok(())
}

fn print_package(out: &mut dyn Write,
                 hb: &Handlebars,
                 i: usize,
                 package: &Package,
                 config: &Configuration,
                 print_runtime_deps: bool,
                 print_build_deps: bool,
                 print_sources: bool,
                 print_dependencies: bool,
                 print_patches: bool,
                 print_env: bool,
                 print_flags: bool,
                 print_deny_images: bool,
                 print_phases: bool,
                 print_script: bool)
    -> Result<()>
{
    let script = ScriptBuilder::new(&Shebang::from(config.shebang().clone()))
        .build(package, config.available_phases(), *config.strict_script_interpolation())?;

    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)?);
    data.insert("script"             , serde_json::to_value(script)?);
    data.insert("print_runtime_deps" , serde_json::Value::Bool(print_runtime_deps));
    data.insert("print_build_deps"   , serde_json::Value::Bool(print_build_deps));
    data.insert("print_sources"      , serde_json::Value::Bool(print_sources));
    data.insert("print_dependencies" , serde_json::Value::Bool(print_dependencies));
    data.insert("print_patches"      , serde_json::Value::Bool(print_patches));
    data.insert("print_env"          , serde_json::Value::Bool(print_env));
    data.insert("print_flags"        , serde_json::Value::Bool(print_flags));
    data.insert("print_deny_images"  , serde_json::Value::Bool(print_deny_images));
    data.insert("print_phases"       , serde_json::Value::Bool(print_phases));
    data.insert("print_script"       , serde_json::Value::Bool(print_script));


    hb.render("package", &data)
        .map_err(Error::from)
        .and_then(|r| writeln!(out, "{}", r).map_err(Error::from))
}