Use a vim script to upgrade vim (#451)

This commit is contained in:
Roey Darwish Dror
2020-06-28 08:33:40 +03:00
committed by GitHub
parent 3b9d09c741
commit 5fb9b41771
4 changed files with 49 additions and 71 deletions

View File

@@ -588,6 +588,10 @@ impl Config {
&& get_deprecated!(&self.config_file, predefined_git_repos, git, pull_predefined).unwrap_or(true) && get_deprecated!(&self.config_file, predefined_git_repos, git, pull_predefined).unwrap_or(true)
} }
pub fn verbose(&self) -> bool {
self.opt.verbose
}
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
str_value!(linux, emerge_sync_flags); str_value!(linux, emerge_sync_flags);

View File

@@ -318,8 +318,8 @@ fn run() -> Result<()> {
} }
if config.should_run(Step::Vim) { if config.should_run(Step::Vim) {
runner.execute("vim", || vim::upgrade_vim(&base_dirs, run_type, config.cleanup()))?; runner.execute("vim", || vim::upgrade_vim(&base_dirs, &ctx))?;
runner.execute("Neovim", || vim::upgrade_neovim(&base_dirs, run_type, config.cleanup()))?; runner.execute("Neovim", || vim::upgrade_neovim(&base_dirs, &ctx))?;
runner.execute("voom", || vim::run_voom(&base_dirs, run_type))?; runner.execute("voom", || vim::run_voom(&base_dirs, run_type))?;
} }

16
src/steps/upgrade.vim Normal file
View File

@@ -0,0 +1,16 @@
if exists(":NeoBundleUpdate")
echo NeoBundle
NeoBundleUpdate
endif
if exists(":PluginUpdate")
echo "Plugin"
PluginUpdate
endif
if exists(":PlugUpgrade")
echo "Plug"
PlugUpgrade
PlugClean
PlugUpdate
endif

View File

@@ -3,55 +3,19 @@ use anyhow::Result;
use crate::executor::{CommandExt, ExecutorOutput, RunType}; use crate::executor::{CommandExt, ExecutorOutput, RunType};
use crate::terminal::print_separator; use crate::terminal::print_separator;
use crate::utils::{require, require_option, PathExt}; use crate::{
execution_context::ExecutionContext,
utils::{require, require_option, PathExt},
};
use directories::BaseDirs; use directories::BaseDirs;
use log::debug;
use std::path::PathBuf; use std::path::PathBuf;
use std::{ use std::{
fs,
io::{self, Write}, io::{self, Write},
process::Command, process::Command,
}; };
#[derive(Debug, Clone, Copy)] const UPGRADE_VIM: &str = include_str!("upgrade.vim");
pub enum PluginFramework {
Plug,
Vundle,
NeoBundle,
Dein,
}
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 if content.contains("dein#begin") {
Some(PluginFramework::Dein)
} else {
None
}
}
pub fn upgrade_command(self, cleanup: bool) -> &'static str {
match self {
PluginFramework::NeoBundle => "NeoBundleUpdate",
PluginFramework::Vundle => "PluginUpdate",
PluginFramework::Plug => {
if cleanup {
"PlugUpgrade | PlugClean | PlugUpdate"
} else {
"PlugUpgrade | PlugUpdate"
}
}
PluginFramework::Dein => "call dein#install() | call dein#update()",
}
}
}
pub fn vimrc(base_dirs: &BaseDirs) -> Option<PathBuf> { pub fn vimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
base_dirs base_dirs
@@ -69,33 +33,29 @@ fn nvimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
return base_dirs.cache_dir().join("nvim/init.vim").if_exists(); return base_dirs.cache_dir().join("nvim/init.vim").if_exists();
} }
fn upgrade( fn upgrade(vim: &PathBuf, vimrc: &PathBuf, ctx: &ExecutionContext) -> Result<()> {
vim: &PathBuf, let mut tempfile = tempfile::NamedTempFile::new()?;
vimrc: &PathBuf, tempfile.write_all(UPGRADE_VIM.as_bytes())?;
plugin_framework: PluginFramework, debug!("Wrote vim script to {:?}", tempfile.path());
run_type: RunType,
cleanup: bool, let output = ctx
) -> Result<()> { .run_type()
let output = run_type
.execute(&vim) .execute(&vim)
.args(&["-N", "-u"]) .args(&["-u"])
.arg(vimrc) .arg(vimrc)
.args(&[ .args(&["-U", "NONE", "-V1", "-nNesS"])
"-c", .arg(tempfile.path())
plugin_framework.upgrade_command(cleanup),
"-c",
"quitall",
"-e",
"-s",
"-V1",
])
.output()?; .output()?;
if let ExecutorOutput::Wet(output) = output { if let ExecutorOutput::Wet(output) = output {
let status = output.status; let status = output.status;
if !status.success() {
if !status.success() || ctx.config().verbose() {
io::stdout().write(&output.stdout).ok(); io::stdout().write(&output.stdout).ok();
io::stderr().write(&output.stderr).ok(); io::stderr().write(&output.stderr).ok();
}
if !status.success() {
return Err(TopgradeError::ProcessFailed(status).into()); return Err(TopgradeError::ProcessFailed(status).into());
} else { } else {
println!("Plugins upgraded") println!("Plugins upgraded")
@@ -105,7 +65,7 @@ fn upgrade(
Ok(()) Ok(())
} }
pub fn upgrade_vim(base_dirs: &BaseDirs, run_type: RunType, cleanup: bool) -> Result<()> { pub fn upgrade_vim(base_dirs: &BaseDirs, ctx: &ExecutionContext) -> Result<()> {
let vim = require("vim")?; let vim = require("vim")?;
let output = Command::new(&vim).arg("--version").check_output()?; let output = Command::new(&vim).arg("--version").check_output()?;
@@ -114,19 +74,17 @@ pub fn upgrade_vim(base_dirs: &BaseDirs, run_type: RunType, cleanup: bool) -> Re
} }
let vimrc = require_option(vimrc(&base_dirs))?; let vimrc = require_option(vimrc(&base_dirs))?;
let plugin_framework = require_option(PluginFramework::detect(&vimrc))?;
print_separator(&format!("Vim ({:?})", plugin_framework)); print_separator("Vim");
upgrade(&vim, &vimrc, plugin_framework, run_type, cleanup) upgrade(&vim, &vimrc, ctx)
} }
pub fn upgrade_neovim(base_dirs: &BaseDirs, run_type: RunType, cleanup: bool) -> Result<()> { pub fn upgrade_neovim(base_dirs: &BaseDirs, ctx: &ExecutionContext) -> Result<()> {
let nvim = require("nvim")?; let nvim = require("nvim")?;
let nvimrc = require_option(nvimrc(&base_dirs))?; let nvimrc = require_option(nvimrc(&base_dirs))?;
let plugin_framework = require_option(PluginFramework::detect(&nvimrc))?;
print_separator(&format!("Neovim ({:?})", plugin_framework)); print_separator("Neovim");
upgrade(&nvim, &nvimrc, plugin_framework, run_type, cleanup) upgrade(&nvim, &nvimrc, ctx)
} }
pub fn run_voom(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> { pub fn run_voom(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {