summaryrefslogtreecommitdiffstats
path: root/src/package/script.rs
blob: c8cde1954a861534157bc26158fe58e44eab8179 (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
use anyhow::Result;
use serde::Deserialize;
use serde::Serialize;

use crate::package::Package;
use crate::phase::Phase;
use crate::phase::PhaseName;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(transparent)]
pub struct Script(String);

impl AsRef<str> for Script {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

pub struct ScriptBuilder<'a> {
    shebang : &'a String,
}

impl<'a> ScriptBuilder<'a> {
    // TODO: Use handlebars and templating instead of hardcoding
    pub fn new(shebang: &'a String) -> Self {
        ScriptBuilder {
            shebang,
        }
    }

    pub fn build(self, package: &Package, phaseorder: &Vec<PhaseName>) -> Result<Script> {
        let mut script = format!("{shebang}\n", shebang = self.shebang);

        for name in phaseorder {
            match package.phases().get(name) {
                Some(Phase::Text(text)) => {
                    script.push_str(&indoc::formatdoc!(r#"
                        ### phase {}
                        {}
                        ### / {} phase
                    "#,
                    name.as_str(),
                    text,
                    name.as_str(),
                    ));

                    script.push_str("\n");
                },

                // TODO: Support path embedding
                // (requires possibility to have stuff in Script type that gets copied to
                // container)
                Some(Phase::Path(pb)) => {
                    script.push_str(&format!(r#"
                        # Phase (from file {path}): {name}
                        # NOT SUPPORTED YET
                        exit 1
                    "#,
                    path = pb.display(),
                    name = name.as_str()));

                    script.push_str("\n");
                },

                None => {
                    script.push_str(&format!("# No script for phase: {name}", name = name.as_str()));
                    script.push_str("\n");
                },
            }
        }

        Ok(Script(script))
    }
}