yolk/
eggs_config.rs

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use normalize_path::NormalizePath;
use std::{
    collections::{HashMap, HashSet},
    path::{Path, PathBuf},
    str::FromStr,
};

use miette::{miette, IntoDiagnostic as _};
use rhai::Dynamic;

use crate::{script::rhai_error::RhaiError, util::PathExt as _};

macro_rules! rhai_error {
    ($($tt:tt)*) => {
        RhaiError::Other(miette!($($tt)*))
    };
}

/// How the contents of an egg should be deployed.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub enum DeploymentStrategy {
    /// Recursively traverse the directory structure until a directory / file doesn't exist yet, then symlink there.
    /// This allows stow-like behavior.
    Merge,
    /// Simply deploy to the given target, or fail.
    #[default]
    Put,
}

impl FromStr for DeploymentStrategy {
    type Err = miette::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "merge" => Ok(DeploymentStrategy::Merge),
            "put" => Ok(DeploymentStrategy::Put),
            _ => miette::bail!(
                help = "strategy must be one of 'merge' or 'put'",
                "Invalid deployment strategy {}",
                s
            ),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct EggConfig {
    /// The targets map is a map from `path-relative-to-egg-dir` -> `path-where-it-should-go`.
    pub targets: HashMap<PathBuf, PathBuf>,
    pub enabled: bool,
    pub templates: HashSet<PathBuf>,
    /// The "main" file of this egg -- currently used to determine which path should be opened by `yolk edit`.
    pub main_file: Option<PathBuf>,
    pub strategy: DeploymentStrategy,
}

impl Default for EggConfig {
    fn default() -> Self {
        EggConfig {
            enabled: true,
            targets: HashMap::new(),
            templates: HashSet::new(),
            main_file: None,
            strategy: Default::default(),
        }
    }
}

impl EggConfig {
    pub fn new(in_egg: impl AsRef<Path>, deployed_to: impl AsRef<Path>) -> Self {
        let in_egg = in_egg.as_ref();
        EggConfig {
            enabled: true,
            targets: maplit::hashmap! {
                in_egg.to_path_buf() => deployed_to.as_ref().to_path_buf()
            },
            templates: HashSet::new(),
            main_file: None,
            strategy: DeploymentStrategy::default(),
        }
    }
    pub fn new_merge(in_egg: impl AsRef<Path>, deployed_to: impl AsRef<Path>) -> Self {
        Self::new(in_egg, deployed_to).with_strategy(DeploymentStrategy::Merge)
    }

    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    pub fn with_template(mut self, template: impl AsRef<Path>) -> Self {
        self.templates.insert(template.as_ref().to_path_buf());
        self
    }

