diff --git a/Cargo.lock b/Cargo.lock index 322a2329..7859404c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,17 +14,6 @@ dependencies = [ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "app_dirs" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "shell32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "atty" version = "0.2.10" @@ -168,15 +157,6 @@ dependencies = [ "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ole32-sys" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "proc-macro2" version = "0.4.6" @@ -256,15 +236,6 @@ dependencies = [ "syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "shell32-sys" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "shellexpand" version = "1.0.0" @@ -369,7 +340,6 @@ dependencies = [ name = "topgrade" version = "0.10.0" dependencies = [ - "app_dirs 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "directories 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -474,15 +444,9 @@ dependencies = [ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "xdg" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [metadata] "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -"checksum app_dirs 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e73a24bad9bd6a94d6395382a6c69fe071708ae4409f763c5475e14ee896313d" "checksum atty 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2fc4a1aa4c24c0718a250f0681885c1af91419d242f29eb8f2ab28502d80dbd1" "checksum backtrace 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dbdd17cd962b570302f5297aea8648d5923e22e555c2ed2d8b2e34eca646bf6d" "checksum backtrace-sys 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)" = "bff67d0c06556c0b8e6b5f090f0eac52d950d9dfd1d35ba04e4ca3543eaf6a7e" @@ -500,7 +464,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1" "checksum log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "61bd98ae7f7b754bc53dca7d44b604f733c6bba044ea6f41bc8d89272d8161d2" "checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" -"checksum ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c" "checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" @@ -512,7 +475,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "76d7ba1feafada44f2d38eed812bd2489a03c0f5abb975799251518b68848649" "checksum serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)" = "210e5a3b159c566d7527e9b22e44be73f2e0fcc330bb78fef4dbccb56d2e74c8" "checksum serde_derive 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)" = "dd724d68017ae3a7e63600ee4b2fdb3cad2158ffd1821d44aff4580f63e2b593" -"checksum shell32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9ee04b46101f57121c9da2b151988283b6beb79b34f5bb29a58ee48cb695122c" "checksum shellexpand 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de7a5b5a9142fd278a10e0209b021a1b85849352e6951f4f914735c976737564" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" @@ -540,4 +502,3 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767" -"checksum xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a66b7c2281ebde13cf4391d70d4c7e5946c3c25e72a7b859ca8f677dcd0b0c61" diff --git a/Cargo.toml b/Cargo.toml index 9383e576..c5f150d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,3 @@ log = "0.4.3" env_logger = "0.5.10" term_size = "0.3.1" termcolor = "0.3.6" -app_dirs = "1.2.1" diff --git a/src/config.rs b/src/config.rs index a02c399e..7e2765fe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use directories; +use directories::BaseDirs; use failure; use shellexpand; use std::collections::BTreeMap; @@ -15,8 +15,7 @@ pub struct Config { } impl Config { - pub fn read() -> Result { - let base_dirs = directories::BaseDirs::new().unwrap(); + pub fn read(base_dirs: &BaseDirs) -> Result { let config_path = base_dirs.config_dir().join("topgrade.toml"); if !config_path.exists() { return Ok(Default::default()); diff --git a/src/main.rs b/src/main.rs index 8573ad82..60f101eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ extern crate serde; extern crate shellexpand; #[macro_use] extern crate log; -extern crate app_dirs; extern crate env_logger; extern crate term_size; extern crate termcolor; @@ -41,16 +40,19 @@ use failure::Error; use git::{Git, Repositories}; use report::{Report, Reporter}; use std::env; -use std::env::home_dir; use std::process::exit; use steps::*; use terminal::Terminal; -use utils::{home_path, is_ancestor}; +use utils::is_ancestor; #[derive(Fail, Debug)] #[fail(display = "A step failed")] struct StepFailed; +#[derive(Fail, Debug)] +#[fail(display = "Cannot find the user base directories")] +struct NoBaseDirectories; + fn run() -> Result<(), Error> { let matches = App::new("Topgrade") .version(crate_version!()) @@ -71,10 +73,11 @@ fn run() -> Result<(), Error> { } env_logger::init(); + let base_dirs = directories::BaseDirs::new().ok_or(NoBaseDirectories)?; let git = Git::new(); let mut git_repos = Repositories::new(&git); let mut terminal = Terminal::new(); - let config = Config::read()?; + let config = Config::read(&base_dirs)?; let mut reports = Report::new(); #[cfg(target_os = "linux")] @@ -121,16 +124,16 @@ fn run() -> Result<(), Error> { } } - git_repos.insert(home_path(".emacs.d")); - git_repos.insert(home_path(".vim")); - git_repos.insert(home_path(".config/nvim")); + git_repos.insert(base_dirs.home_dir().join(".emacs.d")); + git_repos.insert(base_dirs.home_dir().join(".vim")); + git_repos.insert(base_dirs.home_dir().join(".config/nvim")); #[cfg(unix)] { - git_repos.insert(home_path(".zshrc")); - git_repos.insert(home_path(".oh-my-zsh")); - git_repos.insert(home_path(".tmux")); - git_repos.insert(home_path(".config/fish")); + git_repos.insert(base_dirs.home_dir().join(".zshrc")); + git_repos.insert(base_dirs.home_dir().join(".oh-my-zsh")); + git_repos.insert(base_dirs.home_dir().join(".tmux")); + git_repos.insert(base_dirs.home_dir().join(".config/fish")); } if let Some(custom_git_repos) = config.git_repos() { @@ -149,14 +152,14 @@ fn run() -> Result<(), Error> { #[cfg(unix)] { if let Some(zsh) = utils::which("zsh") { - if home_path(".zplug").exists() { + if base_dirs.home_dir().join(".zplug").exists() { terminal.print_separator("zplug"); unix::run_zplug(&zsh).report("zplug", &mut reports); } } if let Some(fish) = utils::which("fish") { - if home_path(".config/fish/functions/fisher.fish").exists() { + if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() { terminal.print_separator("fisherman"); unix::run_fisherman(&fish).report("fisherman", &mut reports); } @@ -173,14 +176,14 @@ fn run() -> Result<(), Error> { run_rustup(&rustup).report("rustup", &mut reports); } - let cargo_upgrade = home_path(".cargo/bin/cargo-install-update"); + let cargo_upgrade = base_dirs.home_dir().join(".cargo/bin/cargo-install-update"); if cargo_upgrade.exists() { terminal.print_separator("Cargo"); run_cargo_update(&cargo_upgrade).report("Cargo", &mut reports); } if let Some(emacs) = utils::which("emacs") { - let init_file = home_path(".emacs.d/init.el"); + let init_file = base_dirs.home_dir().join(".emacs.d/init.el"); if init_file.exists() { terminal.print_separator("Emacs"); run_emacs(&emacs, &init_file).report("Emacs", &mut reports); @@ -188,7 +191,7 @@ fn run() -> Result<(), Error> { } if let Some(vim) = utils::which("vim") { - if let Some(vimrc) = vim::vimrc() { + if let Some(vimrc) = vim::vimrc(&base_dirs) { if let Some(plugin_framework) = vim::PluginFramework::detect(&vimrc) { terminal.print_separator(&format!("Vim ({:?})", plugin_framework)); run_vim(&vim, &vimrc, plugin_framework.upgrade_command()).report("Vim", &mut reports); @@ -197,7 +200,7 @@ fn run() -> Result<(), Error> { } if let Some(nvim) = utils::which("nvim") { - if let Some(nvimrc) = vim::nvimrc() { + if let Some(nvimrc) = vim::nvimrc(&base_dirs) { if let Some(plugin_framework) = vim::PluginFramework::detect(&nvimrc) { terminal.print_separator(&format!("Neovim ({:?})", plugin_framework)); run_vim(&nvim, &nvimrc, plugin_framework.upgrade_command()).report("Neovim", &mut reports); @@ -207,7 +210,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(&home_dir().unwrap(), &npm_root) { + if is_ancestor(&base_dirs.home_dir(), &npm_root) { terminal.print_separator("Node Package Manager"); npm.upgrade().report("Node Package Manager", &mut reports); } diff --git a/src/utils.rs b/src/utils.rs index ac1e0ff1..e2fcdf1f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,4 @@ use failure::Error; -use std::env::home_dir; use std::ffi::OsStr; use std::fmt::Debug; use std::path::{Path, PathBuf}; @@ -23,13 +22,6 @@ impl Check for ExitStatus { } } } - -pub fn home_path(p: &str) -> PathBuf { - let mut path = home_dir().unwrap(); - path.push(p); - path -} - pub fn is_ancestor(ancestor: &Path, path: &Path) -> bool { let mut p = path; while let Some(parent) = p.parent() { diff --git a/src/vim.rs b/src/vim.rs index c8d75abd..20e4a657 100644 --- a/src/vim.rs +++ b/src/vim.rs @@ -1,5 +1,4 @@ -use super::home_path; -use app_dirs::*; +use directories::BaseDirs; use std::fs; use std::path::PathBuf; @@ -34,16 +33,16 @@ impl PluginFramework { } } -pub fn vimrc() -> Option { +pub fn vimrc(base_dirs: &BaseDirs) -> Option { { - let vimrc = home_path(".vimrc"); + let vimrc = base_dirs.home_dir().join(".vimrc"); if vimrc.exists() { return Some(vimrc); } } { - let vimrc = home_path(".vim/vimrc"); + let vimrc = base_dirs.home_dir().join(".vim/vimrc"); if vimrc.exists() { return Some(vimrc); } @@ -52,10 +51,9 @@ pub fn vimrc() -> Option { None } -pub fn nvimrc() -> Option { +pub fn nvimrc(base_dirs: &BaseDirs) -> Option { { - let mut nvimrc = get_data_root(AppDataType::UserConfig).unwrap(); - nvimrc.push("nvim/init.vim"); + let nvimrc = base_dirs.config_dir().join("nvim/init.vim"); if nvimrc.exists() { return Some(nvimrc); @@ -63,8 +61,7 @@ pub fn nvimrc() -> Option { } { - let mut nvimrc = get_data_root(AppDataType::UserCache).unwrap(); - nvimrc.push("nvim/init.vim"); + let nvimrc = base_dirs.cache_dir().join("nvim/init.vim"); if nvimrc.exists() { return Some(nvimrc);