diff --git a/src/main.rs b/src/main.rs index 2d13cc1c..a47f5523 100644 --- a/src/main.rs +++ b/src/main.rs @@ -183,17 +183,19 @@ fn run() -> Result<(), Error> { execute(&mut report, "Scoop", || windows::run_scoop(run_type), config.no_retry())?; #[cfg(unix)] - report.push_result(execute_legacy( + execute( + &mut report, + "brew", || unix::run_homebrew(config.cleanup(), run_type), config.no_retry(), - )?); + )?; #[cfg(target_os = "freebsd")] report.push_result(execute_legacy( || freebsd::upgrade_packages(&sudo, run_type), config.no_retry(), )?); #[cfg(unix)] - report.push_result(execute_legacy(|| unix::run_nix(run_type), config.no_retry())?); + execute(&mut report, "nix", || unix::run_nix(run_type), config.no_retry())?; if config.should_run(Step::Emacs) { #[cfg(unix)] @@ -243,14 +245,18 @@ fn run() -> Result<(), Error> { #[cfg(unix)] { - report.push_result(execute_legacy( + execute( + &mut report, + "zplug", || unix::run_zplug(&base_dirs, run_type), config.no_retry(), - )?); - report.push_result(execute_legacy( + )?; + execute( + &mut report, + "fisher", || unix::run_fisher(&base_dirs, run_type), config.no_retry(), - )?); + )?; report.push_result(execute_legacy( || tmux::run_tpm(&base_dirs, run_type), config.no_retry(), diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index 6b0f4105..e98e7bb4 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -1,116 +1,76 @@ use crate::error::Error; use crate::executor::{CommandExt, RunType}; use crate::terminal::print_separator; -use crate::utils::{require, which, PathExt}; +use crate::utils::{require, PathExt}; use directories::BaseDirs; use std::env; use std::path::{Path, PathBuf}; use std::process::Command; -fn zplug_exists(base_dirs: &BaseDirs) -> bool { +pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> { + let zsh = require("zsh")?; + env::var("ZPLUG_HOME") - .map(|ref zplug_home| Path::new(zplug_home).exists()) - .unwrap_or(false) - || base_dirs.home_dir().join("zplug").exists() -} - -fn get_zshrc(base_dirs: &BaseDirs) -> PathBuf { - env::var("ZDOTDIR") .map(PathBuf::from) + .unwrap_or_else(|_| base_dirs.home_dir().join("zplug")) + .require()?; + + let zshrc = env::var("ZDOTDIR") + .map(|p| Path::new(&p).join(".zshrc")) .unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc")) + .require()?; + + let cmd = format!("source {} && zplug update", zshrc.display()); + run_type.execute(zsh).args(&["-c", cmd.as_str()]).check_run() } -pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(zsh) = which("zsh") { - if zplug_exists(base_dirs) { - print_separator("zplug"); - - let success = || -> Result<(), Error> { - let zshrc = get_zshrc(base_dirs).require()?; - let cmd = format!("source {} && zplug update", zshrc.display()); - run_type.execute(zsh).args(&["-c", cmd.as_str()]).check_run()?; - Ok(()) - }() - .is_ok(); - - return Some(("zplug", success)); - } - } - - None -} - -pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(fish) = which("fish") { - if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() { - print_separator("fisher"); - - let success = || -> Result<(), Error> { - run_type - .execute(&fish) - .args(&["-c", "fisher self-update"]) - .check_run()?; - run_type.execute(&fish).args(&["-c", "fisher"]).check_run()?; - Ok(()) - }() - .is_ok(); - - return Some(("fisher", success)); - } - } - - None +pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> { + let fish = require("fish")?; + base_dirs + .home_dir() + .join(".config/fish/functions/fisher.fish") + .require()?; + run_type + .execute(&fish) + .args(&["-c", "fisher self-update"]) + .check_run()?; + run_type.execute(&fish).args(&["-c", "fisher"]).check_run() } #[must_use] -pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(brew) = which("brew") { - print_separator("Brew"); +pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Result<(), Error> { + let brew = require("brew")?; + print_separator("Brew"); - let inner = || -> Result<(), Error> { - run_type.execute(&brew).arg("update").check_run()?; - run_type.execute(&brew).arg("upgrade").check_run()?; + run_type.execute(&brew).arg("update").check_run()?; + run_type.execute(&brew).arg("upgrade").check_run()?; - let cask_upgrade_exists = Command::new(&brew) - .args(&["--repository", "buo/cask-upgrade"]) - .check_output() - .map(|p| Path::new(p.trim()).exists())?; + let cask_upgrade_exists = Command::new(&brew) + .args(&["--repository", "buo/cask-upgrade"]) + .check_output() + .map(|p| Path::new(p.trim()).exists())?; - if cask_upgrade_exists { - run_type.execute(&brew).args(&["cu", "-a"]).check_run()?; - } else { - run_type.execute(&brew).args(&["cask", "upgrade"]).check_run()?; - } - - if cleanup { - run_type.execute(&brew).arg("cleanup").check_run()?; - } - Ok(()) - }; - - return Some(("Brew", inner().is_ok())); + if cask_upgrade_exists { + run_type.execute(&brew).args(&["cu", "-a"]).check_run()?; + } else { + run_type.execute(&brew).args(&["cask", "upgrade"]).check_run()?; } - None + if cleanup { + run_type.execute(&brew).arg("cleanup").check_run()?; + } + + Ok(()) } #[must_use] -pub fn run_nix(run_type: RunType) -> Option<(&'static str, bool)> { - if let Some(nix) = which("nix") { - if let Some(nix_env) = which("nix-env") { - print_separator("Nix"); +pub fn run_nix(run_type: RunType) -> Result<(), Error> { + let nix = require("nix")?; + let nix_env = require("nix_env")?; - let inner = || -> Result<(), Error> { - run_type.execute(&nix).arg("upgrade-nix").check_run()?; - run_type.execute(&nix_env).arg("--upgrade").check_run()?; - Ok(()) - }; - - return Some(("Nix", inner().is_ok())); - } - } - - None + print_separator("Nix"); + run_type.execute(&nix).arg("upgrade-nix").check_run()?; + run_type.execute(&nix_env).arg("--upgrade").check_run() } pub fn run_pearl(run_type: RunType) -> Result<(), Error> {