summaryrefslogtreecommitdiffstats
path: root/src/ui/package.rs
blob: ba68f864d75a81da82d1f519fdc62ee0359f6b03 (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
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
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use std::borrow::Borrow;
use std::collections::BTreeMap;

use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use handlebars::Handlebars;

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

pub struct PackagePrintFlags {
    pub print_all: bool,
    pub print_runtime_deps: bool,
    pub print_build_deps: bool,
    pub print_sources: bool,
    pub print_dependencies: bool,
    pub print_patches: bool,
    pub print_env: bool,
    pub print_flags: bool,
    pub print_allowed_images: bool,
    pub print_denied_images: bool,
    pub print_phases: bool,
    pub print_script: bool,
    pub script_line_numbers: bool,
    pub script_highlighting: bool,
}

impl PackagePrintFlags {
    // Helper to check whether any of the CLI args requested one of these flags.
    //
    // The print_build_deps and print_runtime_deps as well as the script_highlighting and
    // script_line_numbers is not included, because these only modify what to print and not whether
    // to print.
    fn print_any(&self) -> bool {
        self.print_all || {
            self.print_sources
                || self.print_dependencies
                || self.print_patches
                || self.print_env
                || self.print_flags
                || self.print_allowed_images
                || self.print_denied_images
                || self.print_phases
                || self.print_script
        }
    }
}


pub trait PreparePrintable<'a>
    where Self: Borrow<Package> + Sized
{
    fn prepare_print(self, config: &'a Configuration, flags: &'a PackagePrintFlags, handlebars: &'a Handlebars<'a>, i: usize) -> PreparePrintPackage<'a, Self>;
}

impl<'a, P> PreparePrintable<'a> for P
    where P: Borrow<Package>
{
    fn prepare_print(self, config: &'a Configuration, flags: &'a PackagePrintFlags, handlebars: &'a Handlebars<'a>, i: usize) -> PreparePrintPackage<'a, P> {
        PreparePrintPackage {
            package: self,
            config,
            flags,
            handlebars,
            i,
        }
    }
}

pub struct PreparePrintPackage<'a, P: Borrow<Package>> {
    package: P,
    config: &'a Configuration,
    flags: &'a PackagePrintFlags,
    handlebars: &'a Handlebars<'a>,
    i: usize,
}


pub fn handlebars_for_package_printing(format: &str) -> Result<Handlebars> {
    let mut hb = Handlebars::new();
    hb.register_escape_fn(handlebars::no_escape);
    hb.register_template_string("package", format)?;
    Ok(hb)
}

impl<'a, P: Borrow<Package>> PreparePrintPackage<'a, P> {
    pub fn into_displayable(self) -> Result<PrintablePackage> {
        let script = ScriptBuilder::new(&Shebang::from(self.config.shebang().clone())).build(
            self.package.borrow(),
            self.config.available_phases(),
            *self.config.strict_script_interpolation(),
        ).context("Rendering script for printing it failed")?;

        let script = crate::ui::script_to_printable(
            &script,
            self.flags.script_highlighting,
            self.config
                .script_highlight_theme()
                .as_ref()
                .ok_or_else(|| anyhow!("Highlighting for script enabled, but no theme configured"))?,
            self.flags.script_line_numbers,
        )?;

        let mut data = BTreeMap::new();
        data.insert("i", serde_json::Value::Number(serde_json::Number::from(self.i)));
        data.insert("p", serde_json::to_value(self.package.borrow())?);
        data.insert("script", serde_json::Value::String(script));
        data.insert("print_any", serde_json::Value::Bool(self.flags.print_any()));
        data.insert(
            "print_runtime_deps",
            serde_json::Value::Bool(self.flags.print_runtime_deps),
        );
        data.insert(
            "print_build_deps",
            serde_json::Value::Bool(self.flags.print_build_deps),
        );

        data.insert(
            "print_sources",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_sources),
        );
        data.insert(
            "print_dependencies",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_dependencies),
        );
        data.insert(
            "print_patches",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_patches),
        );
        data.insert(
            "print_env",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_env),
        );
        data.insert(
            "print_flags",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_flags),
        );
        data.insert(
            "print_allowed_images",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_allowed_images),
        );
        data.insert(
            "print_denied_images",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_denied_images),
        );
        data.insert(
            "print_phases",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_phases),
        );
        data.insert(
            "print_script",
            serde_json::Value::Bool(self.flags.print_all || self.flags.print_script),
        );

        let string = self.handlebars.render("package", &data)?;
        Ok(PrintablePackage { string })
    }
}

pub struct PrintablePackage { string: String }

impl std::fmt::Display for PrintablePackage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.string)
    }
}