diff --git a/src/main.rs b/src/main.rs index 5e6c3c1d..029354ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,6 @@ use std::borrow::Cow; use std::env; use std::fmt::Debug; use std::io; -#[cfg(windows)] -use std::path::PathBuf; use std::process::exit; fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error> @@ -164,16 +162,10 @@ fn run() -> Result<(), Error> { #[cfg(unix)] execute(&mut report, "nix", || unix::run_nix(run_type), config.no_retry())?; + let emacs = emacs::Emacs::new(&base_dirs); if config.should_run(Step::Emacs) { - #[cfg(unix)] - git_repos.insert(base_dirs.home_dir().join(".emacs.d")); - - #[cfg(windows)] - { - git_repos.insert(base_dirs.data_dir().join(".emacs.d")); - if let Ok(home) = env::var("HOME") { - git_repos.insert(PathBuf::from(home).join(".emacs.d")); - } + if let Some(directory) = emacs.directory() { + git_repos.insert(directory); } } @@ -264,12 +256,7 @@ fn run() -> Result<(), Error> { )?; if config.should_run(Step::Emacs) { - execute( - &mut report, - "Emacs", - || generic::run_emacs(&base_dirs, run_type), - config.no_retry(), - )?; + execute(&mut report, "Emacs", || emacs.upgrade(run_type), config.no_retry())?; } execute( diff --git a/src/steps/emacs.rs b/src/steps/emacs.rs new file mode 100644 index 00000000..de9be7ea --- /dev/null +++ b/src/steps/emacs.rs @@ -0,0 +1,49 @@ +use crate::error::Error; +use crate::executor::RunType; +use crate::terminal::print_separator; +use crate::utils::{require, require_option, PathExt}; +use directories::BaseDirs; +#[cfg(windows)] +use std::env; +use std::path::PathBuf; + +const EMACS_UPGRADE: &str = include_str!("emacs.el"); + +pub struct Emacs { + directory: Option, +} + +impl Emacs { + fn directory_path(base_dirs: &BaseDirs) -> Option { + #[cfg(unix)] + return base_dirs.home_dir().join(".emacs.d").if_exists(); + + #[cfg(windows)] + return env::var("HOME") + .ok() + .and_then(|home| PathBuf::from(home).join(".emacs.d").if_exists()) + .or_else(|| base_dirs.data_dir().join(".emacs.d").if_exists()); + } + + pub fn new(base_dirs: &BaseDirs) -> Self { + Self { + directory: Emacs::directory_path(base_dirs), + } + } + + pub fn directory(&self) -> Option<&PathBuf> { + self.directory.as_ref() + } + + pub fn upgrade(&self, run_type: RunType) -> Result<(), Error> { + let emacs = require("emacs")?; + let init_file = require_option(self.directory.as_ref())?.join("init.el").require()?; + + print_separator("Emacs"); + + run_type + .execute(&emacs) + .args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE]) + .check_run() + } +} diff --git a/src/steps/generic.rs b/src/steps/generic.rs index e44c6ffd..10cf036e 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -7,8 +7,6 @@ use failure::ResultExt; use std::path::PathBuf; use std::process::Command; -const EMACS_UPGRADE: &str = include_str!("emacs.el"); - pub fn run_cargo_update(run_type: RunType) -> Result<(), Error> { let cargo_update = utils::require("cargo-install-update")?; @@ -29,18 +27,6 @@ pub fn run_gem(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> { run_type.execute(&gem).args(&["update", "--user-install"]).check_run() } -pub fn run_emacs(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> { - let emacs = utils::require("emacs")?; - let init_file = base_dirs.home_dir().join(".emacs.d/init.el").require()?; - - print_separator("Emacs"); - - run_type - .execute(&emacs) - .args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE]) - .check_run() -} - #[cfg(not(any( target_os = "freebsd", target_os = "openbsd", diff --git a/src/steps/mod.rs b/src/steps/mod.rs index 789ab6fd..4de7489f 100644 --- a/src/steps/mod.rs +++ b/src/steps/mod.rs @@ -1,3 +1,4 @@ +pub mod emacs; pub mod generic; pub mod git; pub mod node;