From c490b2c108c1d1c5be7bfc3c22aa6e1667002340 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Thu, 7 Jun 2018 08:51:16 +0300 Subject: [PATCH] Vim (fixes #17) --- README.md | 4 ++++ src/main.rs | 14 +++++++++++++- src/steps.rs | 25 +++++++++++++++++++++++++ src/vim.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/vim.rs diff --git a/README.md b/README.md index 4cedada5..64cca89a 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ Just invoke `topgrade`. It will invoke the following steps: * *Unix*: Upgrade tmux plugins with [TPM](https://github.com/tmux-plugins/tpm) * Invoke Cargo [install-update](https://github.com/nabijaczleweli/cargo-update) * Upgrade Emacs packages +* Upgrade Vim packages. Works with the following plugin frameworks: + * [NeoBundle](https://github.com/Shougo/neobundle.vim) + * [Vundle](https://github.com/VundleVim/Vundle.vim) + * [Plug](https://github.com/junegunn/vim-plug) * Upgrade NPM globally installed packages * Upgrade Atom packages * Upgrade RubyGems globally installed packages diff --git a/src/main.rs b/src/main.rs index 78762193..9b2b88a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod git; mod report; mod steps; mod terminal; +mod vim; use failure::Error; use git::Git; @@ -110,7 +111,18 @@ fn main() -> Result<(), Error> { let init_file = home_path(".emacs.d/init.el"); if init_file.exists() { terminal.print_separator("Emacs"); - run_emacs(&emacs, &home_path(".emacs.d/init.el")).report("Emacs", &mut reports); + run_emacs(&emacs, &init_file).report("Emacs", &mut reports); + } + } + + if let Ok(vim) = which("Vim") { + let vimrc = home_path(".vimrc"); + if vimrc.exists() { + 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); + } } } diff --git a/src/steps.rs b/src/steps.rs index 6745f999..85e5a9a7 100644 --- a/src/steps.rs +++ b/src/steps.rs @@ -48,6 +48,31 @@ pub fn run_emacs(emacs: &PathBuf, init: &PathBuf) -> Result<(), failure::Error> Ok(()) } +pub fn run_vim( + vim: &PathBuf, + vimrc: &PathBuf, + upgrade_command: &str, +) -> Result<(), failure::Error> { + Command::new(&vim) + .args(&[ + "-N", + "-u", + vimrc.to_str().unwrap(), + "-c", + upgrade_command, + "-c", + "quitall", + "-e", + "-s", + "-V1", + ]) + .spawn()? + .wait()? + .check()?; + + Ok(()) +} + pub fn run_gem(gem: &PathBuf) -> Result<(), failure::Error> { Command::new(&gem) .args(&["update"]) diff --git a/src/vim.rs b/src/vim.rs new file mode 100644 index 00000000..501f2cff --- /dev/null +++ b/src/vim.rs @@ -0,0 +1,33 @@ +use std::fs; +use std::path::PathBuf; + +#[derive(Debug, Clone, Copy)] +pub enum PluginFramework { + Plug, + Vundle, + NeoBundle, +} + +impl PluginFramework { + pub fn detect(vimrc: &PathBuf) -> Option { + let content = fs::read_to_string(vimrc).ok()?; + + if content.contains("NeoBundle") { + Some(PluginFramework::NeoBundle) + } else if content.contains("Vundle") { + Some(PluginFramework::Vundle) + } else if content.contains("plug#begin") { + Some(PluginFramework::Plug) + } else { + None + } + } + + pub fn upgrade_command(self) -> &'static str { + match self { + PluginFramework::NeoBundle => "NeoBundleUpdate", + PluginFramework::Vundle => "PluginUpdate", + PluginFramework::Plug => "PlugUpdate", + } + } +}