sheldon 0.8.5

Fast, configurable, shell plugin manager.
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! The raw config file.

use std::collections::BTreeMap;
use std::fmt;
use std::path::PathBuf;
use std::result;
use std::str;
use std::str::FromStr;

use anyhow::Result;
use indexmap::IndexMap;
use regex_macro::regex;
use serde::de;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;
use url::Url;

use crate::config::{GitReference, Shell};

/// The contents of the configuration file.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct RawConfig {
    /// What type of shell is being used.
    pub shell: Option<Shell>,
    /// Which files to match and use in a plugin's directory.
    #[serde(rename = "match")]
    pub matches: Option<Vec<String>>,
    /// The default list of template names to apply to each matched file.
    pub apply: Option<Vec<String>>,
    /// A map of name to template string.
    pub templates: IndexMap<String, String>,
    /// A map of name to plugin.
    pub plugins: IndexMap<String, RawPlugin>,
    /// Any extra keys,
    #[serde(flatten, deserialize_with = "deserialize_rest_toml_value")]
    pub rest: Option<toml::Value>,
}

/// The actual plugin configuration.
#[derive(Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(default)]
pub struct RawPlugin {
    /// A clonable Git repository.
    pub git: Option<Url>,
    /// A clonable Gist repository.
    pub gist: Option<GistRepository>,
    /// A clonable GitHub repository.
    pub github: Option<GitHubRepository>,
    /// A downloadable file.
    pub remote: Option<Url>,
    /// A local directory.
    pub local: Option<PathBuf>,
    /// An inline script.
    pub inline: Option<String>,
    /// What protocol to use when cloning a repository.
    pub proto: Option<GitProtocol>,
    /// The Git reference to checkout.
    #[serde(flatten)]
    pub reference: Option<GitReference>,
    /// Which directory to use in this plugin.
    ///
    /// This directory can contain template parameters.
    pub dir: Option<String>,
    /// Which files to use in this plugin's directory. If this is `None` then
    /// this will figured out based on the global `matches` field.
    ///
    /// These files can contain template parameters.
    #[serde(rename = "use")]
    pub uses: Option<Vec<String>>,
    /// What templates to apply to each matched file. If this is `None` then the
    /// default templates will be applied.
    pub apply: Option<Vec<String>>,
    /// If configured, only installs this plugin if one of the given profiles is
    /// set in the SHELDON_PROFILE environment variable.
    pub profiles: Option<Vec<String>>,
    /// Hooks executed during template evaluation.
    pub hooks: Option<BTreeMap<String, String>>,
    /// Any extra keys,
    #[serde(flatten, deserialize_with = "deserialize_rest_toml_value")]
    pub rest: Option<toml::Value>,
}

/// A Gist repository identifier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GistRepository {
    /// The GitHub username / organization.
    pub owner: Option<String>,
    /// The Gist identifier.
    pub identifier: String,
}

/// A GitHub repository identifier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitHubRepository {
    /// The GitHub username / organization.
    pub owner: String,
    /// The GitHub repository name.
    pub name: String,
}

/// The Git protocol.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitProtocol {
    Git,
    Https,
    Ssh,
}

////////////////////////////////////////////////////////////////////////////////
// Serialization implementations
////////////////////////////////////////////////////////////////////////////////

impl fmt::Display for Shell {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Bash => f.write_str("bash"),
            Self::Zsh => f.write_str("zsh"),
        }
    }
}

impl fmt::Display for GitProtocol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Git => f.write_str("git"),
            Self::Https => f.write_str("https"),
            Self::Ssh => f.write_str("ssh"),
        }
    }
}

impl fmt::Display for GistRepository {
    /// Displays as "[{owner}/]{identifier}".
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self {
                owner: Some(owner),
                identifier,
            } => write!(f, "{owner}/{identifier}"),
            Self {
                owner: None,
                identifier,
            } => write!(f, "{identifier}"),
        }
    }
}

impl fmt::Display for GitHubRepository {
    /// Displays as "{owner}/{repository}".
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.owner, self.name)
    }
}

