ydevlib/
base_config.rs

1#[allow(unused)]
2use {
3    crate::meta::MetaTopDir,
4    crate::recipe::Recipe,
5    crate::utils::{scan_dir, scan_dir_for_dir, scan_dir_for_file, verify_content},
6    anyhow::{Context, Error, Result},
7    jlogger::{jdebug, jerror, jinfo, jwarn},
8    log::{debug, error, info, warn},
9    std::collections::HashMap,
10    std::ffi::OsStr,
11    std::fmt::Display,
12    std::fs,
13    std::path::{Path, PathBuf},
14};
15
16#[derive(Default)]
17pub struct BaseConfig {
18    conf: Option<String>,
19    bblayer: Option<String>,
20    dev_setup: Option<String>,
21}
22
23impl BaseConfig {
24    pub fn new(meta_top: &str, build_dir: &str, base_config_dif: Option<String>) -> Result<Self> {
25        let mut conf: Option<String> = None;
26        let mut bblayer: Option<String> = None;
27        let mut dev_setup: Option<String> = None;
28
29        if let Some(base_dir) = base_config_dif {
30            let p = Path::new(&base_dir);
31            if !p.is_dir() {
32                return Err(Error::msg("Invalid base config directory"));
33            }
34            scan_dir_for_file(p, &mut |f: PathBuf| -> Result<()> {
35                let l = f.file_name().unwrap();
36                if l == OsStr::new("local.conf.sample") {
37                    let content = fs::read_to_string(&f)?;
38                    if let Some(c) = verify_content(content) {
39                        conf = Some(c);
40                    }
41                }
42
43                if l == OsStr::new("bblayers.conf.sample") {
44                    let content = fs::read_to_string(&f)?;
45                    if let Some(c) = verify_content(content) {
46                        let modified = c.replace("##OEROOT##", meta_top);
47                        bblayer = Some(modified);
48                    }
49                }
50
51                if l == OsStr::new("dev-init-build-env.sample") {
52                    let content = fs::read_to_string(&f)?;
53                    if let Some(c) = verify_content(content) {
54                        let modified = c
55                            .replace("##OEROOT##", meta_top)
56                            .replace("##BUILDDIR##", build_dir);
57                        dev_setup = Some(modified);
58                    }
59                }
60                Ok(())
61            })?
62        } else {
63            let content = std::include_str!("../base_config/local.conf.sample");
64            if let Some(c) = verify_content(content.to_owned()) {
65                conf = Some(c);
66            }
67
68            let content = std::include_str!("../base_config/bblayers.conf.sample");
69            if let Some(c) = verify_content(content.to_owned()) {
70                let modified = c.replace("##OEROOT##", meta_top);
71                bblayer = Some(modified);
72            }
73
74            let content = std::include_str!("../base_config/dev-init-build-env.sample");
75            if let Some(c) = verify_content(content.to_owned()) {
76                let modified = c
77                    .replace("##OEROOT##", meta_top)
78                    .replace("##BUILDDIR##", build_dir);
79                dev_setup = Some(modified);
80            }
81        }
82
83        Ok(BaseConfig {
84            conf,
85            bblayer,
86            dev_setup,
87        })
88    }
89
90    pub fn conf(&self) -> Option<&str> {
91        if let Some(c) = &self.conf {
92            Some(c.as_str())
93        } else {
94            None
95        }
96    }
97
98    pub fn bblayer(&self) -> Option<&str> {
99        if let Some(c) = &self.bblayer {
100            Some(c.as_str())
101        } else {
102            None
103        }
104    }
105
106    pub fn dev_setup(&self) -> Option<&str> {
107        if let Some(c) = &self.dev_setup {
108            Some(c.as_str())
109        } else {
110            None
111        }
112    }
113}