Files
topgrade/src/main.rs

227 lines
5.8 KiB
Rust
Raw Normal View History

2018-05-31 09:19:27 +03:00
extern crate os_type;
2018-05-29 23:48:30 +03:00
extern crate which;
#[macro_use]
extern crate error_chain;
2018-05-31 16:00:01 +03:00
extern crate termion;
2018-05-29 23:48:30 +03:00
2018-05-30 07:53:19 +03:00
mod error {
2018-05-29 23:48:30 +03:00
error_chain!{
foreign_links {
Io(::std::io::Error);
}
}
}
2018-05-30 07:53:19 +03:00
mod git;
2018-05-31 16:00:01 +03:00
mod terminal;
2018-05-30 07:53:19 +03:00
use error::*;
use git::Git;
2018-05-31 09:19:27 +03:00
use os_type::OSType;
2018-05-30 07:53:19 +03:00
use std::collections::HashSet;
2018-05-29 23:48:30 +03:00
use std::env::home_dir;
use std::path::PathBuf;
use std::process::{Command, ExitStatus};
2018-05-31 16:00:01 +03:00
use terminal::Terminal;
2018-05-29 23:48:30 +03:00
use which::which;
trait Chain
where
Self: std::marker::Sized,
{
fn and_then<F>(self, f: F) -> ::std::io::Result<Self>
where
F: FnOnce() -> ::std::io::Result<Self>;
}
impl Chain for ExitStatus {
fn and_then<F>(self, f: F) -> ::std::io::Result<Self>
where
F: FnOnce() -> ::std::io::Result<Self>,
{
if !self.success() {
Ok(self)
} else {
f()
}
}
}
2018-05-31 16:17:28 +03:00
const EMACS_UPGRADE: &str = "(progn (let ((package-menu-async nil))
(package-list-packages))
(package-menu-mark-upgrades)
(package-menu-execute 'noquery))";
2018-05-30 07:53:19 +03:00
fn home_path(p: &str) -> PathBuf {
2018-05-29 23:48:30 +03:00
let mut path = home_dir().unwrap();
2018-05-30 07:53:19 +03:00
path.push(p);
path
2018-05-29 23:48:30 +03:00
}
#[cfg(unix)]
fn tpm() -> Option<PathBuf> {
let mut path = home_dir().unwrap();
path.push(".tmux/plugins/tpm/bin/update_plugins");
if path.exists() {
Some(path)
} else {
None
}
}
fn run() -> Result<()> {
2018-05-30 07:53:19 +03:00
let git = Git::new();
let mut git_repos: HashSet<String> = HashSet::new();
2018-05-31 16:00:01 +03:00
let terminal = Terminal::new();
2018-05-30 07:53:19 +03:00
{
let mut collect_repo = |path| {
if let Some(repo) = git.get_repo_root(path) {
git_repos.insert(repo);
}
};
collect_repo(home_path(".emacs.d"));
if cfg!(unix) {
collect_repo(home_path(".zshrc"));
collect_repo(home_path(".oh-my-zsh"));
2018-05-30 07:53:19 +03:00
collect_repo(home_path(".tmux"));
}
}
for repo in git_repos {
2018-05-31 16:00:01 +03:00
terminal.print_separator(format!("Pulling {}", repo));
git.pull(repo)?;
}
2018-05-29 23:48:30 +03:00
if cfg!(unix) {
if let Ok(zsh) = which("zsh") {
2018-05-31 16:00:01 +03:00
terminal.print_separator("zplug");
2018-05-30 07:53:19 +03:00
if home_path(".zplug").exists() {
2018-05-29 23:48:30 +03:00
Command::new(&zsh)
2018-05-31 09:19:07 +03:00
.arg("-c")
.arg("source ~/.zshrc && zplug update")
2018-05-29 23:48:30 +03:00
.spawn()?
.wait()?;
}
}
if let Some(tpm) = tpm() {
2018-05-31 16:00:01 +03:00
terminal.print_separator("tmux plugins");
2018-05-30 07:53:19 +03:00
Command::new(&tpm).arg("all").spawn()?.wait()?;
2018-05-29 23:48:30 +03:00
}
}
2018-05-31 16:17:22 +03:00
let cargo_upgrade = home_path(".cargo/bin/cargo-install-update");
if cargo_upgrade.exists() {
terminal.print_separator("Cargo");
Command::new(&cargo_upgrade)
.arg("install-update")
.arg("--all")
.spawn()?
.wait()?;
}
2018-05-31 16:17:28 +03:00
if let Ok(emacs) = which("emacs") {
if home_path(".emacs.d").exists() {
2018-06-01 14:46:33 +03:00
terminal.print_separator("Emacs");
2018-05-31 16:17:28 +03:00
Command::new(&emacs)
.arg("--batch")
.arg("-l")
.arg(home_path(".emacs.d/init.el"))
2018-05-31 16:17:28 +03:00
.arg("--eval")
.arg(EMACS_UPGRADE)
.spawn()?
.wait()?;
}
}
2018-05-31 09:19:27 +03:00
if cfg!(target_os = "linux") {
let sudo = which("sudo");
match os_type::current_platform().os_type {
OSType::Arch => {
2018-05-31 16:00:01 +03:00
terminal.print_separator("System update");
2018-05-31 09:19:27 +03:00
if let Ok(yay) = which("yay") {
Command::new(yay).spawn()?.wait()?;
} else {
if let Ok(sudo) = &sudo {
Command::new(&sudo)
.arg("pacman")
.arg("-Syu")
.spawn()?
.wait()?;
}
}
}
2018-06-01 14:43:16 +03:00
OSType::CentOS | OSType::Redhat => {
if let Ok(sudo) = &sudo {
Command::new(&sudo)
.arg("yum")
.arg("upgrade")
.spawn()?
.wait()?;
}
}
OSType::Ubuntu | OSType::Debian => {
if let Ok(sudo) = &sudo {
Command::new(&sudo)
.arg("apt")
.arg("update")
.spawn()?
.wait()?
.and_then(|| {
Command::new(&sudo)
.arg("apt")
.arg("dist-upgrade")
.spawn()?
.wait()
})?;
}
}
2018-05-31 09:19:27 +03:00
_ => (),
}
if let Ok(sudo) = &sudo {
if let Ok(needrestart) = which("needrestart") {
2018-05-31 16:00:01 +03:00
terminal.print_separator("Check for needed restarts");
2018-05-31 09:19:27 +03:00
Command::new(&sudo).arg(&needrestart).spawn()?.wait()?;
}
}
}
2018-05-29 23:48:30 +03:00
if cfg!(target_os = "macos") {
if let Ok(brew) = which("brew") {
2018-05-31 16:00:01 +03:00
terminal.print_separator("Homebrew");
2018-05-29 23:48:30 +03:00
Command::new(&brew)
.arg("update")
.spawn()?
.wait()?
.and_then(|| Command::new(&brew).arg("upgrade").spawn()?.wait())?
.and_then(|| {
Command::new(&brew)
.arg("cleanup")
.arg("-sbr")
.spawn()?
.wait()
})?;
}
2018-05-31 16:00:01 +03:00
terminal.print_separator("System update");
2018-05-29 23:48:30 +03:00
Command::new("softwareupdate")
.arg("--install")
.arg("--all")
.spawn()?
.wait()?;
}
Ok(())
}
quick_main!(run);