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

140 lines
4.1 KiB
Rust
Raw Normal View History

2019-11-12 15:38:50 +02:00
use crate::error::Error;
#[cfg(target_os = "linux")]
use crate::error::ErrorKind;
use crate::executor::{CommandExt, RunType};
2018-12-15 21:52:21 +02:00
use crate::terminal::print_separator;
2019-02-27 10:31:30 +02:00
use crate::utils::{require, 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;
2019-02-27 10:31:30 +02:00
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()?;
2019-04-16 08:37:56 +03:00
print_separator("Fisher");
2019-02-27 10:31:30 +02:00
run_type
.execute(&fish)
.args(&["-c", "fisher self-update"])
.check_run()?;
2019-04-06 09:46:50 +03:00
2019-02-27 10:31:30 +02:00
run_type.execute(&fish).args(&["-c", "fisher"]).check_run()
2018-06-28 12:16:54 +03:00
}
2018-08-19 14:45:23 +03:00
#[must_use]
2019-02-27 10:31:30 +02:00
pub fn run_homebrew(cleanup: bool, run_type: RunType) -> Result<(), Error> {
let brew = require("brew")?;
print_separator("Brew");
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 {
2019-03-12 19:17:21 +02:00
run_type.execute(&brew).args(&["cu", "-ay"]).check_run()?;
2019-02-27 10:31:30 +02:00
} else {
run_type.execute(&brew).args(&["cask", "upgrade"]).check_run()?;
}
2018-08-19 14:45:23 +03:00
2019-02-27 10:31:30 +02:00
if cleanup {
run_type.execute(&brew).arg("cleanup").check_run()?;
2018-08-19 14:45:23 +03:00
}
2019-02-27 10:31:30 +02:00
Ok(())
2018-08-19 14:45:23 +03:00
}
2018-10-21 13:05:49 +03:00
#[must_use]
2019-02-27 10:31:30 +02:00
pub fn run_nix(run_type: RunType) -> Result<(), Error> {
let nix = require("nix")?;
let nix_channel = require("nix-channel")?;
2019-04-06 09:46:59 +03:00
let nix_env = require("nix-env")?;
2019-02-27 10:31:30 +02:00
print_separator("Nix");
#[cfg(target_os = "linux")]
{
use super::linux::Distribution;
use log::debug;
if let Ok(Distribution::NixOS) = Distribution::detect() {
debug!("Nix on NixOS must be upgraded via 'nixos-rebuild switch', skipping.");
return Err(ErrorKind::SkipStep.into());
}
}
2019-02-27 10:31:30 +02:00
run_type.execute(&nix).arg("upgrade-nix").check_run()?;
run_type.execute(&nix_channel).arg("--update").check_run()?;
2019-02-27 10:31:30 +02:00
run_type.execute(&nix_env).arg("--upgrade").check_run()
2018-10-21 13:05:49 +03:00
}
2019-02-19 08:47:01 +02:00
pub fn run_home_manager(run_type: RunType) -> Result<(), Error> {
let home_manager = require("home-manager")?;
print_separator("home-manager");
run_type.execute(&home_manager).arg("switch").check_run()
}
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()
}
pub fn run_sdkman(base_dirs: &BaseDirs, cleanup: bool, run_type: RunType) -> Result<(), Error> {
let bash = require("bash")?;
let sdkman_init_path = env::var("SDKMAN_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| base_dirs.home_dir().join(".sdkman"))
.join("bin")
.join("sdkman-init.sh")
.require()
.map(|p| format!("{}", &p.display()))?;
print_separator("SDKMAN!");
let cmd_selfupdate = format!("source {} && sdk selfupdate", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_selfupdate.as_str()])
.check_run()?;
let cmd_update = format!("source {} && sdk update", &sdkman_init_path);
run_type.execute(&bash).args(&["-c", cmd_update.as_str()]).check_run()?;
let cmd_upgrade = format!("source {} && sdk upgrade", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_upgrade.as_str()])
.check_run()?;
if cleanup {
let cmd_flush_archives = format!("source {} && sdk flush archives", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_flush_archives.as_str()])
.check_run()?;
let cmd_flush_temp = format!("source {} && sdk flush temp", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_flush_temp.as_str()])
.check_run()?;
}
Ok(())
}
2019-06-13 22:05:18 +03:00
pub fn reboot() {
2019-06-16 09:09:05 +03:00
print!("Rebooting...");
Command::new("sudo").arg("reboot").spawn().unwrap().wait().unwrap();
2019-06-13 22:05:18 +03:00
}