Skip to content

Commit

Permalink
Stop defaulting to Zsh and allow unspecified shell
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Aug 25, 2024
1 parent a804ff2 commit 3240afc
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 84 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
- Local plugins.
- Inline plugins.
- Highly configurable install methods using templates.
- Shell agnostic, with sensible defaults for Zsh.
- Shell agnostic, with sensible defaults for Bash or Zsh
- Super-fast plugin loading and parallel installation. See [benchmarks].
- Config file using [TOML](https://toml.io) syntax.
- Clean `~/.zshrc` or `~/.bashrc` (just add 1 line).
- Clean `~/.bashrc` or `~/.zshrc` (just add 1 line).

[benchmarks]: https://github.com/rossmacarthur/zsh-plugin-manager-benchmark

Expand Down
4 changes: 2 additions & 2 deletions docs/README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
- Local plugins.
- Inline plugins.
- Highly configurable install methods using templates.
- Shell agnostic, with sensible defaults for Zsh.
- Shell agnostic, with sensible defaults for Bash or Zsh
- Super-fast plugin loading and parallel installation. See [benchmarks].
- Config file using [TOML](https://toml.io) syntax.
- Clean `~/.zshrc` or `~/.bashrc` (just add 1 line).
- Clean `~/.bashrc` or `~/.zshrc` (just add 1 line).

[benchmarks]: https://github.com/rossmacarthur/zsh-plugin-manager-benchmark

Expand Down
6 changes: 0 additions & 6 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ impl_serialize_as_str! { GitHubRepository }
// Deserialization implementations
////////////////////////////////////////////////////////////////////////////////

impl Default for Shell {
fn default() -> Self {
Self::Zsh
}
}

/// Produced when we fail to parse the shell type.
#[derive(Debug, Error)]
#[error("expected one of `bash` or `zsh`, got `{}`", self.0)]
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use crate::config::profile::MatchesProfile;
#[derive(Debug)]
pub struct Config {
/// What type of shell is being used.
pub shell: Shell,
pub shell: Option<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
56 changes: 35 additions & 21 deletions src/config/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ pub fn normalize(raw_config: RawConfig, warnings: &mut Vec<Error>) -> Result<Con
.with_context(|| format!("failed to compile template `{name}`"))?;
}

let shell = shell.unwrap_or_default();

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

// Normalize the plugins.
Expand Down Expand Up @@ -69,7 +67,7 @@ pub fn normalize(raw_config: RawConfig, warnings: &mut Vec<Error>) -> Result<Con
fn normalize_plugin(
raw_plugin: RawPlugin,
name: String,
shell: Shell,
shell: Option<Shell>,
templates: &IndexMap<String, String>,
warnings: &mut Vec<Error>,
) -> Result<Plugin> {
Expand Down Expand Up @@ -250,14 +248,30 @@ where

/// Check whether the specifed templates actually exist.
fn validate_template_names(
shell: Shell,
shell: Option<Shell>,
apply: &Option<Vec<String>>,
templates: &IndexMap<String, String>,
) -> Result<()> {
if let Some(apply) = apply {
for name in apply {
if !shell.default_templates().contains_key(name) && !templates.contains_key(name) {
bail!("unknown template `{name}`");
if !templates.contains_key(name)
&& !shell
.map(|s| s.default_templates().contains_key(name))
.unwrap_or(false)
{
match shell {
Some(shell) => {
if !shell.default_templates().contains_key(name) {
bail!("unknown template `{name}`");
}
}
None => {
bail!(
"unknown template `{name}` (help: set `shell` to use default \
templates)"
);
}
}
}
}
}
Expand Down Expand Up @@ -297,7 +311,7 @@ mod tests {
let err = normalize_plugin(
raw,
"test".to_string(),
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -330,7 +344,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -368,7 +382,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -399,7 +413,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -437,7 +451,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -471,7 +485,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -504,7 +518,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -538,7 +552,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -568,7 +582,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand All @@ -591,7 +605,7 @@ mod tests {
let err = normalize_plugin(
raw_plugin,
"test".to_string(),
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand All @@ -617,7 +631,7 @@ mod tests {
let err = normalize_plugin(
raw_plugin,
"test".to_string(),
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down Expand Up @@ -649,7 +663,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand All @@ -673,7 +687,7 @@ mod tests {
let plugin = normalize_plugin(
raw_plugin,
name,
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand All @@ -691,7 +705,7 @@ mod tests {
let err = normalize_plugin(
raw_plugin,
"test".to_string(),
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand All @@ -715,7 +729,7 @@ mod tests {
let err = normalize_plugin(
raw_plugin,
"test".to_string(),
Shell::default(),
Some(Shell::Zsh),
&IndexMap::new(),
&mut Vec::new(),
)
Expand Down
23 changes: 12 additions & 11 deletions src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ pub fn config(ctx: &Context, config: Config) -> Result<LockedConfig> {
} = config;

let templates = {
let mut map = shell.default_templates().clone();
let mut map = shell
.map(|s| s.default_templates().clone())
.unwrap_or_default();
for (name, template) in templates {
map.insert(name, template);
}
Expand Down Expand Up @@ -88,8 +90,10 @@ pub fn config(ctx: &Context, config: Config) -> Result<LockedConfig> {

let matches = matches
.as_deref()
.unwrap_or_else(|| shell.default_matches());
let apply = apply.as_ref().unwrap_or_else(|| Shell::default_apply());
.or_else(|| shell.map(|s| s.default_matches()));
let apply = apply
.as_deref()
.or_else(|| shell.map(|s| s.default_apply()));
let count = map.len();
let mut errors = Vec::new();

Expand Down Expand Up @@ -171,7 +175,7 @@ pub fn config(ctx: &Context, config: Config) -> Result<LockedConfig> {

impl Shell {
/// The default files to match on for this shell.
fn default_matches(&self) -> &[String] {
fn default_matches(&self) -> &'static [String] {
static DEFAULT_MATCHES_BASH: Lazy<Vec<String>> = Lazy::new(|| {
vec_into![
"{{ name }}.plugin.bash",
Expand Down Expand Up @@ -225,7 +229,7 @@ impl Shell {
}

/// The default template names to apply.
fn default_apply() -> &'static Vec<String> {
fn default_apply(&self) -> &'static [String] {
static DEFAULT_APPLY: Lazy<Vec<String>> = Lazy::new(|| vec_into!["source"]);
&DEFAULT_APPLY
}
Expand Down Expand Up @@ -316,7 +320,7 @@ mod tests {
let dir = temp.path();
let ctx = Context::testing(dir);
let cfg = Config {
shell: Shell::Zsh,
shell: Some(Shell::Zsh),
matches: None,
apply: None,
templates: IndexMap::new(),
Expand All @@ -327,10 +331,7 @@ mod tests {

assert_eq!(locked.ctx, ctx);
assert_eq!(locked.plugins, Vec::new());
assert_eq!(
locked.templates,
Shell::default().default_templates().clone(),
);
assert_eq!(locked.templates, Shell::Zsh.default_templates().clone(),);
assert_eq!(locked.errors.len(), 0);
}

Expand All @@ -339,7 +340,7 @@ mod tests {
let temp = tempfile::tempdir().expect("create temporary directory");
let ctx = Context::testing(temp.path());
let cfg = Config {
shell: Shell::Zsh,
shell: Some(Shell::Zsh),
matches: None,
apply: None,
templates: IndexMap::new(),
Expand Down
Loading

0 comments on commit 3240afc

Please sign in to comment.