Files
topgrade/src/steps/os/unix.rs

122 lines
3.5 KiB
Rust
Raw Normal View History

2019-02-27 10:06:54 +02:00
use crate::error::Error;
use crate::executor::{CommandExt, RunType};
2018-12-15 21:52:21 +02:00
use crate::terminal::print_separator;
2019-02-27 10:06:54 +02:00
use crate::utils::{require, which, PathExt};
2018-07-07 09:18:53 +03:00
use directories::BaseDirs;
2019-02-26 02:40:51 -08:00
use std::env;
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())
.unwrap_or(false)
|| base_dirs.home_dir().join("zplug").exists()
}
2019-02-27 10:06:54 +02:00
fn get_zshrc(base_dirs: &BaseDirs) -> PathBuf {
env::var("ZDOTDIR")
.map(PathBuf::from)
2019-02-27 10:06:54 +02:00
.unwrap_or_else(|_| base_dirs.home_dir().join(".zshrc"))
}
2018-06-28 12:16:54 +03: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") {
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-27 10:06:54 +02:00
let zshrc = get_zshrc(base_dirs).require()?;
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
}
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> {
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]
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()?;
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 {
2018-12-31 22:00:34 +02:00
run_type.execute(&brew).arg("cleanup").check_run()?;
}
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]
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()
}