Skip to content

Commit 2a60788

Browse files
committed
Improve shell specific configuration
- Attach default matches and default templates to the `Shell` struct. - Default to Zsh in config normalization. - Update default templates for Bash. `path` and `fpath` are now removed.
1 parent 0fca1c1 commit 2a60788

4 files changed

Lines changed: 166 additions & 97 deletions

File tree

‎README.md‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,19 @@ For example the **PATH** template adds the plugin directory to the shell `PATH`
553553
variable. A template will be applied to a plugin if you add the template name to
554554
the [`apply`](#apply) field on a plugin.
555555

556-
Available built in templates are
556+
Available built-in templates are different depending on what shell you are
557+
using. The following are available for both Bash and Zsh.
557558

558559
* **source**: source each file in a plugin.
559560
* **PATH**: add the plugin directory to the `PATH` variable.
561+
562+
If you are using Zsh then the following are also available.
563+
560564
* **path**: add the plugin directory to the `path` variable.
561-
* **fpath**: add the plugin directory to the `fpath` variable. As template
562-
strings they could be represented like this
565+
* **fpath**: add the plugin directory to the `fpath` variable.
566+
567+
As template strings in the config file they could be represented like the
568+
following.
563569

564570
```toml
565571
[templates]
@@ -586,7 +592,7 @@ template should be applied to each matched file for the plugin. This defaults to
586592
### Custom templates
587593

588594
It is possible to create your own custom templates, and you can even override
589-
the built in ones.
595+
the built-in ones.
590596

591597
Plugins all have the following information that can be used in templates
592598

@@ -626,7 +632,7 @@ apply = ["function"]
626632

627633
### Example: overriding the PATH template
628634

629-
The built in **PATH** template adds the directory path to the beginning of the
635+
The built-in **PATH** template adds the directory path to the beginning of the
630636
`PATH` variable, we might want to change it to the be added at the end. We could
631637
do this like this
632638

@@ -649,15 +655,20 @@ apply = ["source", "PATH"]
649655

650656
### `shell`
651657

652-
Indicates the shell that you are using Sheldon with. If this field is set to
653-
`bash` the global [`match`](#match) default configuration will use Bash relevant
654-
defaults. If you are using Zsh you don't need to set this value but you may set
655-
it to `zsh`. For example
658+
Indicates the shell that you are using. This setting will affect the default
659+
values for several global config settings. This includes the global
660+
[`match`](#match) setting and the available templates. This defaults to `zsh`.
656661

657662
```toml
658663
shell = "bash"
659664
```
660665

666+
or
667+
668+
```toml
669+
shell = "zsh"
670+
```
671+
661672
### `match`
662673

663674
A list of glob patterns to match against a plugin's contents. The first pattern
@@ -677,7 +688,7 @@ match = [
677688
]
678689
```
679690

680-
If `shell = "bash"` then this defaults to
691+
If the shell is Bash then this defaults to
681692

682693
```toml
683694
match = [

‎docs/src/Configuration.md‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,19 @@ For example the **PATH** template adds the plugin directory to the shell `PATH`
212212
variable. A template will be applied to a plugin if you add the template name to
213213
the [`apply`](#apply) field on a plugin.
214214

215-
Available built in templates are
215+
Available built-in templates are different depending on what shell you are
216+
using. The following are available for both Bash and Zsh.
216217

217218
* **source**: source each file in a plugin.
218219
* **PATH**: add the plugin directory to the `PATH` variable.
220+
221+
If you are using Zsh then the following are also available.
222+
219223
* **path**: add the plugin directory to the `path` variable.
220-
* **fpath**: add the plugin directory to the `fpath` variable. As template
221-
strings they could be represented like this
224+
* **fpath**: add the plugin directory to the `fpath` variable.
225+
226+
As template strings in the config file they could be represented like the
227+
following.
222228

223229
```toml
224230
[templates]
@@ -245,7 +251,7 @@ template should be applied to each matched file for the plugin. This defaults to
245251
### Custom templates
246252

247253
It is possible to create your own custom templates, and you can even override
248-
the built in ones.
254+
the built-in ones.
249255

250256
Plugins all have the following information that can be used in templates
251257

@@ -285,7 +291,7 @@ apply = ["function"]
285291

286292
### Example: overriding the PATH template
287293

288-
The built in **PATH** template adds the directory path to the beginning of the
294+
The built-in **PATH** template adds the directory path to the beginning of the
289295
`PATH` variable, we might want to change it to the be added at the end. We could
290296
do this like this
291297

@@ -308,15 +314,20 @@ apply = ["source", "PATH"]
308314

309315
### `shell`
310316

311-
Indicates the shell that you are using Sheldon with. If this field is set to
312-
`bash` the global [`match`](#match) default configuration will use Bash relevant
313-
defaults. If you are using Zsh you don't need to set this value but you may set
314-
it to `zsh`. For example
317+
Indicates the shell that you are using. This setting will affect the default
318+
values for several global config settings. This includes the global
319+
[`match`](#match) setting and the available templates. This defaults to `zsh`.
315320

316321
```toml
317322
shell = "bash"
318323
```
319324

325+
or
326+
327+
```toml
328+
shell = "zsh"
329+
```
330+
320331
### `match`
321332

322333
A list of glob patterns to match against a plugin's contents. The first pattern
@@ -336,7 +347,7 @@ match = [
336347
]
337348
```
338349

339-
If `shell = "bash"` then this defaults to
350+
If the shell is Bash then this defaults to
340351

341352
```toml
342353
match = [

‎src/config.rs‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use regex::Regex;
1616
use serde::{self, de, Deserialize, Deserializer, Serialize, Serializer};
1717
use url::Url;
1818

19-
use crate::lock::DEFAULT_TEMPLATES;
20-
2119
/// The Gist domain host.
2220
const GIST_HOST: &str = "gist.github.com";
2321

@@ -190,7 +188,7 @@ pub struct RawConfig {
190188
#[derive(Debug)]
191189
pub struct Config {
192190
/// What type of shell is being used.
193-
pub shell: Option<Shell>,
191+
pub shell: Shell,
194192
/// Which files to match and use in a plugin's directory.
195193
pub matches: Option<Vec<String>>,
196194
/// The default list of template names to apply to each matched file.
@@ -576,12 +574,13 @@ where
576574

577575
/// Check whether the specifed templates actually exist.
578576
fn validate_template_names(
577+
shell: &Shell,
579578
apply: &Option<Vec<String>>,
580579
templates: &IndexMap<String, Template>,
581580
) -> Result<()> {
582581
if let Some(apply) = apply {
583582
for name in apply {
584-
if !DEFAULT_TEMPLATES.contains_key(name) && !templates.contains_key(name) {
583+
if !shell.default_templates().contains_key(name) && !templates.contains_key(name) {
585584
bail!("unknown template `{}`", name);
586585
}
587586
}
@@ -628,6 +627,7 @@ impl RawPlugin {
628627
pub fn normalize(
629628
self,
630629
name: String,
630+
shell: &Shell,
631631
templates: &IndexMap<String, Template>,
632632
warnings: &mut Vec<Error>,
633633
) -> Result<Plugin> {
@@ -733,7 +733,7 @@ impl RawPlugin {
733733
bail!("the `proto` field is not supported by this plugin type");
734734
}
735735

736-
validate_template_names(&apply, templates)?;
736+
validate_template_names(shell, &apply, templates)?;
737737

738738
Ok(Plugin::External(ExternalPlugin {
739739
name,
@@ -815,15 +815,17 @@ impl RawConfig {
815815
}
816816
}
817817

818-
validate_template_names(&apply, &templates)?;
818+
let shell = shell.unwrap_or_default();
819+
820+
validate_template_names(&shell, &apply, &templates)?;
819821

820822
// Normalize the plugins.
821823
let mut normalized_plugins = Vec::with_capacity(plugins.len());
822824

823825
for (name, plugin) in plugins {
824826
normalized_plugins.push(
825827
plugin
826-
.normalize(name.clone(), &templates, &mut warnings)
828+
.normalize(name.clone(), &shell, &templates, &mut warnings)
827829
.with_context(s!("failed to normalize plugin `{}`", name))?,
828830
);
829831
}
@@ -1099,7 +1101,12 @@ mod tests {
10991101
let text = format!("{} = '{}'\n{} = '{}'", a, example_a, b, example_b);
11001102
let e = toml::from_str::<RawPlugin>(&text)
11011103
.unwrap()
1102-
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
1104+
.normalize(
1105+
"test".to_string(),
1106+
&Shell::default(),
1107+
&IndexMap::new(),
1108+
&mut Vec::new(),
1109+
)
11031110
.unwrap_err();
11041111
assert_eq!(e.to_string(), "plugin `test` has multiple source fields")
11051112
}
@@ -1126,7 +1133,7 @@ mod tests {
11261133
};
11271134
assert_eq!(
11281135
raw_plugin
1129-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1136+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
11301137
.unwrap(),
11311138
expected
11321139
);
@@ -1156,7 +1163,7 @@ mod tests {
11561163
};
11571164
assert_eq!(
11581165
raw_plugin
1159-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1166+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
11601167
.unwrap(),
11611168
expected
11621169
);
@@ -1182,7 +1189,7 @@ mod tests {
11821189
};
11831190
assert_eq!(
11841191
raw_plugin
1185-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1192+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
11861193
.unwrap(),
11871194
expected
11881195
);
@@ -1213,7 +1220,7 @@ mod tests {
12131220
};
12141221
assert_eq!(
12151222
raw_plugin
1216-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1223+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
12171224
.unwrap(),
12181225
expected
12191226
);
@@ -1242,7 +1249,7 @@ mod tests {
12421249
};
12431250
assert_eq!(
12441251
raw_plugin
1245-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1252+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
12461253
.unwrap(),
12471254
expected
12481255
);
@@ -1270,7 +1277,7 @@ mod tests {
12701277
};
12711278
assert_eq!(
12721279
raw_plugin
1273-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1280+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
12741281
.unwrap(),
12751282
expected
12761283
);
@@ -1299,7 +1306,7 @@ mod tests {
12991306
};
13001307
assert_eq!(
13011308
raw_plugin
1302-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1309+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
13031310
.unwrap(),
13041311
expected
13051312
);
@@ -1324,7 +1331,7 @@ mod tests {
13241331
};
13251332
assert_eq!(
13261333
raw_plugin
1327-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1334+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
13281335
.unwrap(),
13291336
expected
13301337
);
@@ -1343,7 +1350,12 @@ mod tests {
13431350
..Default::default()
13441351
};
13451352
let error = raw_plugin
1346-
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
1353+
.normalize(
1354+
"test".to_string(),
1355+
&Shell::default(),
1356+
&IndexMap::new(),
1357+
&mut Vec::new(),
1358+
)
13471359
.unwrap_err();
13481360
assert_eq!(
13491361
error.to_string(),
@@ -1364,7 +1376,12 @@ mod tests {
13641376
..Default::default()
13651377
};
13661378
let error = raw_plugin
1367-
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
1379+
.normalize(
1380+
"test".to_string(),
1381+
&Shell::default(),
1382+
&IndexMap::new(),
1383+
&mut Vec::new(),
1384+
)
13681385
.unwrap_err();
13691386
assert_eq!(
13701387
error.to_string(),
@@ -1390,7 +1407,7 @@ mod tests {
13901407
};
13911408
assert_eq!(
13921409
raw_plugin
1393-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1410+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
13941411
.unwrap(),
13951412
expected
13961413
);
@@ -1409,7 +1426,7 @@ mod tests {
14091426
};
14101427
assert_eq!(
14111428
raw_plugin
1412-
.normalize(name, &IndexMap::new(), &mut Vec::new())
1429+
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
14131430
.unwrap(),
14141431
expected
14151432
);
@@ -1423,7 +1440,12 @@ mod tests {
14231440
..Default::default()
14241441
};
14251442
let error = raw_plugin
1426-
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
1443+
.normalize(
1444+
"test".to_string(),
1445+
&Shell::default(),
1446+
&IndexMap::new(),
1447+
&mut Vec::new(),
1448+
)
14271449
.unwrap_err();
14281450
assert_eq!(
14291451
error.to_string(),
@@ -1442,7 +1464,12 @@ mod tests {
14421464
..Default::default()
14431465
};
14441466
let error = raw_plugin
1445-
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
1467+
.normalize(
1468+
"test".to_string(),
1469+
&Shell::default(),
1470+
&IndexMap::new(),
1471+
&mut Vec::new(),
1472+
)
14461473
.unwrap_err();
14471474
assert_eq!(error.to_string(), "unknown template `test`");
14481475
}

0 commit comments

Comments
 (0)