macro_rules! impl_serialize_as_str {
    ($name:ident) => {
        impl Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                serializer.serialize_str(&self.to_string())
            }
        }
    };
}

impl_serialize_as_str! { Shell }
impl_serialize_as_str! { GitProtocol }
impl_serialize_as_str! { GistRepository }
impl_serialize_as_str! { GitHubRepository }

////////////////////////////////////////////////////////////////////////////////
// Deserialization implementations
////////////////////////////////////////////////////////////////////////////////

/// Produced when we fail to parse the shell type.
#[derive(Debug, Error)]
#[error("expected one of `bash` or `zsh`, got `{}`", self.0)]
pub struct ParseShellError(String);

impl FromStr for Shell {
    type Err = ParseShellError;

    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        match &*s.to_lowercase() {
            "bash" => Ok(Self::Bash),
            "zsh" => Ok(Self::Zsh),
            s => Err(ParseShellError(s.to_string())),
        }
    }
}

/// Produced when we fail to parse a Git protocol.
#[derive(Debug, Error)]
#[error("expected one of `git`, `https`, or `ssh`, got `{}`", self.0)]
pub struct ParseGitProtocolError(String);

impl FromStr for GitProtocol {
    type Err = ParseGitProtocolError;

    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        match s {
            "git" => Ok(Self::Git),
            "https" => Ok(Self::Https),
            "ssh" => Ok(Self::Ssh),
            s => Err(ParseGitProtocolError(s.to_string())),
        }
    }
}

/// Produced when we fail to parse a Gist repository.
#[derive(Debug, Error)]
#[error("`{}` is not a valid Gist identifier, the hash or username/hash should be provided", self.0)]
pub struct ParseGistRepositoryError(String);

impl FromStr for GistRepository {
    type Err = ParseGistRepositoryError;

    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        let re = regex!("^((?P<owner>[a-zA-Z0-9_-]+)/)?(?P<identifier>[a-fA-F0-9]+)$");
        let captures = re
            .captures(s)
            .ok_or_else(|| ParseGistRepositoryError(s.to_string()))?;
        let owner = captures.name("owner").map(|m| m.as_str().to_string());
        let identifier = captures.name("identifier").unwrap().as_str().to_string();
        Ok(Self { owner, identifier })
    }
}

/// Produced when we fail to parse a GitHub repository.
#[derive(Debug, Error)]
#[error("`{}` is not a valid GitHub repository, the username/repository should be provided", self.0)]
pub struct ParseGitHubRepositoryError(String);

impl FromStr for GitHubRepository {
    type Err = ParseGitHubRepositoryError;

    fn from_str(s: &str) -> result::Result<Self, Self::Err> {
        let re = regex!("^(?P<owner>[a-zA-Z0-9_-]+)/(?P<name>[a-zA-Z0-9\\._-]+)$");
        let captures = re
            .captures(s)
            .ok_or_else(|| ParseGitHubRepositoryError(s.to_string()))?;
        let owner = captures.name("owner").unwrap().as_str().to_string();
        let name = captures.name("name").unwrap().as_str().to_string();
        Ok(Self { owner, name })
    }
}

macro_rules! impl_deserialize_from_str {
    ($module:ident, $name:ident, $expecting:expr) => {
        mod $module {
            use super::*;

            struct Visitor;

            impl<'de> de::Visitor<'de> for Visitor {
                type Value = $name;

                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str($expecting)
                }

                fn visit_str<E>(self, value: &str) -> result::Result<Self::Value, E>
                where
                    E: de::Error,
                {
                    $name::from_str(value).map_err(|e| de::Error::custom(e.to_string()))
                }
            }

            impl<'de> Deserialize<'de> for $name {
                fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
                where
                    D: Deserializer<'de>,
                {
                    deserializer.deserialize_str(Visitor)
                }
            }
        }
    };
}

