2019-11-12 15:38:50 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
2019-12-11 23:05:38 +02:00
|
|
|
use crate::error::SkipStep;
|
2020-03-17 22:49:07 +02:00
|
|
|
use crate::error::TopgradeError;
|
2020-02-08 22:17:29 +02:00
|
|
|
use crate::execution_context::ExecutionContext;
|
2020-12-30 10:35:50 +02:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
use crate::executor::CommandExt;
|
|
|
|
|
use crate::executor::{Executor, ExecutorExitStatus, RunType};
|
2020-02-08 22:17:29 +02:00
|
|
|
use crate::terminal::{print_separator, print_warning};
|
2019-02-27 10:31:30 +02:00
|
|
|
use crate::utils::{require, PathExt};
|
2019-12-11 23:05:38 +02:00
|
|
|
use anyhow::Result;
|
2018-07-07 09:18:53 +03:00
|
|
|
use directories::BaseDirs;
|
2020-02-08 22:17:29 +02:00
|
|
|
use log::debug;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
2020-07-10 11:22:29 +03:00
|
|
|
use std::path::PathBuf;
|
2019-02-03 10:12:54 +02:00
|
|
|
use std::process::Command;
|
2020-10-01 14:31:36 +03:00
|
|
|
use std::{env, path::Path};
|
2019-02-26 02:34:51 -08:00
|
|
|
|
2021-02-08 11:33:06 +02:00
|
|
|
const INTEL_BREW: &str = "/usr/local/bin/brew";
|
|
|
|
|
const ARM_BREW: &str = "/opt/homebrew/bin/brew";
|
2021-10-25 21:31:13 +03:00
|
|
|
|
2020-12-16 13:43:38 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
pub enum BrewVariant {
|
|
|
|
|
Linux,
|
|
|
|
|
MacIntel,
|
|
|
|
|
MacArm,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BrewVariant {
|
|
|
|
|
fn binary_name(self) -> &'static str {
|
|
|
|
|
match self {
|
2021-02-08 11:33:06 +02:00
|
|
|
BrewVariant::Linux => "brew",
|
|
|
|
|
BrewVariant::MacIntel => INTEL_BREW,
|
|
|
|
|
BrewVariant::MacArm => ARM_BREW,
|
2020-12-16 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn both_both_exist() -> bool {
|
2021-02-08 11:33:06 +02:00
|
|
|
Path::new(INTEL_BREW).exists() && Path::new(ARM_BREW).exists()
|
2020-12-16 13:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn step_title(self) -> &'static str {
|
|
|
|
|
let both_exists = Self::both_both_exist();
|
|
|
|
|
match self {
|
|
|
|
|
BrewVariant::MacArm if both_exists => "Brew (ARM)",
|
|
|
|
|
BrewVariant::MacIntel if both_exists => "Brew (Intel)",
|
|
|
|
|
_ => "Brew",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn execute(self, run_type: RunType) -> Executor {
|
|
|
|
|
match self {
|
|
|
|
|
BrewVariant::MacIntel if cfg!(target_arch = "aarch64") => {
|
|
|
|
|
let mut command = run_type.execute("arch");
|
|
|
|
|
command.arg("-x86_64").arg(self.binary_name());
|
|
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
BrewVariant::MacArm if cfg!(target_arch = "x86_64") => {
|
|
|
|
|
let mut command = run_type.execute("arch");
|
|
|
|
|
command.arg("-arm64e").arg(self.binary_name());
|
|
|
|
|
command
|
|
|
|
|
}
|
|
|
|
|
_ => run_type.execute(self.binary_name()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
|
2019-02-27 10:31:30 +02:00
|
|
|
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-04-06 09:46:50 +03:00
|
|
|
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&fish).args(["-c", "fisher update"]).check_run()
|
2018-06-28 12:16:54 +03:00
|
|
|
}
|
|
|
|
|
|
2021-02-10 08:58:03 +02:00
|
|
|
pub fn run_bashit(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
ctx.base_dirs().home_dir().join(".bash_it").require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("Bash-it");
|
|
|
|
|
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute("bash")
|
2021-09-04 20:01:19 +02:00
|
|
|
.args(["-lic", &format!("bash-it update {}", ctx.config().bashit_branch())])
|
2021-02-10 08:58:03 +02:00
|
|
|
.check_run()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 09:32:45 +03:00
|
|
|
pub fn run_oh_my_fish(ctx: &ExecutionContext) -> Result<()> {
|
2020-09-24 13:21:09 +08:00
|
|
|
let fish = require("fish")?;
|
|
|
|
|
ctx.base_dirs()
|
|
|
|
|
.home_dir()
|
|
|
|
|
.join(".local/share/omf/pkg/omf/functions/omf.fish")
|
|
|
|
|
.require()?;
|
2020-09-03 09:32:45 +03:00
|
|
|
|
|
|
|
|
print_separator("oh-my-fish");
|
2020-09-24 13:21:09 +08:00
|
|
|
|
2021-09-04 20:01:19 +02:00
|
|
|
ctx.run_type().execute(&fish).args(["-c", "omf update"]).check_run()
|
2020-09-03 09:32:45 +03:00
|
|
|
}
|
|
|
|
|
|
2021-09-02 06:14:56 +03:00
|
|
|
pub fn run_pkgin(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let pkgin = require("pkgin")?;
|
|
|
|
|
|
|
|
|
|
let mut command = ctx.run_type().execute(ctx.sudo().as_ref().unwrap());
|
|
|
|
|
command.arg(&pkgin).arg("update");
|
|
|
|
|
if ctx.config().yes() {
|
|
|
|
|
command.arg("-y");
|
|
|
|
|
}
|
|
|
|
|
command.check_run()?;
|
|
|
|
|
|
|
|
|
|
let mut command = ctx.run_type().execute(ctx.sudo().as_ref().unwrap());
|
|
|
|
|
command.arg(&pkgin).arg("upgrade");
|
|
|
|
|
if ctx.config().yes() {
|
|
|
|
|
command.arg("-y");
|
|
|
|
|
}
|
|
|
|
|
command.check_run()
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 04:14:14 -05:00
|
|
|
pub fn run_fish_plug(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let fish = require("fish")?;
|
|
|
|
|
ctx.base_dirs()
|
|
|
|
|
.home_dir()
|
|
|
|
|
.join(".local/share/fish/plug/kidonng/fish-plug/functions/plug.fish")
|
|
|
|
|
.require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("fish-plug");
|
|
|
|
|
|
2021-09-04 20:01:19 +02:00
|
|
|
ctx.run_type().execute(&fish).args(["-c", "plug update"]).check_run()
|
2021-04-12 04:14:14 -05:00
|
|
|
}
|
|
|
|
|
|
2021-10-25 21:31:13 +03:00
|
|
|
pub fn upgrade_gnome_extensions(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let gdbus = require("gdbus")?;
|
|
|
|
|
print_separator("Gnome Shell extensions");
|
|
|
|
|
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(gdbus)
|
|
|
|
|
.args(&[
|
|
|
|
|
"call",
|
|
|
|
|
"--session",
|
|
|
|
|
"--dest",
|
2021-10-26 08:29:55 +02:00
|
|
|
"org.gnome.Shell.Extensions",
|
2021-10-25 21:31:13 +03:00
|
|
|
"--object-path",
|
2021-10-28 18:45:53 +02:00
|
|
|
"/org/gnome/Shell/Extensions",
|
2021-10-26 08:29:55 +02:00
|
|
|
"--method",
|
2021-10-25 21:31:13 +03:00
|
|
|
"org.gnome.Shell.Extensions.CheckForUpdates",
|
|
|
|
|
])
|
|
|
|
|
.check_run()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-30 10:35:50 +02:00
|
|
|
pub fn run_brew_formula(ctx: &ExecutionContext, variant: BrewVariant) -> Result<()> {
|
2020-12-16 13:43:38 +02:00
|
|
|
require(variant.binary_name())?;
|
|
|
|
|
print_separator(variant.step_title());
|
2020-05-12 09:07:54 +03:00
|
|
|
let run_type = ctx.run_type();
|
2019-02-27 10:31:30 +02:00
|
|
|
|
2020-12-30 10:35:50 +02:00
|
|
|
variant.execute(run_type).arg("update").check_run()?;
|
|
|
|
|
variant
|
|
|
|
|
.execute(run_type)
|
2021-09-04 20:01:19 +02:00
|
|
|
.args(["upgrade", "--ignore-pinned", "--formula"])
|
2020-12-30 10:35:50 +02:00
|
|
|
.check_run()?;
|
|
|
|
|
|
|
|
|
|
if ctx.config().cleanup() {
|
|
|
|
|
variant.execute(run_type).arg("cleanup").check_run()?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
pub fn run_brew_cask(ctx: &ExecutionContext, variant: BrewVariant) -> Result<()> {
|
|
|
|
|
require(variant.binary_name())?;
|
|
|
|
|
print_separator(format!("{} - Cask", variant.step_title()));
|
|
|
|
|
let run_type = ctx.run_type();
|
|
|
|
|
|
2020-12-16 13:43:38 +02:00
|
|
|
let cask_upgrade_exists = variant
|
|
|
|
|
.execute(RunType::Wet)
|
2021-09-04 20:01:19 +02:00
|
|
|
.args(["--repository", "buo/cask-upgrade"])
|
2020-10-01 14:31:36 +03:00
|
|
|
.check_output()
|
|
|
|
|
.map(|p| Path::new(p.trim()).exists())?;
|
|
|
|
|
|
2020-12-30 10:35:50 +02:00
|
|
|
let mut brew_args = vec![];
|
2020-10-01 14:31:36 +03:00
|
|
|
|
|
|
|
|
if cask_upgrade_exists {
|
2021-09-04 20:01:19 +02:00
|
|
|
brew_args.extend(["cu", "-y"]);
|
2020-10-01 14:31:36 +03:00
|
|
|
if ctx.config().brew_cask_greedy() {
|
2020-12-30 10:35:50 +02:00
|
|
|
brew_args.push("-a");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-09-04 20:01:19 +02:00
|
|
|
brew_args.extend(["upgrade", "--cask"]);
|
2020-12-30 10:35:50 +02:00
|
|
|
if ctx.config().brew_cask_greedy() {
|
|
|
|
|
brew_args.push("--greedy");
|
2020-10-01 14:31:36 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-30 10:35:50 +02:00
|
|
|
variant.execute(run_type).args(&brew_args).check_run()?;
|
|
|
|
|
|
2020-10-01 14:31:36 +03:00
|
|
|
if ctx.config().cleanup() {
|
2020-12-16 13:43:38 +02:00
|
|
|
variant.execute(run_type).arg("cleanup").check_run()?;
|
2020-10-01 14:31:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2020-07-02 08:37:18 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-08 22:17:29 +02:00
|
|
|
pub fn run_nix(ctx: &ExecutionContext) -> Result<()> {
|
2019-02-27 10:31:30 +02:00
|
|
|
let nix = require("nix")?;
|
2019-11-11 22:16:40 +02:00
|
|
|
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");
|
2019-11-11 22:16:40 +02:00
|
|
|
|
2020-02-08 22:17:29 +02:00
|
|
|
let multi_user = fs::metadata(&nix)?.uid() == 0;
|
|
|
|
|
debug!("Multi user nix: {}", multi_user);
|
|
|
|
|
|
2019-11-11 22:16:40 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
use super::linux::Distribution;
|
|
|
|
|
|
|
|
|
|
if let Ok(Distribution::NixOS) = Distribution::detect() {
|
2020-08-21 23:04:36 +03:00
|
|
|
return Err(SkipStep(String::from("Nix on NixOS must be upgraded via nixos-rebuild switch")).into());
|
2019-11-11 22:16:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 22:17:29 +02:00
|
|
|
let run_type = ctx.run_type();
|
|
|
|
|
|
|
|
|
|
if multi_user {
|
|
|
|
|
if let Some(sudo) = ctx.sudo() {
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&sudo).args(["-i", "nix", "upgrade-nix"]).check_run()?;
|
2020-02-08 22:17:29 +02:00
|
|
|
} else {
|
|
|
|
|
print_warning("Need sudo to upgrade Nix");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
run_type.execute(&nix).arg("upgrade-nix").check_run()?;
|
|
|
|
|
}
|
2019-11-11 22:16:40 +02:00
|
|
|
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
|
|
|
|
2020-07-11 08:00:35 +03:00
|
|
|
pub fn run_yadm(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let yadm = require("yadm")?;
|
|
|
|
|
|
|
|
|
|
print_separator("yadm");
|
|
|
|
|
|
|
|
|
|
ctx.run_type().execute(&yadm).arg("pull").check_run()
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 09:07:03 +02:00
|
|
|
pub fn run_asdf(run_type: RunType) -> Result<()> {
|
|
|
|
|
let asdf = require("asdf")?;
|
|
|
|
|
|
|
|
|
|
print_separator("asdf");
|
2020-03-17 22:49:07 +02:00
|
|
|
let exit_status = run_type.execute(&asdf).arg("update").spawn()?.wait()?;
|
|
|
|
|
|
|
|
|
|
if let ExecutorExitStatus::Wet(e) = exit_status {
|
|
|
|
|
if !(e.success() || e.code().map(|c| c == 42).unwrap_or(false)) {
|
|
|
|
|
return Err(TopgradeError::ProcessFailed(e).into());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&asdf).args(["plugin", "update", "--all"]).check_run()
|
2020-02-29 09:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn run_home_manager(run_type: RunType) -> Result<()> {
|
2019-11-11 22:16:40 +02:00
|
|
|
let home_manager = require("home-manager")?;
|
|
|
|
|
|
|
|
|
|
print_separator("home-manager");
|
|
|
|
|
run_type.execute(&home_manager).arg("switch").check_run()
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 22:56:18 +02:00
|
|
|
pub fn run_tldr(run_type: RunType) -> Result<()> {
|
|
|
|
|
let tldr = require("tldr")?;
|
|
|
|
|
|
|
|
|
|
print_separator("TLDR");
|
2020-01-12 19:37:19 +01:00
|
|
|
run_type.execute(&tldr).arg("--update").check_run()
|
2020-01-05 22:56:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn run_pearl(run_type: RunType) -> Result<()> {
|
2019-02-19 08:47:01 +02:00
|
|
|
let pearl = require("pearl")?;
|
|
|
|
|
print_separator("pearl");
|
|
|
|
|
|
|
|
|
|
run_type.execute(&pearl).arg("update").check_run()
|
|
|
|
|
}
|
2019-06-13 12:19:47 +02:00
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn run_sdkman(base_dirs: &BaseDirs, cleanup: bool, run_type: RunType) -> Result<()> {
|
2019-06-13 13:47:12 +02:00
|
|
|
let bash = require("bash")?;
|
2019-06-13 12:19:47 +02:00
|
|
|
|
|
|
|
|
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);
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&bash).args(["-c", &cmd_selfupdate]).check_run()?;
|
2019-06-13 12:19:47 +02:00
|
|
|
|
|
|
|
|
let cmd_update = format!("source {} && sdk update", &sdkman_init_path);
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&bash).args(["-c", &cmd_update]).check_run()?;
|
2019-06-13 12:19:47 +02:00
|
|
|
|
|
|
|
|
let cmd_upgrade = format!("source {} && sdk upgrade", &sdkman_init_path);
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&bash).args(["-c", &cmd_upgrade]).check_run()?;
|
2019-06-13 12:19:47 +02:00
|
|
|
|
|
|
|
|
if cleanup {
|
|
|
|
|
let cmd_flush_archives = format!("source {} && sdk flush archives", &sdkman_init_path);
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&bash).args(["-c", &cmd_flush_archives]).check_run()?;
|
2019-06-13 12:19:47 +02:00
|
|
|
|
|
|
|
|
let cmd_flush_temp = format!("source {} && sdk flush temp", &sdkman_init_path);
|
2021-09-04 20:01:19 +02:00
|
|
|
run_type.execute(&bash).args(["-c", &cmd_flush_temp]).check_run()?;
|
2019-06-13 12:19:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|