2018-07-14 22:19:03 +03:00
|
|
|
use super::utils::{Check, PathExt};
|
2018-07-07 02:18:19 +03:00
|
|
|
use directories::BaseDirs;
|
2018-07-14 22:19:03 +03:00
|
|
|
use failure;
|
2018-06-07 08:51:16 +03:00
|
|
|
use std::fs;
|
|
|
|
|
use std::path::PathBuf;
|
2018-07-14 22:19:03 +03:00
|
|
|
use std::process::Command;
|
2018-06-07 08:51:16 +03:00
|
|
|
|
|
|
|
|
#[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",
|
2018-07-14 17:20:55 +03:00
|
|
|
PluginFramework::Plug => "PlugUpgrade | PlugUpdate",
|
2018-06-07 08:51:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-07 09:21:44 +03:00
|
|
|
|
2018-07-07 02:18:19 +03:00
|
|
|
pub fn vimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
2018-07-07 09:18:53 +03:00
|
|
|
base_dirs
|
|
|
|
|
.home_dir()
|
|
|
|
|
.join(".vimrc")
|
|
|
|
|
.if_exists()
|
|
|
|
|
.or_else(|| base_dirs.home_dir().join(".vim/vimrc").if_exists())
|
2018-06-07 09:21:44 +03:00
|
|
|
}
|
2018-07-04 09:51:19 +02:00
|
|
|
|
2018-07-07 02:18:19 +03:00
|
|
|
pub fn nvimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
|
2018-07-07 09:18:53 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
return base_dirs.config_dir().join("nvim/init.vim").if_exists();
|
2018-07-04 09:51:19 +02:00
|
|
|
|
2018-07-07 09:18:53 +03:00
|
|
|
#[cfg(windows)]
|
|
|
|
|
return base_dirs.cache_dir().join("nvim/init.vim").if_exists();
|
2018-07-04 09:51:19 +02:00
|
|
|
}
|
2018-07-14 22:19:03 +03:00
|
|
|
|
|
|
|
|
pub fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: &PluginFramework) -> Result<(), failure::Error> {
|
|
|
|
|
Command::new(&vim)
|
|
|
|
|
.args(&[
|
|
|
|
|
"-N",
|
|
|
|
|
"-u",
|
|
|
|
|
vimrc.to_str().unwrap(),
|
|
|
|
|
"-c",
|
|
|
|
|
plugin_framework.upgrade_command(),
|
|
|
|
|
"-c",
|
|
|
|
|
"quitall",
|
|
|
|
|
"-e",
|
|
|
|
|
"-s",
|
|
|
|
|
"-V1",
|
|
|
|
|
])
|
|
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|