Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Vim (fixes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-darwish committed Jun 7, 2018
1 parent ef69dc0 commit c490b2c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod git;
mod report;
mod steps;
mod terminal;
mod vim;

use failure::Error;
use git::Git;
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
33 changes: 33 additions & 0 deletions src/vim.rs
Original file line number Diff line number Diff line change
@@ -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<PluginFramework> {
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",
}
}
}

0 comments on commit c490b2c

Please sign in to comment.