Files
topgrade/src/generic.rs

248 lines
6.8 KiB
Rust
Raw Normal View History

2018-08-26 16:12:59 +03:00
use super::executor::Executor;
2018-08-19 14:45:23 +03:00
use super::terminal::Terminal;
2018-08-26 16:12:59 +03:00
use super::utils::{self, Check, PathExt};
2018-08-19 14:45:23 +03:00
use directories::BaseDirs;
use failure::Error;
2018-10-02 13:45:29 +03:00
use std::path::PathBuf;
use std::process::Command;
2018-08-19 14:45:23 +03:00
const EMACS_UPGRADE: &str = include_str!("emacs.el");
#[must_use]
pub fn run_cargo_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(cargo_update) = utils::which("cargo-install-update") {
2018-08-19 14:45:23 +03:00
terminal.print_separator("Cargo");
let success = || -> Result<(), Error> {
2018-08-26 16:12:59 +03:00
Executor::new(cargo_update, dry_run)
2018-08-19 14:45:23 +03:00
.args(&["install-update", "--git", "--all"])
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("Cargo", success));
}
None
}
2018-09-06 16:46:49 +03:00
#[must_use]
2018-10-04 11:40:29 +03:00
pub fn run_gem(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
2018-09-06 16:46:49 +03:00
if let Some(gem) = utils::which("gem") {
if base_dirs.home_dir().join(".gem").exists() {
terminal.print_separator("RubyGems");
let success = || -> Result<(), Error> {
Executor::new(&gem, dry_run)
.args(&["update", "--user-install"])
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("RubyGems", success));
}
}
None
}
2018-08-19 14:45:23 +03:00
#[must_use]
2018-10-04 11:40:29 +03:00
pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
2018-08-19 14:45:23 +03:00
if let Some(emacs) = utils::which("emacs") {
if let Some(init_file) = base_dirs.home_dir().join(".emacs.d/init.el").if_exists() {
terminal.print_separator("Emacs");
let success = || -> Result<(), Error> {
2018-08-26 16:12:59 +03:00
Executor::new(&emacs, dry_run)
2018-10-04 11:40:29 +03:00
.args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE])
.spawn()?
2018-08-19 14:45:23 +03:00
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("Emacs", success));
}
}
None
}
#[must_use]
2018-10-29 14:32:33 +02:00
#[cfg(not(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly"
)))]
2018-08-26 16:12:59 +03:00
pub fn run_apm(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
2018-08-19 14:45:23 +03:00
if let Some(apm) = utils::which("apm") {
terminal.print_separator("Atom Package Manager");
let success = || -> Result<(), Error> {
2018-08-26 16:12:59 +03:00
Executor::new(&apm, dry_run)
2018-08-19 14:45:23 +03:00
.args(&["upgrade", "--confirm=false"])
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("apm", success));
}
None
}
#[must_use]
2018-10-04 11:40:29 +03:00
pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
2018-08-19 14:45:23 +03:00
if let Some(rustup) = utils::which("rustup") {
terminal.print_separator("rustup");
let success = || -> Result<(), Error> {
if rustup.is_descendant_of(base_dirs.home_dir()) {
2018-08-26 16:12:59 +03:00
Executor::new(&rustup, dry_run)
2018-08-19 14:45:23 +03:00
.args(&["self", "update"])
.spawn()?
.wait()?
.check()?;
}
2018-10-04 11:40:29 +03:00
Executor::new(&rustup, dry_run).arg("update").spawn()?.wait()?.check()?;
2018-08-19 14:45:23 +03:00
Ok(())
}().is_ok();
return Some(("rustup", success));
}
None
}
2018-10-18 16:05:27 +03:00
#[must_use]
pub fn run_jetpack(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(jetpack) = utils::which("jetpack") {
terminal.print_separator("Jetpack");
let success = || -> Result<(), Error> {
Executor::new(&jetpack, dry_run)
.args(&["global", "update"])
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("Jetpack", success));
}
None
}
#[must_use]
pub fn run_opam_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(opam) = utils::which("opam") {
terminal.print_separator("OCaml Package Manager");
let success = || -> Result<(), Error> {
2018-10-04 11:40:29 +03:00
Executor::new(&opam, dry_run).arg("update").spawn()?.wait()?.check()?;
Executor::new(&opam, dry_run).arg("upgrade").spawn()?.wait()?.check()?;
Ok(())
}().is_ok();
return Some(("OPAM", success));
}
None
}
2018-11-10 20:22:26 +02:00
#[must_use]
pub fn run_vcpkg_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(vcpkg) = utils::which("vcpkg") {
terminal.print_separator("vcpkg");
let success = || -> Result<(), Error> {
Executor::new(&vcpkg, dry_run).args(&["upgrade", "--no-dry-run"]).spawn()?.wait()?.check()?;
Ok(())
}().is_ok();
return Some(("vcpkg", success));
}
None
}
2018-10-31 13:01:57 +02:00
#[must_use]
pub fn run_pipx_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(pipx) = utils::which("pipx") {
terminal.print_separator("pipx");
let success = || -> Result<(), Error> {
Executor::new(&pipx, dry_run)
.arg("upgrade-all")
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("pipx", success));
}
None
}
2018-08-19 14:45:23 +03:00
#[must_use]
2018-10-04 11:40:29 +03:00
pub fn run_custom_command(name: &str, command: &str, terminal: &mut Terminal, dry_run: bool) -> Result<(), Error> {
2018-08-19 14:45:23 +03:00
terminal.print_separator(name);
2018-08-26 16:12:59 +03:00
Executor::new("sh", dry_run)
.arg("-c")
.arg(command)
.spawn()?
.wait()?
.check()?;
2018-08-19 14:45:23 +03:00
Ok(())
}
2018-10-02 13:45:29 +03:00
#[must_use]
pub fn run_composer_update(
base_dirs: &BaseDirs,
terminal: &mut Terminal,
dry_run: bool,
) -> Option<(&'static str, bool)> {
if let Some(composer) = utils::which("composer") {
let composer_home = || -> Result<PathBuf, Error> {
let output = Command::new(&composer)
.args(&["global", "config", "--absolute", "home"])
.output()?;
output.status.check()?;
Ok(PathBuf::from(&String::from_utf8(output.stdout)?))
}();
if let Ok(composer_home) = composer_home {
if composer_home.is_descendant_of(base_dirs.home_dir()) {
terminal.print_separator("Composer");
let success = || -> Result<(), Error> {
Executor::new(&composer, dry_run)
.args(&["global", "update"])
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("Composer", success));
}
}
}
None
}