Refactor unix steps

This commit is contained in:
Roey Darwish Dror
2019-02-27 10:31:30 +02:00
parent 32a151227a
commit 78652d68d4
2 changed files with 61 additions and 95 deletions

View File

@@ -183,17 +183,19 @@ fn run() -> Result<(), Error> {
execute(&mut report, "Scoop", || windows::run_scoop(run_type), config.no_retry())?; execute(&mut report, "Scoop", || windows::run_scoop(run_type), config.no_retry())?;
#[cfg(unix)] #[cfg(unix)]
report.push_result(execute_legacy( execute(
&mut report,
"brew",
|| unix::run_homebrew(config.cleanup(), run_type), || unix::run_homebrew(config.cleanup(), run_type),
config.no_retry(), config.no_retry(),
)?); )?;
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
report.push_result(execute_legacy( report.push_result(execute_legacy(
|| freebsd::upgrade_packages(&sudo, run_type), || freebsd::upgrade_packages(&sudo, run_type),
config.no_retry(), config.no_retry(),
)?); )?);
#[cfg(unix)] #[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) { if config.should_run(Step::Emacs) {
#[cfg(unix)] #[cfg(unix)]
@@ -243,14 +245,18 @@ fn run() -> Result<(), Error> {
#[cfg(unix)] #[cfg(unix)]
{ {
report.push_result(execute_legacy( execute(
&mut report,
"zplug",
|| unix::run_zplug(&base_dirs, run_type), || unix::run_zplug(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
report.push_result(execute_legacy( execute(
&mut report,
"fisher",
|| unix::run_fisher(&base_dirs, run_type), || unix::run_fisher(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
report.push_result(execute_legacy( report.push_result(execute_legacy(
|| tmux::run_tpm(&base_dirs, run_type), || tmux::run_tpm(&base_dirs, run_type),
config.no_retry(), config.no_retry(),

View File

@@ -1,73 +1,47 @@
use crate::error::Error; use crate::error::Error;
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{require, which, PathExt}; use crate::utils::{require, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; 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") 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) .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")) .unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc"))
} .require()?;
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()); let cmd = format!("source {} && zplug update", zshrc.display());
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();
return Some(("zplug", success));
}
}
None
} }
pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
if let Some(fish) = which("fish") { let fish = require("fish")?;
if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() { base_dirs
print_separator("fisher"); .home_dir()
.join(".config/fish/functions/fisher.fish")
let success = || -> Result<(), Error> { .require()?;
run_type run_type
.execute(&fish) .execute(&fish)
.args(&["-c", "fisher self-update"]) .args(&["-c", "fisher self-update"])
.check_run()?; .check_run()?;
run_type.execute(&fish).args(&["-c", "fisher"]).check_run()?; run_type.execute(&fish).args(&["-c", "fisher"]).check_run()
Ok(())
}()
.is_ok();
return Some(("fisher", success));
}
}
None
} }
#[must_use] #[must_use]
pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Option<(&'static str, bool)> { pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Result<(), Error> {
if let Some(brew) = which("brew") { let brew = require("brew")?;
print_separator("Brew"); print_separator("Brew");
let inner = || -> Result<(), Error> {
run_type.execute(&brew).arg("update").check_run()?; run_type.execute(&brew).arg("update").check_run()?;
run_type.execute(&brew).arg("upgrade").check_run()?; run_type.execute(&brew).arg("upgrade").check_run()?;
@@ -85,32 +59,18 @@ pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Option<(&'static str, b
if cleanup { if cleanup {
run_type.execute(&brew).arg("cleanup").check_run()?; run_type.execute(&brew).arg("cleanup").check_run()?;
} }
Ok(()) Ok(())
};
return Some(("Brew", inner().is_ok()));
}
None
} }
#[must_use] #[must_use]
pub fn run_nix(run_type: RunType) -> Option<(&'static str, bool)> { pub fn run_nix(run_type: RunType) -> Result<(), Error> {
if let Some(nix) = which("nix") { let nix = require("nix")?;
if let Some(nix_env) = which("nix-env") { let nix_env = require("nix_env")?;
print_separator("Nix"); print_separator("Nix");
let inner = || -> Result<(), Error> {
run_type.execute(&nix).arg("upgrade-nix").check_run()?; run_type.execute(&nix).arg("upgrade-nix").check_run()?;
run_type.execute(&nix_env).arg("--upgrade").check_run()?; run_type.execute(&nix_env).arg("--upgrade").check_run()
Ok(())
};
return Some(("Nix", inner().is_ok()));
}
}
None
} }
pub fn run_pearl(run_type: RunType) -> Result<(), Error> { pub fn run_pearl(run_type: RunType) -> Result<(), Error> {