diff --git a/appveyor.yml b/appveyor.yml index b87866b3..6229b6e0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -46,12 +46,6 @@ cache: - C:\Users\appveyor\.cargo\registry - target -branches: - only: - # Release tags - - /^v\d+\.\d+\.\d+.*$/ - - master - notifications: - provider: Email on_build_success: false diff --git a/src/linux.rs b/src/linux.rs index 23e77a91..3f7bf3c2 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -138,3 +138,49 @@ pub fn upgrade_debian( Ok(()) } + +pub fn run_needrestart(sudo: &PathBuf) -> Result<(), failure::Error> { + Command::new(&sudo) + .arg("needrestart") + .spawn()? + .wait()? + .check()?; + + Ok(()) +} + +pub fn run_fwupdmgr(fwupdmgr: &PathBuf) -> Result<(), failure::Error> { + Command::new(&fwupdmgr) + .arg("refresh") + .spawn()? + .wait()? + .check()?; + + Command::new(&fwupdmgr) + .arg("get-updates") + .spawn()? + .wait()? + .check()?; + + Ok(()) +} + +pub fn run_flatpak(flatpak: &PathBuf) -> Result<(), failure::Error> { + Command::new(&flatpak) + .arg("update") + .spawn()? + .wait()? + .check()?; + + Ok(()) +} + +pub fn run_snap(sudo: &PathBuf, snap: &PathBuf) -> Result<(), failure::Error> { + Command::new(&sudo) + .args(&[snap.to_str().unwrap(), "refresh"]) + .spawn()? + .wait()? + .check()?; + + Ok(()) +} diff --git a/src/macos.rs b/src/macos.rs new file mode 100644 index 00000000..87bda353 --- /dev/null +++ b/src/macos.rs @@ -0,0 +1,13 @@ +use super::utils::Check; +use failure; +use std::process::Command; + +pub fn upgrade_macos() -> Result<(), failure::Error> { + Command::new("softwareupdate") + .args(&["--install", "--all"]) + .spawn()? + .wait()? + .check()?; + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 826331be..7d4f04f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,10 +16,17 @@ extern crate env_logger; extern crate term_size; extern crate termcolor; -mod config; -mod git; #[cfg(target_os = "linux")] mod linux; +#[cfg(target_os = "macos")] +mod macos; +#[cfg(unix)] +mod unix; +#[cfg(target_os = "windows")] +mod windows; + +mod config; +mod git; mod npm; mod report; mod steps; @@ -34,13 +41,7 @@ use git::{Git, Repositories}; use report::{Report, Reporter}; use std::env; use std::env::home_dir; -#[cfg(unix)] -use std::os::unix::process::CommandExt; -#[cfg(unix)] -use std::path::PathBuf; use std::process::exit; -#[cfg(unix)] -use std::process::Command; use steps::*; use terminal::Terminal; use utils::{home_path, is_ancestor}; @@ -49,17 +50,6 @@ use utils::{home_path, is_ancestor}; #[fail(display = "A step failed")] struct StepFailed; -#[cfg(unix)] -fn tpm() -> Option { - let mut path = home_dir().unwrap(); - path.push(".tmux/plugins/tpm/bin/update_plugins"); - if path.exists() { - Some(path) - } else { - None - } -} - fn run() -> Result<(), Error> { let matches = App::new("Topgrade") .version(crate_version!()) @@ -75,22 +65,7 @@ fn run() -> Result<(), Error> { if matches.is_present("tmux") && !env::var("TMUX").is_ok() { #[cfg(unix)] { - let tmux = utils::which("tmux").expect("Could not find tmux"); - let err = Command::new(tmux) - .args(&[ - "new-session", - "-s", - "topgrade", - "-n", - "topgrade", - &env::args().collect::>().join(" "), - ";", - "set", - "remain-on-exit", - "on", - ]) - .exec(); - panic!("{:?}", err); + unix::run_in_tmux(); } } @@ -141,7 +116,7 @@ fn run() -> Result<(), Error> { { if let Some(choco) = utils::which("choco") { terminal.print_separator("Chocolatey"); - run_chocolatey(&choco).report("Chocolatey", &mut reports); + windows::run_chocolatey(&choco).report("Chocolatey", &mut reports); } } @@ -173,13 +148,13 @@ fn run() -> Result<(), Error> { if let Some(zsh) = utils::which("zsh") { if home_path(".zplug").exists() { terminal.print_separator("zplug"); - run_zplug(&zsh).report("zplug", &mut reports); + unix::run_zplug(&zsh).report("zplug", &mut reports); } } - if let Some(tpm) = tpm() { + if let Some(tpm) = unix::tpm_path() { terminal.print_separator("tmux plugins"); - run_tpm(&tpm).report("tmux", &mut reports); + unix::run_tpm(&tpm).report("tmux", &mut reports); } } @@ -230,13 +205,13 @@ fn run() -> Result<(), Error> { { if let Some(flatpak) = utils::which("flatpak") { terminal.print_separator("Flatpak"); - run_flatpak(&flatpak).report("Flatpak", &mut reports); + linux::run_flatpak(&flatpak).report("Flatpak", &mut reports); } if let Some(sudo) = &sudo { if let Some(snap) = utils::which("snap") { terminal.print_separator("snap"); - run_snap(&sudo, &snap).report("snap", &mut reports); + linux::run_snap(&sudo, &snap).report("snap", &mut reports); } } } @@ -252,13 +227,13 @@ fn run() -> Result<(), Error> { { if let Some(fwupdmgr) = utils::which("fwupdmgr") { terminal.print_separator("Firmware upgrades"); - run_fwupdmgr(&fwupdmgr).report("Firmware upgrade", &mut reports); + linux::run_fwupdmgr(&fwupdmgr).report("Firmware upgrade", &mut reports); } if let Some(sudo) = &sudo { if let Some(_) = utils::which("needrestart") { terminal.print_separator("Check for needed restarts"); - run_needrestart(&sudo).report("Restarts", &mut reports); + linux::run_needrestart(&sudo).report("Restarts", &mut reports); } } } @@ -266,7 +241,7 @@ fn run() -> Result<(), Error> { #[cfg(target_os = "macos")] { terminal.print_separator("App Store"); - upgrade_macos().report("App Store", &mut reports);; + macos::upgrade_macos().report("App Store", &mut reports); } if !reports.is_empty() { diff --git a/src/steps.rs b/src/steps.rs index 9cdd10b1..c29041ee 100644 --- a/src/steps.rs +++ b/src/steps.rs @@ -7,24 +7,6 @@ use utils::is_ancestor; const EMACS_UPGRADE: &str = include_str!("emacs.el"); -#[cfg(unix)] -pub fn run_zplug(zsh: &PathBuf) -> Result<(), failure::Error> { - Command::new(zsh) - .args(&["-c", "source ~/.zshrc && zplug update"]) - .spawn()? - .wait()? - .check()?; - - Ok(()) -} - -#[cfg(unix)] -pub fn run_tpm(tpm: &PathBuf) -> Result<(), failure::Error> { - Command::new(&tpm).arg("all").spawn()?.wait()?.check()?; - - Ok(()) -} - pub fn run_cargo_update(cargo_update: &PathBuf) -> Result<(), failure::Error> { Command::new(&cargo_update) .args(&["install-update", "--git", "--all"]) @@ -88,34 +70,6 @@ pub fn run_apm(apm: &PathBuf) -> Result<(), failure::Error> { Ok(()) } -#[cfg(target_os = "linux")] -pub fn run_needrestart(sudo: &PathBuf) -> Result<(), failure::Error> { - Command::new(&sudo) - .arg("needrestart") - .spawn()? - .wait()? - .check()?; - - Ok(()) -} - -#[cfg(target_os = "linux")] -pub fn run_fwupdmgr(fwupdmgr: &PathBuf) -> Result<(), failure::Error> { - Command::new(&fwupdmgr) - .arg("refresh") - .spawn()? - .wait()? - .check()?; - - Command::new(&fwupdmgr) - .arg("get-updates") - .spawn()? - .wait()? - .check()?; - - Ok(()) -} - pub fn run_rustup(rustup: &PathBuf) -> Result<(), failure::Error> { if is_ancestor(&home_dir().unwrap(), &rustup) { Command::new(rustup) @@ -130,28 +84,6 @@ pub fn run_rustup(rustup: &PathBuf) -> Result<(), failure::Error> { Ok(()) } -#[cfg(target_os = "macos")] -pub fn upgrade_macos() -> Result<(), failure::Error> { - Command::new("softwareupdate") - .args(&["--install", "--all"]) - .spawn()? - .wait()? - .check()?; - - Ok(()) -} - -#[cfg(windows)] -pub fn run_chocolatey(choco: &PathBuf) -> Result<(), failure::Error> { - Command::new(&choco) - .args(&["upgrade", "all"]) - .spawn()? - .wait()? - .check()?; - - Ok(()) -} - pub fn run_homebrew(homebrew: &PathBuf) -> Result<(), failure::Error> { Command::new(&homebrew) .arg("update") @@ -178,25 +110,3 @@ pub fn run_custom_command(command: &str) -> Result<(), failure::Error> { Ok(()) } - -#[cfg(target_os = "linux")] -pub fn run_flatpak(flatpak: &PathBuf) -> Result<(), failure::Error> { - Command::new(&flatpak) - .arg("update") - .spawn()? - .wait()? - .check()?; - - Ok(()) -} - -#[cfg(target_os = "linux")] -pub fn run_snap(sudo: &PathBuf, snap: &PathBuf) -> Result<(), failure::Error> { - Command::new(&sudo) - .args(&[snap.to_str().unwrap(), "refresh"]) - .spawn()? - .wait()? - .check()?; - - Ok(()) -} diff --git a/src/unix.rs b/src/unix.rs new file mode 100644 index 00000000..85cefc9c --- /dev/null +++ b/src/unix.rs @@ -0,0 +1,53 @@ +use super::utils; +use super::utils::Check; +use failure; +use std::env; +use std::env::home_dir; +use std::os::unix::process::CommandExt; +use std::path::PathBuf; +use std::process::Command; + +pub fn run_zplug(zsh: &PathBuf) -> Result<(), failure::Error> { + Command::new(zsh) + .args(&["-c", "source ~/.zshrc && zplug update"]) + .spawn()? + .wait()? + .check()?; + + Ok(()) +} + +pub fn run_tpm(tpm: &PathBuf) -> Result<(), failure::Error> { + Command::new(&tpm).arg("all").spawn()?.wait()?.check()?; + + Ok(()) +} + +pub fn tpm_path() -> Option { + let mut path = home_dir().unwrap(); + path.push(".tmux/plugins/tpm/bin/update_plugins"); + if path.exists() { + Some(path) + } else { + None + } +} + +pub fn run_in_tmux() -> ! { + let tmux = utils::which("tmux").expect("Could not find tmux"); + let err = Command::new(tmux) + .args(&[ + "new-session", + "-s", + "topgrade", + "-n", + "topgrade", + &env::args().collect::>().join(" "), + ";", + "set", + "remain-on-exit", + "on", + ]) + .exec(); + panic!("{:?}", err); +} diff --git a/src/windows.rs b/src/windows.rs new file mode 100644 index 00000000..4157bbc6 --- /dev/null +++ b/src/windows.rs @@ -0,0 +1,14 @@ +use super::utils::Check; +use failure; +use std::path::PathBuf; +use std::process::Command; + +pub fn run_chocolatey(choco: &PathBuf) -> Result<(), failure::Error> { + Command::new(&choco) + .args(&["upgrade", "all"]) + .spawn()? + .wait()? + .check()?; + + Ok(()) +}