impl_deserialize_from_str! { shell, Shell, "a supported shell type" }
impl_deserialize_from_str! { git_protocol, GitProtocol, "a Git protocol type" }
impl_deserialize_from_str! { gist_repository, GistRepository, "a Gist identifier" }
impl_deserialize_from_str! { github_repository, GitHubRepository, "a GitHub repository" }

/// Deserialize the remaining keys into an [`Option<toml::Value>`]. Empty tables
/// are coerced to [`None`].
fn deserialize_rest_toml_value<'de, D>(deserializer: D) -> Result<Option<toml::Value>, D::Error>
where
    D: Deserializer<'de>,
{
    let value: toml::Value = de::Deserialize::deserialize(deserializer)?;
    Ok(match value {
        toml::Value::Table(table) if table.is_empty() => None,
        value => Some(value),
    })
}

////////////////////////////////////////////////////////////////////////////////
// Unit tests
////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;

    use pretty_assertions::assert_eq;

    #[test]
    fn shell_to_string() {
        assert_eq!(Shell::Bash.to_string(), "bash");
        assert_eq!(Shell::Zsh.to_string(), "zsh");
    }

    #[test]
    fn gist_repository_to_string() {
        let test = GistRepository {
            owner: None,
            identifier: "579d02802b1cc17baed07753d09f5009".to_string(),
        };
        assert_eq!(test.to_string(), "579d02802b1cc17baed07753d09f5009");
    }

    #[test]
    fn gist_repository_to_string_with_owner() {
        let test = GistRepository {
            owner: Some("rossmacarthur".to_string()),
            identifier: "579d02802b1cc17baed07753d09f5009".to_string(),
        };
        assert_eq!(
            test.to_string(),
            "rossmacarthur/579d02802b1cc17baed07753d09f5009"
        );
    }

    #[test]
    fn github_repository_to_string() {
        let test = GitHubRepository {
            owner: "rossmacarthur".to_string(),
            name: "sheldon-test".to_string(),
        };
        assert_eq!(test.to_string(), "rossmacarthur/sheldon-test");
    }

    #[derive(Debug, Deserialize)]
    struct ShellTest {
        s: Shell,
    }

    #[test]
    fn shell_deserialize_as_str() {
        let test: ShellTest = toml::from_str("s = 'bash'").unwrap();
        assert_eq!(test.s, Shell::Bash);
    }

    #[test]
    fn shell_deserialize_invalid() {
        let error = toml::from_str::<ShellTest>("s = 'ksh'").unwrap_err();
        assert_eq!(
            error.to_string(),
            "TOML parse error at line 1, column 5
  |
1 | s = 'ksh'
  |     ^^^^^
expected one of `bash` or `zsh`, got `ksh`
"
        );
    }

    #[derive(Debug, Deserialize)]
    struct TestGitReference {
        #[serde(flatten)]
        g: GitReference,
    }

    #[test]
    fn git_reference_deserialize_branch() {
        let test: TestGitReference = toml::from_str("branch = 'master'").unwrap();
        assert_eq!(test.g, GitReference::Branch(String::from("master")));
    }

    #[test]
    fn git_reference_deserialize_tag() {
        let test: TestGitReference = toml::from_str("tag = 'v0.5.1'").unwrap();
        assert_eq!(test.g, GitReference::Tag(String::from("v0.5.1")));
    }

    #[test]
    fn git_reference_deserialize_rev() {
        let test: TestGitReference = toml::from_str("rev = 'cd65e828'").unwrap();
        assert_eq!(test.g, GitReference::Rev(String::from("cd65e828")));
    }

    #[derive(Debug, Deserialize)]
    struct TestGistRepository {
        g: GistRepository,
    }

    #[test]
    fn gist_repository_deserialize() {
        let test: TestGistRepository =
            toml::from_str("g = 'rossmacarthur/579d02802b1cc17baed07753d09f5009'").unwrap();
        assert_eq!(
            test.g,
            GistRepository {
                owner: Some("rossmacarthur".to_string()),
                identifier: "579d02802b1cc17baed07753d09f5009".to_string()
            }
        );
    }

    #[test]
    fn gist_repository_deserialize_two_slashes() {
        let error = toml::from_str::<TestGistRepository>(
            "g = 'rossmacarthur/579d02802b1cc17baed07753d09f5009/test'",
        )
        .unwrap_err();
        assert_eq!(
            error.to_string(),
            "TOML parse error at line 1, column 5
  |
1 | g = 'rossmacarthur/579d02802b1cc17baed07753d09f5009/test'
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`rossmacarthur/579d02802b1cc17baed07753d09f5009/test` is not a valid Gist identifier, the hash or \
             username/hash should be provided
"
        );
    }

    #[test]
    fn gist_repository_deserialize_not_hex() {
        let error = toml::from_str::<TestGistRepository>("g = 'nothex'").unwrap_err();
        assert_eq!(
            error.to_string(),
            "TOML parse error at line 1, column 5
  |
1 | g = 'nothex'
  |     ^^^^^^^^
`nothex` is not a valid Gist identifier, the hash or username/hash should be provided
"
        );
    }

    #[derive(Debug, Deserialize)]
    struct TestGitHubRepository {
        g: GitHubRepository,
    }

    #[test]
    fn github_repository_deserialize() {
        let test: TestGitHubRepository =
            toml::from_str("g = 'rossmacarthur/sheldon-test'").unwrap();
        assert_eq!(
            test.g,
            GitHubRepository {
                owner: "rossmacarthur".to_string(),
                name: "sheldon-test".to_string()
            }
        );
    }

    #[test]
    fn github_repository_deserialize_two_slashes() {
        let error =
            toml::from_str::<TestGitHubRepository>("g = 'rossmacarthur/sheldon/test'").unwrap_err();
        assert_eq!(
            error.to_string(),
            "TOML parse error at line 1, column 5
  |
1 | g = 'rossmacarthur/sheldon/test'
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`rossmacarthur/sheldon/test` is not a valid GitHub repository, the username/repository should be \
             provided
"
        );
    }

    #[test]
    fn github_repository_deserialize_no_slashes() {
        let error = toml::from_str::<TestGitHubRepository>("g = 'noslash'").unwrap_err();
        assert_eq!(
            error.to_string(),
            "TOML parse error at line 1, column 5
  |
1 | g = 'noslash'
  |     ^^^^^^^^^
`noslash` is not a valid GitHub repository, the username/repository should be provided
"
        );
    }

    #[test]
    fn raw_plugin_deserialize_git() {
        let expected = RawPlugin {
            git: Some(Url::parse("https://github.com/rossmacarthur/sheldon-test").unwrap()),
            ..Default::default()
        };
        let plugin: RawPlugin =
            toml::from_str("git = 'https://github.com/rossmacarthur/sheldon-test'").unwrap();
        assert_eq!(plugin, expected);
    }

    #[test]
    fn raw_plugin_deserialize_github() {
        let expected = RawPlugin {
            github: Some(GitHubRepository {
                owner: "rossmacarthur".into(),
                name: "sheldon-test".into(),
            }),
            ..Default::default()
        };
        let plugin: RawPlugin = toml::from_str("github = 'rossmacarthur/sheldon-test'").unwrap();
        assert_eq!(plugin, expected);
    }

    #[test]
    fn raw_plugin_deserialize_profiles() {
        let expected = RawPlugin {
            profiles: Some(vec!["p1".into(), "p2".into()]),
            ..Default::default()
        };
        let plugin: RawPlugin = toml::from_str("profiles = ['p1', 'p2']").unwrap();
        assert_eq!(plugin, expected);
    }

    #[test]
    fn raw_plugin_deserialize_hooks() {
        let expected = RawPlugin {
            hooks: Some(BTreeMap::from([
                ("pre".into(), "PRE".into()),
                ("post".into(), "POST".into()),
            ])),
            ..Default::default()
        };
        let plugin: RawPlugin = toml::from_str("hooks.pre = 'PRE'\nhooks.post = 'POST'").unwrap();
        assert_eq!(plugin, expected);
    }
}