Path overhaul
This commit is contained in:
@@ -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<Config, failure::Error> {
|
||||
let base_dirs = directories::BaseDirs::new().unwrap();
|
||||
pub fn read(base_dirs: &BaseDirs) -> Result<Config, failure::Error> {
|
||||
let config_path = base_dirs.config_dir().join("topgrade.toml");
|
||||
if !config_path.exists() {
|
||||
return Ok(Default::default());
|
||||
|
||||
39
src/main.rs
39
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);
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
17
src/vim.rs
17
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<PathBuf> {
|
||||
pub fn vimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
||||
{
|
||||
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<PathBuf> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn nvimrc() -> Option<PathBuf> {
|
||||
pub fn nvimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
||||
{
|
||||
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<PathBuf> {
|
||||
}
|
||||
|
||||
{
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user