Skip to main content

panbuild/
execution_context.rs

1use std::collections::BTreeMap;
2use std::fs;
3use std::path;
4
5use serde::{Deserialize, Serialize};
6
7// Make that more robust maybe?
8pub const DEFAULT_CACHE_DIR: &str = ".panbuild/";
9pub const DEFAULT_SOURCE_TYPE: &str = "flatpak";
10
11pub struct ExecutionContext {
12    pub source_filename: String,
13    pub source_type: String,
14    pub data_dir: String,
15    pub content: String,
16    pub manifest: crate::manifests::manifest::AbstractManifest,
17}
18impl Default for ExecutionContext {
19    fn default() -> Self {
20        return ExecutionContext {
21            source_filename: "".to_string(),
22            source_type: DEFAULT_SOURCE_TYPE.to_string(),
23            data_dir: "".to_string(),
24            content: "".to_string(),
25            manifest: crate::manifests::manifest::AbstractManifest::default(),
26        };
27    }
28}
29
30#[derive(Deserialize, Serialize, Debug, Default)]
31#[serde(default)]
32pub struct PanbuildConfig {
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub current_workspace: Option<String>,
35
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub last_build: Option<String>,
38
39    pub workspaces: BTreeMap<String, String>,
40}
41
42pub fn write_config(config: &PanbuildConfig) -> Result<PanbuildConfig, String> {
43    let cache_dir = path::Path::new(DEFAULT_CACHE_DIR);
44    if !cache_dir.is_dir() {
45        match fs::create_dir(cache_dir) {
46            Ok(_) => {}
47            Err(e) => return Err(format!("Could not create cache dir at {}", DEFAULT_CACHE_DIR)),
48        };
49    }
50
51    let config_content = match serde_yaml::to_string(&config) {
52        Ok(m) => m,
53        Err(e) => return Err(format!("Failed to dump the config {}", e)),
54    };
55
56    let config_path = DEFAULT_CACHE_DIR.to_owned() + "config.yaml";
57    let config_path = path::Path::new(&config_path);
58    match fs::write(config_path, config_content) {
59        Ok(m) => m,
60        Err(e) => return Err(format!("Failed to write the config file at {}: {}", config_path.to_str().unwrap_or(""), e)),
61    };
62
63    read_config()
64}
65
66pub fn read_config() -> Result<PanbuildConfig, String> {
67    // Make that more robust maybe?
68    let config_path = DEFAULT_CACHE_DIR.to_owned() + "config.yaml";
69    let config_path = path::Path::new(&config_path);
70    let config_content = match fs::read_to_string(config_path) {
71        Ok(m) => m,
72        Err(e) => return Err(format!("Failed to read the config file at {}", config_path.to_str().unwrap_or(""))),
73    };
74
75    let config: PanbuildConfig = match serde_yaml::from_str(&config_content) {
76        Ok(m) => m,
77        Err(e) => return Err(format!("Failed to parse the config file at {}: {}.", config_path.to_str().unwrap_or(""), e)),
78    };
79    Ok(config)
80}
81
82pub fn read_or_init_config() -> Result<PanbuildConfig, String> {
83    match read_config() {
84        Ok(config) => Ok(config),
85        Err(_) => match write_config(&PanbuildConfig::default()) {
86            Ok(c) => return Ok(c),
87            Err(e) => return Err(e),
88        },
89    }
90}