2019-12-11 23:05:38 +02:00
|
|
|
use crate::error::{SkipStep, TopgradeError};
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
2022-10-26 23:27:59 +02:00
|
|
|
use crate::executor::{CommandExt, Executor, ExecutorOutput, RunType};
|
2018-12-15 21:52:21 +02:00
|
|
|
use crate::terminal::print_separator;
|
2020-06-28 08:33:40 +03:00
|
|
|
use crate::{
|
|
|
|
|
execution_context::ExecutionContext,
|
2020-08-21 23:04:36 +03:00
|
|
|
utils::{require, PathExt},
|
2020-06-28 08:33:40 +03:00
|
|
|
};
|
2018-07-07 02:18:19 +03:00
|
|
|
use directories::BaseDirs;
|
2020-06-28 08:33:40 +03:00
|
|
|
use log::debug;
|
2022-10-26 23:27:59 +02:00
|
|
|
use std::path::PathBuf;
|
2019-05-16 11:08:52 +03:00
|
|
|
use std::{
|
|
|
|
|
io::{self, Write},
|
2019-05-21 16:39:13 +03:00
|
|
|
process::Command,
|
2019-05-16 11:08:52 +03:00
|
|
|
};
|
2018-06-07 08:51:16 +03:00
|
|
|
|
2020-06-28 08:33:40 +03:00
|
|
|
const UPGRADE_VIM: &str = include_str!("upgrade.vim");
|
2018-06-07 09:21:44 +03:00
|
|
|
|
2020-08-21 23:04:36 +03:00
|
|
|
pub fn vimrc(base_dirs: &BaseDirs) -> Result<PathBuf> {
|
2018-07-07 09:18:53 +03:00
|
|
|
base_dirs
|
|
|
|
|
.home_dir()
|
|
|
|
|
.join(".vimrc")
|
2020-08-21 23:04:36 +03:00
|
|
|
.require()
|
|
|
|
|
.or_else(|_| base_dirs.home_dir().join(".vim/vimrc").require())
|
2018-06-07 09:21:44 +03:00
|
|
|
}
|
2018-07-04 09:51:19 +02:00
|
|
|
|
2020-08-21 23:04:36 +03:00
|
|
|
fn nvimrc(base_dirs: &BaseDirs) -> Result<PathBuf> {
|
2018-07-07 09:18:53 +03:00
|
|
|
#[cfg(unix)]
|
2021-11-06 06:06:10 +02:00
|
|
|
let base_dir =
|
2020-09-06 18:44:03 +02:00
|
|
|
// Bypass directories crate as nvim doesn't use the macOS-specific directories.
|
|
|
|
|
std::env::var_os("XDG_CONFIG_HOME").map_or_else(|| base_dirs.home_dir().join(".config"), PathBuf::from);
|
2018-07-04 09:51:19 +02:00
|
|
|
|
2018-07-07 09:18:53 +03:00
|
|
|
#[cfg(windows)]
|
2020-09-06 18:44:03 +02:00
|
|
|
let base_dir = base_dirs.cache_dir();
|
|
|
|
|
|
2021-02-10 23:32:39 -05:00
|
|
|
base_dir
|
|
|
|
|
.join("nvim/init.vim")
|
|
|
|
|
.require()
|
|
|
|
|
.or_else(|_| base_dir.join("nvim/init.lua").require())
|
2018-07-04 09:51:19 +02:00
|
|
|
}
|
2018-07-14 22:19:03 +03:00
|
|
|
|
2022-10-26 23:27:59 +02:00
|
|
|
fn upgrade_script() -> Result<tempfile::NamedTempFile> {
|
2020-06-28 08:33:40 +03:00
|
|
|
let mut tempfile = tempfile::NamedTempFile::new()?;
|
2020-07-21 09:09:37 +03:00
|
|
|
tempfile.write_all(UPGRADE_VIM.replace('\r', "").as_bytes())?;
|
2020-06-28 08:33:40 +03:00
|
|
|
debug!("Wrote vim script to {:?}", tempfile.path());
|
2022-10-26 23:27:59 +02:00
|
|
|
Ok(tempfile)
|
|
|
|
|
}
|
2020-06-28 08:33:40 +03:00
|
|
|
|
2022-10-26 23:27:59 +02:00
|
|
|
fn upgrade(command: &mut Executor, ctx: &ExecutionContext) -> Result<()> {
|
2021-11-06 06:06:10 +02:00
|
|
|
if ctx.config().force_vim_plug_update() {
|
|
|
|
|
command.env("TOPGRADE_FORCE_PLUGUPDATE", "true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let output = command.output()?;
|
2018-07-14 22:19:03 +03:00
|
|
|
|
2019-05-16 11:08:52 +03:00
|
|
|
if let ExecutorOutput::Wet(output) = output {
|
|
|
|
|
let status = output.status;
|
2020-06-28 08:33:40 +03:00
|
|
|
|
|
|
|
|
if !status.success() || ctx.config().verbose() {
|
2019-05-16 11:08:52 +03:00
|
|
|
io::stdout().write(&output.stdout).ok();
|
|
|
|
|
io::stderr().write(&output.stderr).ok();
|
2020-06-28 08:33:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !status.success() {
|
2019-12-11 23:05:38 +02:00
|
|
|
return Err(TopgradeError::ProcessFailed(status).into());
|
2019-05-16 11:08:52 +03:00
|
|
|
} else {
|
|
|
|
|
println!("Plugins upgraded")
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-14 22:19:03 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2018-08-19 14:45:23 +03:00
|
|
|
|
2022-04-23 15:39:58 +03:00
|
|
|
pub fn upgrade_ultimate_vimrc(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let config_dir = ctx.base_dirs().home_dir().join(".vim_runtime").require()?;
|
|
|
|
|
let git = require("git")?;
|
|
|
|
|
let python = require("python3")?;
|
|
|
|
|
let update_plugins = config_dir.join("update_plugins.py").require()?;
|
|
|
|
|
|
|
|
|
|
print_separator("The Ultimate vimrc");
|
|
|
|
|
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(&git)
|
|
|
|
|
.current_dir(&config_dir)
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["reset", "--hard"])
|
2022-04-23 15:39:58 +03:00
|
|
|
.check_run()?;
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(&git)
|
|
|
|
|
.current_dir(&config_dir)
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["clean", "-d", "--force"])
|
2022-04-23 15:39:58 +03:00
|
|
|
.check_run()?;
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(&git)
|
|
|
|
|
.current_dir(&config_dir)
|
2022-10-23 11:34:30 +00:00
|
|
|
.args(["pull", "--rebase"])
|
2022-04-23 15:39:58 +03:00
|
|
|
.check_run()?;
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(python)
|
|
|
|
|
.current_dir(config_dir)
|
|
|
|
|
.arg(update_plugins)
|
|
|
|
|
.check_run()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 08:33:40 +03:00
|
|
|
pub fn upgrade_vim(base_dirs: &BaseDirs, ctx: &ExecutionContext) -> Result<()> {
|
2019-03-10 21:48:49 +02:00
|
|
|
let vim = require("vim")?;
|
2019-05-21 16:39:13 +03:00
|
|
|
|
|
|
|
|
let output = Command::new(&vim).arg("--version").check_output()?;
|
|
|
|
|
if !output.starts_with("VIM") {
|
2022-10-26 23:27:59 +02:00
|
|
|
return Err(SkipStep(String::from("vim binary might be actually nvim")).into());
|
2019-05-21 16:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
2021-09-02 07:27:09 +03:00
|
|
|
let vimrc = vimrc(base_dirs)?;
|
2018-08-19 14:45:23 +03:00
|
|
|
|
2020-06-28 08:33:40 +03:00
|
|
|
print_separator("Vim");
|
2022-10-26 23:27:59 +02:00
|
|
|
upgrade(
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(&vim)
|
|
|
|
|
.args(&["-u"])
|
|
|
|
|
.arg(vimrc)
|
|
|
|
|
.args(&["-U", "NONE", "-V1", "-nNesS"])
|
|
|
|
|
.arg(upgrade_script()?.path()),
|
|
|
|
|
ctx,
|
|
|
|
|
)
|
2018-08-19 14:45:23 +03:00
|
|
|
}
|
|
|
|
|
|
2020-06-28 08:33:40 +03:00
|
|
|
pub fn upgrade_neovim(base_dirs: &BaseDirs, ctx: &ExecutionContext) -> Result<()> {
|
2019-03-10 21:48:49 +02:00
|
|
|
let nvim = require("nvim")?;
|
2021-09-02 07:27:09 +03:00
|
|
|
let nvimrc = nvimrc(base_dirs)?;
|
2018-08-19 14:45:23 +03:00
|
|
|
|
2020-06-28 08:33:40 +03:00
|
|
|
print_separator("Neovim");
|
2022-10-26 23:27:59 +02:00
|
|
|
upgrade(
|
|
|
|
|
ctx.run_type()
|
|
|
|
|
.execute(&nvim)
|
|
|
|
|
.args(&["-u"])
|
|
|
|
|
.arg(nvimrc)
|
|
|
|
|
.args(&["--headless", "-V1", "-nS"])
|
|
|
|
|
.arg(upgrade_script()?.path()),
|
|
|
|
|
ctx,
|
|
|
|
|
)
|
2018-08-19 14:45:23 +03:00
|
|
|
}
|
2019-10-05 10:19:07 -07:00
|
|
|
|
2019-12-11 23:05:38 +02:00
|
|
|
pub fn run_voom(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
|
2019-10-05 10:19:07 -07:00
|
|
|
let voom = require("voom")?;
|
|
|
|
|
|
|
|
|
|
print_separator("voom");
|
|
|
|
|
|
|
|
|
|
run_type.execute(voom).arg("update").check_run()
|
|
|
|
|
}
|