From 2d2804bf03922da9ba113e49fcdb3d087c9f95b5 Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Tue, 26 Feb 2019 02:34:51 -0800 Subject: [PATCH 1/5] fix(zplug): search appropriate paths for zplug folder Check ZPLUG_HOME environment variable in addition to ~/.zplug. Also consider ZDOTDIR when sourcing .zshrc. --- src/steps/os/unix.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index a61c8ecc..895e8baa 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -1,20 +1,44 @@ -use crate::error::Error; +use crate::error::{Error, ErrorKind::*}; use crate::executor::{CommandExt, RunType}; use crate::terminal::print_separator; use crate::utils::{require, which}; use directories::BaseDirs; use std::path::Path; use std::process::Command; +use std::env; + +fn zplug_exists(base_dirs: &BaseDirs) -> bool { + let home_exists = match env::var("ZPLUG_HOME") { + Ok(ref zplug_home) => Path::new(zplug_home).exists(), + Err(_) => false, + }; + let dotdir_exists = base_dirs.home_dir().join(".zplug").exists(); + home_exists || dotdir_exists +} + +fn get_zshrc(base_dirs: &BaseDirs) -> Result { + let zshrc = match env::var("ZDOTDIR") { + Ok(ref zdotdir) => Ok(Path::new(zdotdir).join(".zshrc")), + Err(_) => Err(()), + }; + zshrc + .unwrap_or(base_dirs.home_dir().join(".zshrc")) + .to_str() + .map(|s| s.to_owned()) + .ok_or(()) +} pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { if let Some(zsh) = which("zsh") { - if base_dirs.home_dir().join(".zplug").exists() { + if zplug_exists(base_dirs) { print_separator("zplug"); let success = || -> Result<(), Error> { + let zshrc = get_zshrc(base_dirs).map_err(|_| Error::from(SkipStep))?; + let cmd = format!("source {} && zplug update", zshrc); run_type .execute(zsh) - .args(&["-c", "source ~/.zshrc && zplug update"]) + .args(&["-c", cmd.as_str()]) .check_run()?; Ok(()) }() From 4d98fe86e251feac420caf47a404e128af42f62f Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Tue, 26 Feb 2019 02:40:51 -0800 Subject: [PATCH 2/5] style: make rustfmt compliant --- src/steps/os/unix.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index 895e8baa..6cf6eba2 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -3,16 +3,16 @@ use crate::executor::{CommandExt, RunType}; use crate::terminal::print_separator; use crate::utils::{require, which}; use directories::BaseDirs; +use std::env; use std::path::Path; use std::process::Command; -use std::env; fn zplug_exists(base_dirs: &BaseDirs) -> bool { let home_exists = match env::var("ZPLUG_HOME") { Ok(ref zplug_home) => Path::new(zplug_home).exists(), Err(_) => false, }; - let dotdir_exists = base_dirs.home_dir().join(".zplug").exists(); + let dotdir_exists = base_dirs.home_dir().join(".zplug").exists(); home_exists || dotdir_exists } @@ -36,10 +36,7 @@ pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static st let success = || -> Result<(), Error> { let zshrc = get_zshrc(base_dirs).map_err(|_| Error::from(SkipStep))?; let cmd = format!("source {} && zplug update", zshrc); - run_type - .execute(zsh) - .args(&["-c", cmd.as_str()]) - .check_run()?; + run_type.execute(zsh).args(&["-c", cmd.as_str()]).check_run()?; Ok(()) }() .is_ok(); From 6003ffc83c84d1f24e1532f982df34cde15adb05 Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Tue, 26 Feb 2019 02:47:14 -0800 Subject: [PATCH 3/5] style: make clippy compliant --- src/steps/os/unix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index 6cf6eba2..8afaca29 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -22,7 +22,7 @@ fn get_zshrc(base_dirs: &BaseDirs) -> Result { Err(_) => Err(()), }; zshrc - .unwrap_or(base_dirs.home_dir().join(".zshrc")) + .unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc")) .to_str() .map(|s| s.to_owned()) .ok_or(()) From 79cb5a52843cf3365d626553fb81f18ffee5d8e2 Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Tue, 26 Feb 2019 03:14:40 -0800 Subject: [PATCH 4/5] refactor(zplug): remove match expressions --- src/steps/os/unix.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index 8afaca29..a38fd47c 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -8,20 +8,15 @@ use std::path::Path; use std::process::Command; fn zplug_exists(base_dirs: &BaseDirs) -> bool { - let home_exists = match env::var("ZPLUG_HOME") { - Ok(ref zplug_home) => Path::new(zplug_home).exists(), - Err(_) => false, - }; - let dotdir_exists = base_dirs.home_dir().join(".zplug").exists(); - home_exists || dotdir_exists + env::var("ZPLUG_HOME") + .map(|ref zplug_home| Path::new(zplug_home).exists()) + .map(|p| p || base_dirs.home_dir().join("zplug").exists()) + .unwrap_or(false) } fn get_zshrc(base_dirs: &BaseDirs) -> Result { - let zshrc = match env::var("ZDOTDIR") { - Ok(ref zdotdir) => Ok(Path::new(zdotdir).join(".zshrc")), - Err(_) => Err(()), - }; - zshrc + env::var("ZDOTDIR") + .map(|ref zdotdir| Path::new(zdotdir).join(".zshrc")) .unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc")) .to_str() .map(|s| s.to_owned()) From d0248385d5faeee6eb4aacfb20eece2889f45a6a Mon Sep 17 00:00:00 2001 From: Eric Mark Martin Date: Tue, 26 Feb 2019 11:53:22 -0800 Subject: [PATCH 5/5] refactor(zplug): return PathBuf to .zshrc file --- src/steps/os/unix.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index a38fd47c..212c1839 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -4,22 +4,22 @@ use crate::terminal::print_separator; use crate::utils::{require, which}; use directories::BaseDirs; use std::env; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::Command; fn zplug_exists(base_dirs: &BaseDirs) -> bool { env::var("ZPLUG_HOME") .map(|ref zplug_home| Path::new(zplug_home).exists()) - .map(|p| p || base_dirs.home_dir().join("zplug").exists()) .unwrap_or(false) + || base_dirs.home_dir().join("zplug").exists() } -fn get_zshrc(base_dirs: &BaseDirs) -> Result { +fn get_zshrc(base_dirs: &BaseDirs) -> Result { env::var("ZDOTDIR") .map(|ref zdotdir| Path::new(zdotdir).join(".zshrc")) .unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc")) .to_str() - .map(|s| s.to_owned()) + .map(PathBuf::from) .ok_or(()) } @@ -30,7 +30,7 @@ pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static st let success = || -> Result<(), Error> { let zshrc = get_zshrc(base_dirs).map_err(|_| Error::from(SkipStep))?; - let cmd = format!("source {} && zplug update", zshrc); + let cmd = format!("source {} && zplug update", zshrc.display()); run_type.execute(zsh).args(&["-c", cmd.as_str()]).check_run()?; Ok(()) }()