2022-11-23 15:23:00 +00:00
|
|
|
use std::env;
|
2023-02-26 23:45:43 +02:00
|
|
|
use std::path::PathBuf;
|
2022-11-23 15:23:00 +00:00
|
|
|
use std::process::Command;
|
2022-11-08 05:54:35 -05:00
|
|
|
|
2022-11-11 09:39:29 -05:00
|
|
|
use color_eyre::eyre::Result;
|
2022-11-16 13:43:57 -05:00
|
|
|
use tracing::debug;
|
2022-11-23 15:23:00 +00:00
|
|
|
use walkdir::WalkDir;
|
2022-11-23 15:18:09 +00:00
|
|
|
|
2022-11-08 05:54:35 -05:00
|
|
|
use crate::command::CommandExt;
|
|
|
|
|
use crate::execution_context::ExecutionContext;
|
2024-03-09 17:57:33 +08:00
|
|
|
use crate::git::RepoStep;
|
2022-11-08 05:54:35 -05:00
|
|
|
use crate::terminal::print_separator;
|
|
|
|
|
use crate::utils::{require, PathExt};
|
2023-05-01 00:02:13 +05:30
|
|
|
use crate::HOME_DIR;
|
2024-02-17 13:15:53 +08:00
|
|
|
use crate::XDG_DIRS;
|
|
|
|
|
use etcetera::base_strategy::BaseStrategy;
|
2022-11-08 05:54:35 -05:00
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_zr(ctx: &ExecutionContext) -> Result<()> {
|
2019-10-01 20:50:44 +03:00
|
|
|
let zsh = require("zsh")?;
|
|
|
|
|
|
2020-09-05 00:05:54 +08:00
|
|
|
require("zr")?;
|
2019-10-01 20:50:44 +03:00
|
|
|
|
|
|
|
|
print_separator("zr");
|
|
|
|
|
|
2023-05-01 00:02:13 +05:30
|
|
|
let cmd = format!("source {} && zr --update", zshrc().display());
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(zsh)
|
|
|
|
|
.args(["-l", "-c", cmd.as_str()])
|
|
|
|
|
.status_checked()
|
2019-10-01 20:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-01 00:02:13 +05:30
|
|
|
fn zdotdir() -> PathBuf {
|
2019-10-01 20:50:44 +03:00
|
|
|
env::var("ZDOTDIR")
|
2023-02-26 23:45:43 +02:00
|
|
|
.map(PathBuf::from)
|
2023-05-01 00:02:13 +05:30
|
|
|
.unwrap_or_else(|_| HOME_DIR.clone())
|
2023-02-26 23:45:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-01 00:02:13 +05:30
|
|
|
pub fn zshrc() -> PathBuf {
|
|
|
|
|
zdotdir().join(".zshrc")
|
2023-02-26 23:45:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_antidote(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let zsh = require("zsh")?;
|
2023-05-01 00:02:13 +05:30
|
|
|
let mut antidote = zdotdir().join(".antidote").require()?;
|
2023-02-26 23:45:43 +02:00
|
|
|
antidote.push("antidote.zsh");
|
|
|
|
|
|
|
|
|
|
print_separator("antidote");
|
|
|
|
|
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(zsh)
|
|
|
|
|
.arg("-c")
|
|
|
|
|
.arg(format!("source {} && antidote update", antidote.display()))
|
|
|
|
|
.status_checked()
|
2019-10-01 20:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_antibody(ctx: &ExecutionContext) -> Result<()> {
|
2020-02-11 06:57:30 +01:00
|
|
|
require("zsh")?;
|
|
|
|
|
let antibody = require("antibody")?;
|
|
|
|
|
|
|
|
|
|
print_separator("antibody");
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type().execute(antibody).arg("update").status_checked()
|
2020-02-11 06:57:30 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_antigen(ctx: &ExecutionContext) -> Result<()> {
|
2019-10-01 20:50:44 +03:00
|
|
|
let zsh = require("zsh")?;
|
2023-05-01 00:02:13 +05:30
|
|
|
let zshrc = zshrc().require()?;
|
2019-10-01 20:50:44 +03:00
|
|
|
env::var("ADOTDIR")
|
|
|
|
|
.map(PathBuf::from)
|
2023-05-01 00:02:13 +05:30
|
|
|
.unwrap_or_else(|_| HOME_DIR.join("antigen.zsh"))
|
2019-10-01 20:50:44 +03:00
|
|
|
.require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("antigen");
|
|
|
|
|
|
2022-10-22 09:26:49 +00:00
|
|
|
let cmd = format!("source {} && (antigen selfupdate ; antigen update)", zshrc.display());
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(zsh)
|
|
|
|
|
.args(["-l", "-c", cmd.as_str()])
|
|
|
|
|
.status_checked()
|
2019-10-01 20:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_zgenom(ctx: &ExecutionContext) -> Result<()> {
|
2022-02-18 15:01:34 +01:00
|
|
|
let zsh = require("zsh")?;
|
2023-05-01 00:02:13 +05:30
|
|
|
let zshrc = zshrc().require()?;
|
2022-02-18 15:01:34 +01:00
|
|
|
env::var("ZGEN_SOURCE")
|
|
|
|
|
.map(PathBuf::from)
|
2023-05-01 00:02:13 +05:30
|
|
|
.unwrap_or_else(|_| HOME_DIR.join(".zgenom"))
|
2022-02-18 15:01:34 +01:00
|
|
|
.require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("zgenom");
|
|
|
|
|
|
|
|
|
|
let cmd = format!("source {} && zgenom selfupdate && zgenom update", zshrc.display());
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(zsh)
|
|
|
|
|
.args(["-l", "-c", cmd.as_str()])
|
|
|
|
|
.status_checked()
|
2022-02-18 15:01:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_zplug(ctx: &ExecutionContext) -> Result<()> {
|
2019-10-01 20:50:44 +03:00
|
|
|
let zsh = require("zsh")?;
|
2023-05-01 00:02:13 +05:30
|
|
|
zshrc().require()?;
|
2019-10-01 20:50:44 +03:00
|
|
|
|
|
|
|
|
env::var("ZPLUG_HOME")
|
|
|
|
|
.map(PathBuf::from)
|
2023-05-01 00:02:13 +05:30
|
|
|
.unwrap_or_else(|_| HOME_DIR.join(".zplug"))
|
2019-10-01 20:50:44 +03:00
|
|
|
.require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("zplug");
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type()
|
2022-11-08 05:54:35 -05:00
|
|
|
.execute(zsh)
|
|
|
|
|
.args(["-i", "-c", "zplug update"])
|
|
|
|
|
.status_checked()
|
2019-10-01 20:50:44 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_zinit(ctx: &ExecutionContext) -> Result<()> {
|
2019-12-01 06:40:23 +00:00
|
|
|
let zsh = require("zsh")?;
|
2023-05-01 00:02:13 +05:30
|
|
|
let zshrc = zshrc().require()?;
|
2019-12-01 06:40:23 +00:00
|
|
|
|
2022-02-18 15:01:34 +01:00
|
|
|
env::var("ZINIT_HOME")
|
2020-02-16 15:59:10 +02:00
|
|
|
.map(PathBuf::from)
|
2024-02-17 13:15:53 +08:00
|
|
|
.unwrap_or_else(|_| XDG_DIRS.data_dir().join("zinit"))
|
2020-02-20 17:29:11 +02:00
|
|
|
.require()?;
|
2020-02-17 21:10:17 +02:00
|
|
|
|
|
|
|
|
print_separator("zinit");
|
2020-02-15 08:11:45 -06:00
|
|
|
|
2024-02-24 11:26:41 +08:00
|
|
|
let cmd = format!("source {} && zinit self-update && zinit update --all", zshrc.display());
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(zsh)
|
|
|
|
|
.args(["-i", "-c", cmd.as_str()])
|
|
|
|
|
.status_checked()
|
2019-12-01 06:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_zi(ctx: &ExecutionContext) -> Result<()> {
|
2022-05-07 00:37:28 +09:00
|
|
|
let zsh = require("zsh")?;
|
2023-05-01 00:02:13 +05:30
|
|
|
let zshrc = zshrc().require()?;
|
2022-05-07 00:37:28 +09:00
|
|
|
|
2023-05-01 00:02:13 +05:30
|
|
|
HOME_DIR.join(".zi").require()?;
|
2022-05-07 00:37:28 +09:00
|
|
|
|
|
|
|
|
print_separator("zi");
|
|
|
|
|
|
2024-02-24 11:26:41 +08:00
|
|
|
let cmd = format!("source {} && zi self-update && zi update --all", zshrc.display());
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type().execute(zsh).args(["-i", "-c", &cmd]).status_checked()
|
2022-05-07 00:37:28 +09:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
pub fn run_zim(ctx: &ExecutionContext) -> Result<()> {
|
2020-10-24 14:46:38 -05:00
|
|
|
let zsh = require("zsh")?;
|
|
|
|
|
env::var("ZIM_HOME")
|
|
|
|
|
.or_else(|_| {
|
|
|
|
|
Command::new("zsh")
|
2022-11-08 05:54:35 -05:00
|
|
|
// TODO: Should these be quoted?
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["-c", "[[ -n ${ZIM_HOME} ]] && print -n ${ZIM_HOME}"])
|
2022-11-08 05:54:35 -05:00
|
|
|
.output_checked_utf8()
|
|
|
|
|
.map(|o| o.stdout)
|
2020-10-24 14:46:38 -05:00
|
|
|
})
|
|
|
|
|
.map(PathBuf::from)
|
2023-05-01 00:02:13 +05:30
|
|
|
.unwrap_or_else(|_| HOME_DIR.join(".zim"))
|
2020-10-24 14:46:38 -05:00
|
|
|
.require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("zim");
|
|
|
|
|
|
2023-05-25 15:09:23 +08:00
|
|
|
ctx.run_type()
|
2020-10-24 14:46:38 -05:00
|
|
|
.execute(zsh)
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["-i", "-c", "zimfw upgrade && zimfw update"])
|
2022-11-08 05:54:35 -05:00
|
|
|
.status_checked()
|
2020-10-24 14:46:38 -05:00
|
|
|
}
|
|
|
|
|
|
2020-03-08 21:38:49 +02:00
|
|
|
pub fn run_oh_my_zsh(ctx: &ExecutionContext) -> Result<()> {
|
2020-01-29 10:22:31 +02:00
|
|
|
require("zsh")?;
|
2023-07-18 13:59:55 +08:00
|
|
|
|
|
|
|
|
// When updating `oh-my-zsh` on a remote machine through topgrade, the
|
|
|
|
|
// following processes will be created:
|
|
|
|
|
//
|
|
|
|
|
// SSH -> ZSH -> ZSH ($SHELL) -> topgrade -> ZSH
|
|
|
|
|
//
|
|
|
|
|
// The first ZSH process, won't source zshrc (as it is a login shell),
|
|
|
|
|
// and thus it won't have the ZSH environment variable, as a result, the
|
|
|
|
|
// children processes won't get it either, so we source the zshrc and set
|
|
|
|
|
// the ZSH variable for topgrade here.
|
|
|
|
|
if ctx.under_ssh() {
|
2024-01-22 09:18:27 +08:00
|
|
|
let res_env_zsh = Command::new("zsh")
|
|
|
|
|
.args(["-ic", "print -rn -- ${ZSH:?}"])
|
|
|
|
|
.output_checked_utf8();
|
|
|
|
|
|
|
|
|
|
// this command will fail if `ZSH` is not set
|
|
|
|
|
if let Ok(output) = res_env_zsh {
|
|
|
|
|
let env_zsh = output.stdout;
|
|
|
|
|
debug!("Oh-my-zsh: under SSH, setting ZSH={}", env_zsh);
|
|
|
|
|
env::set_var("ZSH", env_zsh);
|
2023-09-19 09:15:34 +08:00
|
|
|
}
|
2023-07-18 13:59:55 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-22 20:06:19 +08:00
|
|
|
let oh_my_zsh = env::var("ZSH")
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
// default to `~/.oh-my-zsh`
|
|
|
|
|
.unwrap_or(HOME_DIR.join(".oh-my-zsh"))
|
|
|
|
|
.require()?;
|
2019-10-01 20:50:44 +03:00
|
|
|
|
|
|
|
|
print_separator("oh-my-zsh");
|
|
|
|
|
|
2020-07-22 06:19:08 +03:00
|
|
|
let custom_dir = env::var::<_>("ZSH_CUSTOM")
|
|
|
|
|
.or_else(|_| {
|
|
|
|
|
Command::new("zsh")
|
2022-11-08 05:54:35 -05:00
|
|
|
// TODO: Should these be quoted?
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["-c", "test $ZSH_CUSTOM && echo -n $ZSH_CUSTOM"])
|
2022-11-08 05:54:35 -05:00
|
|
|
.output_checked_utf8()
|
|
|
|
|
.map(|o| o.stdout)
|
2020-07-22 06:19:08 +03:00
|
|
|
})
|
|
|
|
|
.map(PathBuf::from)
|
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
|
let default_path = oh_my_zsh.join("custom");
|
|
|
|
|
debug!(
|
2024-10-03 12:47:35 +02:00
|
|
|
"Running zsh returned {e}. Using default path: {}",
|
2020-07-22 06:19:08 +03:00
|
|
|
default_path.display()
|
|
|
|
|
);
|
|
|
|
|
default_path
|
|
|
|
|
});
|
2020-03-08 21:38:49 +02:00
|
|
|
|
|
|
|
|
debug!("oh-my-zsh custom dir: {}", custom_dir.display());
|
|
|
|
|
|
2024-03-09 17:57:33 +08:00
|
|
|
let mut custom_repos = RepoStep::try_new()?;
|
2020-07-22 06:19:08 +03:00
|
|
|
|
|
|
|
|
for entry in WalkDir::new(custom_dir).max_depth(2) {
|
|
|
|
|
let entry = entry?;
|
2023-11-22 02:04:19 +01:00
|
|
|
custom_repos.insert_if_repo(entry.path());
|
2020-07-22 06:19:08 +03:00
|
|
|
}
|
|
|
|
|
|
2024-03-09 17:57:33 +08:00
|
|
|
custom_repos.remove(&oh_my_zsh);
|
2020-03-08 21:38:49 +02:00
|
|
|
ctx.run_type()
|
2020-12-01 11:28:52 +02:00
|
|
|
.execute("zsh")
|
2024-07-22 01:33:42 +02:00
|
|
|
.arg(oh_my_zsh.join("tools/upgrade.sh"))
|
2023-05-22 20:06:19 +08:00
|
|
|
// oh-my-zsh returns 80 when it is already updated and no changes pulled
|
|
|
|
|
// in this update.
|
|
|
|
|
// See this comment: https://github.com/r-darwish/topgrade/issues/569#issuecomment-736756731
|
|
|
|
|
// for more information.
|
2022-11-08 05:54:35 -05:00
|
|
|
.status_checked_with_codes(&[80])
|
2019-10-01 20:50:44 +03:00
|
|
|
}
|