Files
topgrade/src/main.rs

131 lines
2.7 KiB
Rust
Raw Normal View History

2018-05-29 23:48:30 +03:00
extern crate which;
#[macro_use]
extern crate error_chain;
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;
use error::*;
use git::Git;
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};
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-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();
{
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(".tmux"));
}
}
2018-05-29 23:48:30 +03:00
if cfg!(unix) {
if let Ok(zsh) = which("zsh") {
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-30 07:53:19 +03:00
Command::new(&tpm).arg("all").spawn()?.wait()?;
2018-05-29 23:48:30 +03:00
}
}
2018-05-30 07:53:19 +03:00
for repo in git_repos {
git.pull(repo)?;
}
2018-05-29 23:48:30 +03:00
if cfg!(target_os = "macos") {
if let Ok(brew) = which("brew") {
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()
})?;
}
}
if cfg!(target_os = "macos") {
Command::new("softwareupdate")
.arg("--install")
.arg("--all")
.spawn()?
.wait()?;
}
Ok(())
}
quick_main!(run);