ydevlib/
template.rs

1#[allow(unused)]
2use {
3    crate::recipe::Recipe,
4    crate::utils::{scan_dir, scan_dir_for_dir, scan_dir_for_file, verify_content},
5    anyhow::{Context, Error, Result},
6    jlogger::{jdebug, jerror, jinfo, jwarn},
7    log::{debug, error, info, warn},
8    std::collections::HashMap,
9    std::fmt::Display,
10    std::fs,
11    std::path::{Path, PathBuf},
12};
13
14pub struct TemplateFile {
15    path: PathBuf,
16    conf: Option<String>,
17    bblayer: Option<String>,
18    depends: HashMap<String, ()>,
19    content_loaded: bool,
20}
21
22impl TemplateFile {
23    pub fn new(p: &Path) -> Option<Self> {
24        if !p.is_dir() {
25            return None;
26        }
27
28        Some(TemplateFile {
29            path: p.to_path_buf(),
30            conf: None,
31            bblayer: None,
32            depends: HashMap::<String, ()>::new(),
33            content_loaded: false,
34        })
35    }
36
37    pub fn load_content(&mut self) -> Result<()> {
38        let mut conf: Option<String> = None;
39        let mut bblayer: Option<String> = None;
40        let path = &self.path;
41        let depends = &mut self.depends;
42        if self.content_loaded {
43            return Ok(());
44        }
45
46        scan_dir_for_file(path.as_path(), &mut |p: PathBuf| -> Result<()> {
47            if let Some(l) = p.file_name() {
48                if let Some(f) = l.to_str() {
49                    if f.ends_with("_local.conf.inc") {
50                        let content = fs::read_to_string(p.to_str().unwrap())?;
51                        conf = verify_content(content);
52                        if conf.is_none() {
53                            jwarn!("local config file {} is empty.", p.display());
54                        }
55                    }
56
57                    if f.ends_with("_bblayers.conf.inc") {
58                        let content = fs::read_to_string(p.to_str().unwrap())?;
59                        bblayer = verify_content(content);
60                        if bblayer.is_none() {
61                            jwarn!("bblayer file {} is empty.", p.display());
62                        }
63                    }
64
65                    if f == "included.dep" {
66                        let content = fs::read_to_string(p.to_str().unwrap())?;
67                        if content.trim().is_empty() {
68                            jwarn!("depends file {} is empty.", p.display());
69                        } else {
70                            for f in content.split(' ') {
71                                let f = f.trim();
72                                if !f.is_empty() {
73                                    if depends.get(f).is_some() {
74                                        jwarn!("Multiple {} found.", f);
75                                    }
76                                    depends.insert(f.to_string(), ());
77                                }
78                            }
79                        }
80                    }
81                }
82            }
83            Ok(())
84        })?;
85
86        self.conf = conf;
87        self.bblayer = bblayer;
88        self.content_loaded = true;
89        Ok(())
90    }
91
92    pub fn get_filename(&self) -> &str {
93        self.path.file_name().unwrap().to_str().unwrap()
94    }
95
96    pub fn get_pathname(&self) -> &str {
97        self.path.to_str().unwrap()
98    }
99
100    pub fn get_conf(&self) -> Option<String> {
101        if let Some(c) = &self.conf {
102            let mut result = String::new();
103
104            result.push_str(&format!("{}\n", (0..80).map(|_| "#").collect::<String>()));
105            result.push_str(&format!("# {}\n", self.get_pathname()));
106            result.push_str(c);
107            result.push('\n');
108
109            Some(result)
110        } else {
111            None
112        }
113    }
114
115    pub fn get_bblayer(&self) -> Option<String> {
116        if let Some(c) = &self.bblayer {
117            let mut result = String::new();
118
119            result.push_str(&format!("{}\n", (0..80).map(|_| "#").collect::<String>()));
120            result.push_str(&format!("# {}\n", self.get_pathname()));
121            result.push_str(c);
122            result.push('\n');
123
124            Some(result)
125        } else {
126            None
127        }
128    }
129
130    pub fn get_depends(&self) -> &HashMap<String, ()> {
131        &self.depends
132    }
133}