Merge branch 'master' of github.com:r-darwish/topgrade

This commit is contained in:
Eric Mark Martin
2019-03-10 18:08:25 -07:00
7 changed files with 99 additions and 168 deletions

View File

@@ -24,32 +24,6 @@ use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
fn execute_legacy<'a, F, M>(func: F, no_retry: bool) -> Result<Option<(M, bool)>, Error>
where
M: Into<Cow<'a, str>>,
F: Fn() -> Option<(M, bool)>,
{
while let Some((key, success)) = func() {
if success {
return Ok(Some((key, success)));
}
let interrupted = ctrlc::interrupted();
if interrupted {
ctrlc::unset_interrupted();
}
let should_ask = interrupted || !no_retry;
let should_retry = should_ask && should_retry(interrupted).context(ErrorKind::Retry)?;
if !should_retry {
return Ok(Some((key, success)));
}
}
Ok(None)
}
fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error> fn execute<'a, F, M>(report: &mut Report<'a>, key: M, func: F, no_retry: bool) -> Result<(), Error>
where where
F: Fn() -> Result<(), Error>, F: Fn() -> Result<(), Error>,
@@ -193,10 +167,12 @@ fn run() -> Result<(), Error> {
config.no_retry(), config.no_retry(),
)?; )?;
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
report.push_result(execute_legacy( execute(
&mut report,
"FreeBSD Packages",
|| freebsd::upgrade_packages(&sudo, run_type), || freebsd::upgrade_packages(&sudo, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
#[cfg(unix)] #[cfg(unix)]
execute(&mut report, "nix", || unix::run_nix(run_type), config.no_retry())?; execute(&mut report, "nix", || unix::run_nix(run_type), config.no_retry())?;
@@ -243,7 +219,12 @@ fn run() -> Result<(), Error> {
} }
} }
for repo in git_repos.repositories() { for repo in git_repos.repositories() {
report.push_result(execute_legacy(|| git.pull(&repo, run_type), config.no_retry())?); execute(
&mut report,
format!("git: {}", utils::HumanizedPath::from(std::path::Path::new(&repo))),
|| git.pull(&repo, run_type),
config.no_retry(),
)?;
} }
#[cfg(unix)] #[cfg(unix)]
@@ -260,10 +241,12 @@ fn run() -> Result<(), Error> {
|| unix::run_fisher(&base_dirs, run_type), || unix::run_fisher(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?; )?;
report.push_result(execute_legacy( execute(
&mut report,
"tmux",
|| tmux::run_tpm(&base_dirs, run_type), || tmux::run_tpm(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
execute( execute(
@@ -316,30 +299,38 @@ fn run() -> Result<(), Error> {
)?; )?;
if config.should_run(Step::Vim) { if config.should_run(Step::Vim) {
report.push_result(execute_legacy( execute(
&mut report,
"vim",
|| vim::upgrade_vim(&base_dirs, run_type), || vim::upgrade_vim(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
report.push_result(execute_legacy( execute(
&mut report,
"Neovim",
|| vim::upgrade_neovim(&base_dirs, run_type), || vim::upgrade_neovim(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
report.push_result(execute_legacy( execute(
&mut report,
"NPM",
|| node::run_npm_upgrade(&base_dirs, run_type), || node::run_npm_upgrade(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
execute( execute(
&mut report, &mut report,
"composer", "composer",
|| generic::run_composer_update(&base_dirs, run_type), || generic::run_composer_update(&base_dirs, run_type),
config.no_retry(), config.no_retry(),
)?; )?;
report.push_result(execute_legacy( execute(
&mut report,
"yarn",
|| node::yarn_global_update(run_type), || node::yarn_global_update(run_type),
config.no_retry(), config.no_retry(),
)?); )?;
#[cfg(not(any( #[cfg(not(any(
target_os = "freebsd", target_os = "freebsd",
@@ -376,10 +367,12 @@ fn run() -> Result<(), Error> {
if let Some(commands) = config.commands() { if let Some(commands) = config.commands() {
for (name, command) in commands { for (name, command) in commands {
report.push_result(execute_legacy( execute(
|| Some((name, generic::run_custom_command(&name, &command, run_type).is_ok())), &mut report,
name,
|| generic::run_custom_command(&name, &command, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
} }
@@ -402,17 +395,24 @@ fn run() -> Result<(), Error> {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
if config.should_run(Step::System) { if config.should_run(Step::System) {
report.push_result(execute_legacy(|| macos::upgrade_macos(run_type), config.no_retry())?); execute(
&mut report,
"App Store",
|| macos::upgrade_macos(run_type),
config.no_retry(),
)?;
} }
} }
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
{ {
if config.should_run(Step::System) { if config.should_run(Step::System) {
report.push_result(execute_legacy( execute(
&mut report,
"FreeBSD Upgrade",
|| freebsd::upgrade_freebsd(&sudo, run_type), || freebsd::upgrade_freebsd(&sudo, run_type),
config.no_retry(), config.no_retry(),
)?); )?;
} }
} }

View File

@@ -55,25 +55,18 @@ impl Git {
None None
} }
pub fn pull<P: AsRef<Path>>(&self, path: P, run_type: RunType) -> Option<(String, bool)> { pub fn pull<P: AsRef<Path>>(&self, path: P, run_type: RunType) -> Result<(), Error> {
let path = path.as_ref(); let path = path.as_ref();
print_separator(format!("Pulling {}", HumanizedPath::from(path))); print_separator(format!("Pulling {}", HumanizedPath::from(path)));
let git = self.git.as_ref().unwrap(); let git = self.git.as_ref().unwrap();
let success = || -> Result<(), Error> { run_type
run_type .execute(git)
.execute(git) .args(&["pull", "--rebase", "--autostash"])
.args(&["pull", "--rebase", "--autostash"]) .current_dir(&path)
.current_dir(&path) .check_run()
.check_run()?;
Ok(())
}()
.is_ok();
Some((format!("git: {}", HumanizedPath::from(path)), success))
} }
} }

View File

@@ -1,7 +1,7 @@
use crate::error::Error; use crate::error::{Error, ErrorKind};
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{which, PathExt}; use crate::utils::{require, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
@@ -29,33 +29,20 @@ impl NPM {
} }
} }
#[must_use] pub fn run_npm_upgrade(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
pub fn run_npm_upgrade(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { let npm = require("npm").map(NPM::new)?;
if let Some(npm) = which("npm").map(NPM::new) { let npm_root = npm.root()?;
if let Ok(npm_root) = npm.root() { if !npm_root.is_descendant_of(base_dirs.home_dir()) {
if npm_root.is_descendant_of(base_dirs.home_dir()) { Err(ErrorKind::SkipStep)?;
print_separator("Node Package Manager");
let success = npm.upgrade(run_type).is_ok();
return Some(("NPM", success));
}
}
}
None
}
#[must_use]
pub fn yarn_global_update(run_type: RunType) -> Option<(&'static str, bool)> {
if let Some(yarn) = which("yarn") {
print_separator("Yarn");
let success = || -> Result<(), Error> {
run_type.execute(&yarn).args(&["global", "upgrade", "-s"]).check_run()?;
Ok(())
}()
.is_ok();
return Some(("yarn", success));
} }
None print_separator("Node Package Manager");
npm.upgrade(run_type)
}
pub fn yarn_global_update(run_type: RunType) -> Result<(), Error> {
let yarn = require("yarn")?;
print_separator("Yarn");
run_type.execute(&yarn).args(&["global", "upgrade", "-s"]).check_run()
} }

View File

@@ -5,43 +5,19 @@ use failure::ResultExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
#[must_use] pub fn upgrade_freebsd(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
pub fn upgrade_freebsd(sudo: &Option<PathBuf>, run_type: RunType) -> Option<(&'static str, bool)> { let sudo = require_option(sudo)?;
print_separator("FreeBSD Update"); print_separator("FreeBSD Update");
run_type
if let Some(sudo) = sudo { .execute(sudo)
let success = || -> Result<(), Error> { .args(&["/usr/sbin/freebsd-update", "fetch", "install"])
run_type .check_run()
.execute(sudo)
.args(&["/usr/sbin/freebsd-update", "fetch", "install"])
.check_run()?;
Ok(())
}()
.is_ok();
Some(("FreeBSD Update", success))
} else {
print_warning("No sudo or yay detected. Skipping system upgrade");
None
}
} }
#[must_use] pub fn upgrade_packages(sudo: &Option<PathBuf>, run_type: RunType) -> Result<(), Error> {
pub fn upgrade_packages(sudo: &Option<PathBuf>, run_type: RunType) -> Option<(&'static str, bool)> { let sudo = require_option(sudo)?;
print_separator("FreeBSD Packages"); print_separator("FreeBSD Packages");
run_type.execute(sudo).args(&["/usr/sbin/pkg", "upgrade"]).check_run()
if let Some(sudo) = sudo {
let success = || -> Result<(), Error> {
run_type.execute(sudo).args(&["/usr/sbin/pkg", "upgrade"]).check_run()?;
Ok(())
}()
.is_ok();
Some(("FreeBSD Packages", success))
} else {
print_warning("No sudo or yay detected. Skipping package upgrade");
None
}
} }
pub fn audit_packages(sudo: &Option<PathBuf>) -> Result<(), Error> { pub fn audit_packages(sudo: &Option<PathBuf>) -> Result<(), Error> {

View File

@@ -3,17 +3,11 @@ use crate::executor::RunType;
use crate::terminal::print_separator; use crate::terminal::print_separator;
#[must_use] #[must_use]
pub fn upgrade_macos(run_type: RunType) -> Option<(&'static str, bool)> { pub fn upgrade_macos(run_type: RunType) -> Result<(), Error> {
print_separator("App Store"); print_separator("App Store");
let success = || -> Result<(), Error> { run_type
run_type .execute("softwareupdate")
.execute("softwareupdate") .args(&["--install", "--all"])
.args(&["--install", "--all"]) .check_run()
.check_run()?;
Ok(())
}()
.is_ok();
Some(("App Store", success))
} }

View File

@@ -10,24 +10,15 @@ use std::os::unix::process::CommandExt;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { pub fn run_tpm(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
if let Some(tpm) = base_dirs let tpm = base_dirs
.home_dir() .home_dir()
.join(".tmux/plugins/tpm/bin/update_plugins") .join(".tmux/plugins/tpm/bin/update_plugins")
.if_exists() .require()?;
{
print_separator("tmux plugins");
let success = || -> Result<(), Error> { print_separator("tmux plugins");
run_type.execute(&tpm).arg("all").check_run()?;
Ok(())
}()
.is_ok();
return Some(("tmux", success)); run_type.execute(&tpm).arg("all").check_run()
}
None
} }
fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> { fn has_session(tmux: &Path, session_name: &str) -> Result<bool, io::Error> {

View File

@@ -1,7 +1,7 @@
use crate::error::Error; use crate::error::Error;
use crate::executor::RunType; use crate::executor::RunType;
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{which, PathExt}; use crate::utils::{require, require_option, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
@@ -77,31 +77,21 @@ fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: PluginFramework, ru
} }
#[must_use] #[must_use]
pub fn upgrade_vim(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { pub fn upgrade_vim(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
if let Some(vim) = which("vim") { let vim = require("vim")?;
if let Some(vimrc) = vimrc(&base_dirs) { let vimrc = require_option(vimrc(&base_dirs))?;
if let Some(plugin_framework) = PluginFramework::detect(&vimrc) { let plugin_framework = require_option(PluginFramework::detect(&vimrc))?;
print_separator(&format!("Vim ({:?})", plugin_framework));
let success = upgrade(&vim, &vimrc, plugin_framework, run_type).is_ok();
return Some(("vim", success));
}
}
}
None print_separator(&format!("Vim ({:?})", plugin_framework));
upgrade(&vim, &vimrc, plugin_framework, run_type)
} }
#[must_use] #[must_use]
pub fn upgrade_neovim(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> { pub fn upgrade_neovim(base_dirs: &BaseDirs, run_type: RunType) -> Result<(), Error> {
if let Some(nvim) = which("nvim") { let nvim = require("nvim")?;
if let Some(nvimrc) = nvimrc(&base_dirs) { let nvimrc = require_option(nvimrc(&base_dirs))?;
if let Some(plugin_framework) = PluginFramework::detect(&nvimrc) { let plugin_framework = require_option(PluginFramework::detect(&nvimrc))?;
print_separator(&format!("Neovim ({:?})", plugin_framework));
let success = upgrade(&nvim, &nvimrc, plugin_framework, run_type).is_ok();
return Some(("Neovim", success));
}
}
}
None print_separator(&format!("Neovim ({:?})", plugin_framework));
upgrade(&nvim, &nvimrc, plugin_framework, run_type)
} }