PathExt trait
This commit is contained in:
14
src/main.rs
14
src/main.rs
@@ -43,7 +43,7 @@ use std::env;
|
||||
use std::process::exit;
|
||||
use steps::*;
|
||||
use terminal::Terminal;
|
||||
use utils::is_ancestor;
|
||||
use utils::PathExt;
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[fail(display = "A step failed")]
|
||||
@@ -165,7 +165,7 @@ fn run() -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(tpm) = unix::tpm_path() {
|
||||
if let Some(tpm) = unix::tpm_path(&base_dirs) {
|
||||
terminal.print_separator("tmux plugins");
|
||||
unix::run_tpm(&tpm).report("tmux", &mut reports);
|
||||
}
|
||||
@@ -173,18 +173,16 @@ fn run() -> Result<(), Error> {
|
||||
|
||||
if let Some(rustup) = utils::which("rustup") {
|
||||
terminal.print_separator("rustup");
|
||||
run_rustup(&rustup).report("rustup", &mut reports);
|
||||
run_rustup(&rustup, &base_dirs).report("rustup", &mut reports);
|
||||
}
|
||||
|
||||
let cargo_upgrade = base_dirs.home_dir().join(".cargo/bin/cargo-install-update");
|
||||
if cargo_upgrade.exists() {
|
||||
if let Some(cargo_upgrade) = base_dirs.home_dir().join(".cargo/bin/cargo-install-update").if_exists() {
|
||||
terminal.print_separator("Cargo");
|
||||
run_cargo_update(&cargo_upgrade).report("Cargo", &mut reports);
|
||||
}
|
||||
|
||||
if let Some(emacs) = utils::which("emacs") {
|
||||
let init_file = base_dirs.home_dir().join(".emacs.d/init.el");
|
||||
if init_file.exists() {
|
||||
if let Some(init_file) = base_dirs.home_dir().join(".emacs.d/init.el").if_exists() {
|
||||
terminal.print_separator("Emacs");
|
||||
run_emacs(&emacs, &init_file).report("Emacs", &mut reports);
|
||||
}
|
||||
@@ -210,7 +208,7 @@ fn run() -> Result<(), Error> {
|
||||
|
||||
if let Some(npm) = utils::which("npm").map(npm::NPM::new) {
|
||||
if let Ok(npm_root) = npm.root() {
|
||||
if is_ancestor(&base_dirs.home_dir(), &npm_root) {
|
||||
if npm_root.is_descendant_of(base_dirs.home_dir()) {
|
||||
terminal.print_separator("Node Package Manager");
|
||||
npm.upgrade().report("Node Package Manager", &mut reports);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::utils::Check;
|
||||
use super::utils::{Check, PathExt};
|
||||
use directories::BaseDirs;
|
||||
use failure;
|
||||
use std::env::home_dir;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use utils::is_ancestor;
|
||||
|
||||
const EMACS_UPGRADE: &str = include_str!("emacs.el");
|
||||
|
||||
@@ -60,8 +59,8 @@ pub fn run_apm(apm: &PathBuf) -> Result<(), failure::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_rustup(rustup: &PathBuf) -> Result<(), failure::Error> {
|
||||
if is_ancestor(&home_dir().unwrap(), &rustup) {
|
||||
pub fn run_rustup(rustup: &PathBuf, base_dirs: &BaseDirs) -> Result<(), failure::Error> {
|
||||
if rustup.is_descendant_of(base_dirs.home_dir()) {
|
||||
Command::new(rustup).args(&["self", "update"]).spawn()?.wait()?.check()?;
|
||||
}
|
||||
|
||||
|
||||
20
src/unix.rs
20
src/unix.rs
@@ -1,8 +1,7 @@
|
||||
use super::utils;
|
||||
use super::utils::Check;
|
||||
use super::utils::{which, Check, PathExt};
|
||||
use directories::BaseDirs;
|
||||
use failure;
|
||||
use std::env;
|
||||
use std::env::home_dir;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
@@ -33,18 +32,15 @@ pub fn run_tpm(tpm: &PathBuf) -> Result<(), failure::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn tpm_path() -> Option<PathBuf> {
|
||||
let mut path = home_dir().unwrap();
|
||||
path.push(".tmux/plugins/tpm/bin/update_plugins");
|
||||
if path.exists() {
|
||||
Some(path)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
pub fn tpm_path(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
||||
base_dirs
|
||||
.home_dir()
|
||||
.join(".tmux/plugins/tpm/bin/update_plugins")
|
||||
.if_exists()
|
||||
}
|
||||
|
||||
pub fn run_in_tmux() -> ! {
|
||||
let tmux = utils::which("tmux").expect("Could not find tmux");
|
||||
let tmux = which("tmux").expect("Could not find tmux");
|
||||
let err = Command::new(tmux)
|
||||
.args(&[
|
||||
"new-session",
|
||||
|
||||
26
src/utils.rs
26
src/utils.rs
@@ -22,17 +22,27 @@ impl Check for ExitStatus {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn is_ancestor(ancestor: &Path, path: &Path) -> bool {
|
||||
let mut p = path;
|
||||
while let Some(parent) = p.parent() {
|
||||
if parent == ancestor {
|
||||
return true;
|
||||
}
|
||||
|
||||
p = parent;
|
||||
pub trait PathExt
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn if_exists(self) -> Option<Self>;
|
||||
fn is_descendant_of(&self, &Path) -> bool;
|
||||
}
|
||||
|
||||
impl PathExt for PathBuf {
|
||||
fn if_exists(self) -> Option<Self> {
|
||||
if self.exists() {
|
||||
Some(self)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
fn is_descendant_of(&self, ancestor: &Path) -> bool {
|
||||
self.iter().zip(ancestor.iter()).all(|(a, b)| a == b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
|
||||
|
||||
41
src/vim.rs
41
src/vim.rs
@@ -1,3 +1,4 @@
|
||||
use super::utils::PathExt;
|
||||
use directories::BaseDirs;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
@@ -34,39 +35,17 @@ impl PluginFramework {
|
||||
}
|
||||
|
||||
pub fn vimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
||||
{
|
||||
let vimrc = base_dirs.home_dir().join(".vimrc");
|
||||
if vimrc.exists() {
|
||||
return Some(vimrc);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let vimrc = base_dirs.home_dir().join(".vim/vimrc");
|
||||
if vimrc.exists() {
|
||||
return Some(vimrc);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
base_dirs
|
||||
.home_dir()
|
||||
.join(".vimrc")
|
||||
.if_exists()
|
||||
.or_else(|| base_dirs.home_dir().join(".vim/vimrc").if_exists())
|
||||
}
|
||||
|
||||
pub fn nvimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
||||
{
|
||||
let nvimrc = base_dirs.config_dir().join("nvim/init.vim");
|
||||
#[cfg(unix)]
|
||||
return base_dirs.config_dir().join("nvim/init.vim").if_exists();
|
||||
|
||||
if nvimrc.exists() {
|
||||
return Some(nvimrc);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let nvimrc = base_dirs.cache_dir().join("nvim/init.vim");
|
||||
|
||||
if nvimrc.exists() {
|
||||
return Some(nvimrc);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
#[cfg(windows)]
|
||||
return base_dirs.cache_dir().join("nvim/init.vim").if_exists();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user