    pub fn with_strategy(mut self, strategy: DeploymentStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    pub fn with_main_file(mut self, main_file: impl AsRef<Path>) -> Self {
        self.main_file = Some(main_file.as_ref().to_path_buf());
        self
    }

    /// Add a new target from a path inside the egg dir to the path it should be deployed as.
    pub fn with_target(mut self, in_egg: impl AsRef<Path>, deploy_to: impl AsRef<Path>) -> Self {
        self.targets.insert(
            in_egg.as_ref().to_path_buf(),
            deploy_to.as_ref().to_path_buf(),
        );
        self
    }

    /// Returns the targets map, but with any `~` expanded to the home directory.
    ///
    /// The targets map is a map from `path-relative-to-egg-dir` -> `path-where-it-should-go`.
    pub fn targets_expanded(
        &self,
        home: impl AsRef<Path>,
        egg_root: impl AsRef<Path>,
    ) -> miette::Result<HashMap<PathBuf, PathBuf>> {
        let egg_root = egg_root.as_ref();
        self.targets
            .iter()
            .map(|(source, target)| {
                let source = egg_root.canonical()?.join(source);
                let target = target.expanduser();
                let target = if target.is_absolute() {
                    target
                } else {
                    home.as_ref().join(target)
                };
                Ok((source.normalize(), target.normalize()))
            })
            .collect()
    }

    /// Expand the glob patterns in the `templates` field to a list of paths.
    /// The globbed paths are considered relative to `in_dir`. The resulting list of paths will contain absolute paths.
    pub fn templates_globexpanded(&self, in_dir: impl AsRef<Path>) -> miette::Result<Vec<PathBuf>> {
        let in_dir = in_dir.as_ref();
        let mut paths = Vec::new();
        for globbed in &self.templates {
            let expanded = glob::glob(&in_dir.join(globbed).to_string_lossy()).into_diagnostic()?;
            for path in expanded {
                paths.push(path.into_diagnostic()?);
            }
        }
        Ok(paths)
    }

    pub fn from_dynamic(value: Dynamic) -> Result<Self, RhaiError> {
        if let Ok(target_path) = value.as_immutable_string_ref() {
            return Ok(EggConfig::new(".", target_path.to_string()));
        }
        let Ok(map) = value.as_map_ref() else {
            return Err(rhai_error!("egg value must be a string or a map"));
        };
        let targets = map
            .get("targets")
            .ok_or_else(|| rhai_error!("egg table must contain a 'target' key"))?;

        let targets = if let Ok(targets) = targets.as_immutable_string_ref() {
            maplit::hashmap! { PathBuf::from(".") => PathBuf::from(targets.to_string()) }
        } else if let Ok(targets) = targets.as_map_ref() {
            targets
                .clone()
                .into_iter()
                .map(|(k, v)| {
                    Ok::<_, RhaiError>((
                        PathBuf::from(&*k),
                        PathBuf::from(&v.into_string().map_err(|e| {
                            rhai_error!("target file value must be a path, but got {e}")
                        })?),
                    ))
                })
                .collect::<Result<_, _>>()?
        } else {
            return Err(rhai_error!("egg `targets` must be a string or a map"));
        };

        let main_file = match map.get("main_file") {
            Some(path) => Some(
                path.as_immutable_string_ref()
                    .map_err(|e| rhai_error!("main_file must be a path, but got {e}"))?
                    .to_string()
                    .into(),
            ),
            None => None,
        };

        let strategy = match map.get("strategy") {
            Some(strategy) => {
                DeploymentStrategy::from_str(&strategy.to_string()).map_err(RhaiError::Other)?
            }
            None => DeploymentStrategy::default(),
        };

        let templates =
            if let Some(templates) = map.get("templates") {
                templates
                    .as_array_ref()
                    .map_err(|t| rhai_error!("`templates` must be a list, but got {t}"))?
                    .iter()
                    .map(|x| {
                        Ok::<_, RhaiError>(PathBuf::from(x.clone().into_string().map_err(|e| {
                            rhai_error!("template entry must be a path, but got {e}")
                        })?))
                    })
                    .collect::<Result<HashSet<_>, _>>()?
            } else {
                HashSet::new()
            };

        let enabled = if let Some(x) = map.get("enabled") {
            x.as_bool()
                .map_err(|t| rhai_error!("`enabled` must be a list, but got {t}"))?
        } else {
            true
        };
        Ok(EggConfig {
            targets,
            enabled,
            templates,
            main_file,
            strategy,
        })
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashSet;

    use assert_fs::{
        prelude::{FileWriteStr as _, PathChild as _},
        TempDir,
    };
    use maplit::hashset;
    use miette::IntoDiagnostic as _;
    use pretty_assertions::assert_eq;

    use crate::{
        eggs_config::{DeploymentStrategy, EggConfig},
        util::test_util::TestResult,
    };

    use rstest::rstest;

    #[rstest]
    #[case(
        indoc::indoc! {r#"
            #{
                enabled: false,
                targets: #{ "foo": "~/bar" },
                templates: ["foo"],
                main_file: "foo",
                strategy: "merge",
            }
        "#},
        EggConfig::new_merge("foo", "~/bar")
            .with_enabled(false)
            .with_template("foo")
            .with_strategy(DeploymentStrategy::Merge)
            .with_main_file("foo")
    )]
    #[case(r#"#{ targets: "~/bar" }"#, EggConfig::new(".", "~/bar"))]
    #[case(r#""~/bar""#, EggConfig::new(".", "~/bar"))]
    fn test_read_eggs_config(#[case] input: &str, #[case] expected: EggConfig) -> TestResult {
        let result = rhai::Engine::new().eval(input)?;
        assert_eq!(EggConfig::from_dynamic(result)?, expected);
        Ok(())
    }

    #[test]
    fn test_template_globbed() -> TestResult {
        let home = TempDir::new().into_diagnostic()?;
        let config = EggConfig::new_merge(home.to_str().unwrap(), ".")
            .with_template("foo")
            .with_template("**/*.foo");
        home.child("foo").write_str("a")?;
        home.child("bar/baz/a.foo").write_str("a")?;
        home.child("bar/a.foo").write_str("a")?;
        home.child("bar/foo").write_str("a")?;
        let result = config.templates_globexpanded(&home)?;

        assert_eq!(
            result.into_iter().collect::<HashSet<_>>(),
            hashset![
                home.child("foo").path().to_path_buf(),
                home.child("bar/baz/a.foo").path().to_path_buf(),
                home.child("bar/a.foo").path().to_path_buf(),
            ]
        );
        Ok(())
    }
}