summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b7bf8c2fac83ae7f7c8867ddf2e86b029eaede0c (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
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Profile {
    pub packages: Vec<String>,
    pub regex_packages: Vec<String>,
}

impl Profile {
    pub fn load(path: &Path) -> Result<Self, String> {
        let profile = fs::read_to_string(path).map_err(|error| error.to_string())?;
        let parent = path.parent().ok_or("profile path has no parent")?;
        let mut packages = Vec::new();
        let mut regex_packages = Vec::new();

        for line in profile.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            if let Some(values) = line.strip_prefix("base:") {
                extend_words(&mut packages, values);
            } else if let Some(relative) = line.strip_prefix("include:") {
                let list = parent.join(relative.trim());
                let contents = fs::read_to_string(&list)
                    .map_err(|error| format!("{}: {error}", list.display()))?;
                for entry in contents.lines().map(str::trim) {
                    if entry.is_empty() || entry.starts_with('#') {
                        continue;
                    }
                    let package = entry.split('@').next().unwrap_or(entry).trim();
                    if !package.is_empty() {
                        packages.push(package.to_owned());
                    }
                }
            } else if let Some(value) = line.strip_prefix("regex:") {
                regex_packages.push(value.trim().to_owned());
            } else {
                return Err(format!("unsupported profile statement: {line}"));
            }
        }
        packages.sort();
        packages.dedup();
        regex_packages.sort();
        regex_packages.dedup();
        Ok(Self { packages, regex_packages })
    }

    pub fn install_arguments(&self) -> Vec<String> {
        self.regex_packages.iter().chain(self.packages.iter()).cloned().collect()
    }
}

fn extend_words(target: &mut Vec<String>, values: &str) {
    target.extend(values.split_whitespace().map(str::to_owned));
}

pub fn artifact_path(project: &Path) -> PathBuf {
    project.join("out/alt-controller-rootfs.tar")
}