2019-02-26 02:34:51 -08:00
|
|
|
use crate::error::{Error, ErrorKind::*};
|
2019-02-03 10:12:54 +02:00
|
|
|
use crate::executor::{CommandExt, RunType};
|
2018-12-15 21:52:21 +02:00
|
|
|
use crate::terminal::print_separator;
|
2019-02-19 08:47:01 +02:00
|
|
|
use crate::utils::{require, which};
|
2018-07-07 09:18:53 +03:00
|
|
|
use directories::BaseDirs;
|
2019-02-26 02:40:51 -08:00
|
|
|
use std::env;
|
2019-02-26 11:53:22 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-02-03 10:12:54 +02:00
|
|
|
use std::process::Command;
|
2019-02-26 02:34:51 -08:00
|
|
|
|
|
|
|
|
fn zplug_exists(base_dirs: &BaseDirs) -> bool {
|
2019-02-26 03:14:40 -08:00
|
|
|
env::var("ZPLUG_HOME")
|
|
|
|
|
.map(|ref zplug_home| Path::new(zplug_home).exists())
|
|
|
|
|
.unwrap_or(false)
|
2019-02-26 11:53:22 -08:00
|
|
|
|| base_dirs.home_dir().join("zplug").exists()
|
2019-02-26 02:34:51 -08:00
|
|
|
}
|
|
|
|
|
|
2019-02-26 11:53:22 -08:00
|
|
|
fn get_zshrc(base_dirs: &BaseDirs) -> Result<PathBuf, ()> {
|
2019-02-26 03:14:40 -08:00
|
|
|
env::var("ZDOTDIR")
|
|
|
|
|
.map(|ref zdotdir| Path::new(zdotdir).join(".zshrc"))
|
2019-02-26 02:47:14 -08:00
|
|
|
.unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc"))
|
2019-02-26 02:34:51 -08:00
|
|
|
.to_str()
|
2019-02-26 11:53:22 -08:00
|
|
|
.map(PathBuf::from)
|
2019-02-26 02:34:51 -08:00
|
|
|
.ok_or(())
|
|
|
|
|
}
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn run_zplug(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-08-19 14:45:23 +03:00
|
|
|
if let Some(zsh) = which("zsh") {
|
2019-02-26 02:34:51 -08:00
|
|
|
if zplug_exists(base_dirs) {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("zplug");
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2018-08-19 14:45:23 +03:00
|
|
|
let success = || -> Result<(), Error> {
|
2019-02-26 02:34:51 -08:00
|
|
|
let zshrc = get_zshrc(base_dirs).map_err(|_| Error::from(SkipStep))?;
|
2019-02-26 11:53:22 -08:00
|
|
|
let cmd = format!("source {} && zplug update", zshrc.display());
|
2019-02-26 02:40:51 -08:00
|
|
|
run_type.execute(zsh).args(&["-c", cmd.as_str()]).check_run()?;
|
2018-08-19 14:45:23 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2018-08-19 14:45:23 +03:00
|
|
|
return Some(("zplug", success));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-03 14:31:25 +03:00
|
|
|
|
2018-08-19 14:45:23 +03:00
|
|
|
None
|
2018-07-03 14:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-08-26 16:53:19 +03:00
|
|
|
if let Some(fish) = which("fish") {
|
2018-08-19 14:45:23 +03:00
|
|
|
if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("fisher");
|
2018-08-19 14:45:23 +03:00
|
|
|
|
|
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 13:26:17 +02:00
|
|
|
run_type
|
|
|
|
|
.execute(&fish)
|
2018-10-07 11:51:12 +03:00
|
|
|
.args(&["-c", "fisher self-update"])
|
2018-12-31 22:00:34 +02:00
|
|
|
.check_run()?;
|
|
|
|
|
run_type.execute(&fish).args(&["-c", "fisher"]).check_run()?;
|
2018-08-19 14:45:23 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-08-19 14:45:23 +03:00
|
|
|
|
2018-10-07 11:51:12 +03:00
|
|
|
return Some(("fisher", success));
|
2018-08-19 14:45:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2018-08-19 14:45:23 +03:00
|
|
|
None
|
2018-06-28 12:16:54 +03:00
|
|
|
}
|
|
|
|
|
|
2018-08-19 14:45:23 +03:00
|
|
|
#[must_use]
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-08-19 14:45:23 +03:00
|
|
|
if let Some(brew) = which("brew") {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("Brew");
|
2018-08-19 14:45:23 +03:00
|
|
|
|
|
|
|
|
let inner = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&brew).arg("update").check_run()?;
|
|
|
|
|
run_type.execute(&brew).arg("upgrade").check_run()?;
|
2019-02-03 10:12:54 +02:00
|
|
|
|
|
|
|
|
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()?;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 19:09:46 +01:00
|
|
|
if cleanup {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&brew).arg("cleanup").check_run()?;
|
2018-11-17 19:09:46 +01:00
|
|
|
}
|
2018-08-19 14:45:23 +03:00
|
|
|
Ok(())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Some(("Brew", inner().is_ok()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
2018-10-21 13:05:49 +03:00
|
|
|
|
|
|
|
|
#[must_use]
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn run_nix(run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-10-21 13:05:49 +03:00
|
|
|
if let Some(nix) = which("nix") {
|
|
|
|
|
if let Some(nix_env) = which("nix-env") {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("Nix");
|
2018-10-21 13:05:49 +03:00
|
|
|
|
|
|
|
|
let inner = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&nix).arg("upgrade-nix").check_run()?;
|
|
|
|
|
run_type.execute(&nix_env).arg("--upgrade").check_run()?;
|
2018-10-21 13:05:49 +03:00
|
|
|
Ok(())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Some(("Nix", inner().is_ok()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
2019-02-19 08:47:01 +02:00
|
|
|
|
|
|
|
|
pub fn run_pearl(run_type: RunType) -> Result<(), Error> {
|
|
|
|
|
let pearl = require("pearl")?;
|
|
|
|
|
print_separator("pearl");
|
|
|
|
|
|
|
|
|
|
run_type.execute(&pearl).arg("update").check_run()
|
|
|
|
|
}
|