Skip to content

Commit

Permalink
Improve shell specific configuration
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
rossmacarthur committed Aug 14, 2020
1 parent 0fca1c1 commit 2a60788
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 97 deletions.
31 changes: 21 additions & 10 deletions README.md
Expand Up @@ -553,13 +553,19 @@ For example the **PATH** template adds the plugin directory to the shell `PATH`
variable. A template will be applied to a plugin if you add the template name to
the [`apply`](#apply) field on a plugin.

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

* **source**: source each file in a plugin.
* **PATH**: add the plugin directory to the `PATH` variable.

If you are using Zsh then the following are also available.

* **path**: add the plugin directory to the `path` variable.
* **fpath**: add the plugin directory to the `fpath` variable. As template
strings they could be represented like this
* **fpath**: add the plugin directory to the `fpath` variable.

As template strings in the config file they could be represented like the
following.

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

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

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

Expand Down Expand Up @@ -626,7 +632,7 @@ apply = ["function"]

### Example: overriding the PATH template

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

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

### `shell`

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

```toml
shell = "bash"
```

or

```toml
shell = "zsh"
```

### `match`

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

If `shell = "bash"` then this defaults to
If the shell is Bash then this defaults to

```toml
match = [
Expand Down
31 changes: 21 additions & 10 deletions docs/src/Configuration.md
Expand Up @@ -212,13 +212,19 @@ For example the **PATH** template adds the plugin directory to the shell `PATH`
variable. A template will be applied to a plugin if you add the template name to
the [`apply`](#apply) field on a plugin.

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

* **source**: source each file in a plugin.
* **PATH**: add the plugin directory to the `PATH` variable.

If you are using Zsh then the following are also available.

* **path**: add the plugin directory to the `path` variable.
* **fpath**: add the plugin directory to the `fpath` variable. As template
strings they could be represented like this
* **fpath**: add the plugin directory to the `fpath` variable.

As template strings in the config file they could be represented like the
following.

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

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

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

Expand Down Expand Up @@ -285,7 +291,7 @@ apply = ["function"]

### Example: overriding the PATH template

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

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

### `shell`

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

```toml
shell = "bash"
```

or

```toml
shell = "zsh"
```

### `match`

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

If `shell = "bash"` then this defaults to
If the shell is Bash then this defaults to

```toml
match = [
Expand Down
71 changes: 49 additions & 22 deletions src/config.rs
Expand Up @@ -16,8 +16,6 @@ use regex::Regex;
use serde::{self, de, Deserialize, Deserializer, Serialize, Serializer};
use url::Url;

use crate::lock::DEFAULT_TEMPLATES;

/// The Gist domain host.
const GIST_HOST: &str = "gist.github.com";

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

/// Check whether the specifed templates actually exist.
fn validate_template_names(
shell: &Shell,
apply: &Option<Vec<String>>,
templates: &IndexMap<String, Template>,
) -> Result<()> {
if let Some(apply) = apply {
for name in apply {
if !DEFAULT_TEMPLATES.contains_key(name) && !templates.contains_key(name) {
if !shell.default_templates().contains_key(name) && !templates.contains_key(name) {
bail!("unknown template `{}`", name);
}
}
Expand Down Expand Up @@ -628,6 +627,7 @@ impl RawPlugin {
pub fn normalize(
self,
name: String,
shell: &Shell,
templates: &IndexMap<String, Template>,
warnings: &mut Vec<Error>,
) -> Result<Plugin> {
Expand Down Expand Up @@ -733,7 +733,7 @@ impl RawPlugin {
bail!("the `proto` field is not supported by this plugin type");
}

validate_template_names(&apply, templates)?;
validate_template_names(shell, &apply, templates)?;

Ok(Plugin::External(ExternalPlugin {
name,
Expand Down Expand Up @@ -815,15 +815,17 @@ impl RawConfig {
}
}

validate_template_names(&apply, &templates)?;
let shell = shell.unwrap_or_default();

validate_template_names(&shell, &apply, &templates)?;

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

for (name, plugin) in plugins {
normalized_plugins.push(
plugin
.normalize(name.clone(), &templates, &mut warnings)
.normalize(name.clone(), &shell, &templates, &mut warnings)
.with_context(s!("failed to normalize plugin `{}`", name))?,
);
}
Expand Down Expand Up @@ -1099,7 +1101,12 @@ mod tests {
let text = format!("{} = '{}'\n{} = '{}'", a, example_a, b, example_b);
let e = toml::from_str::<RawPlugin>(&text)
.unwrap()
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
.normalize(
"test".to_string(),
&Shell::default(),
&IndexMap::new(),
&mut Vec::new(),
)
.unwrap_err();
assert_eq!(e.to_string(), "plugin `test` has multiple source fields")
}
Expand All @@ -1126,7 +1133,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand Down Expand Up @@ -1156,7 +1163,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand All @@ -1182,7 +1189,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand Down Expand Up @@ -1213,7 +1220,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand Down Expand Up @@ -1242,7 +1249,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand Down Expand Up @@ -1270,7 +1277,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand Down Expand Up @@ -1299,7 +1306,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand All @@ -1324,7 +1331,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand All @@ -1343,7 +1350,12 @@ mod tests {
..Default::default()
};
let error = raw_plugin
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
.normalize(
"test".to_string(),
&Shell::default(),
&IndexMap::new(),
&mut Vec::new(),
)
.unwrap_err();
assert_eq!(
error.to_string(),
Expand All @@ -1364,7 +1376,12 @@ mod tests {
..Default::default()
};
let error = raw_plugin
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
.normalize(
"test".to_string(),
&Shell::default(),
&IndexMap::new(),
&mut Vec::new(),
)
.unwrap_err();
assert_eq!(
error.to_string(),
Expand All @@ -1390,7 +1407,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand All @@ -1409,7 +1426,7 @@ mod tests {
};
assert_eq!(
raw_plugin
.normalize(name, &IndexMap::new(), &mut Vec::new())
.normalize(name, &Shell::default(), &IndexMap::new(), &mut Vec::new())
.unwrap(),
expected
);
Expand All @@ -1423,7 +1440,12 @@ mod tests {
..Default::default()
};
let error = raw_plugin
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
.normalize(
"test".to_string(),
&Shell::default(),
&IndexMap::new(),
&mut Vec::new(),
)
.unwrap_err();
assert_eq!(
error.to_string(),
Expand All @@ -1442,7 +1464,12 @@ mod tests {
..Default::default()
};
let error = raw_plugin
.normalize("test".to_string(), &IndexMap::new(), &mut Vec::new())
.normalize(
"test".to_string(),
&Shell::default(),
&IndexMap::new(),
&mut Vec::new(),
)
.unwrap_err();
assert_eq!(error.to_string(), "unknown template `test`");
}
Expand Down

0 comments on commit 2a60788

Please sign in to comment.