89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
"""Install the Vim 9 configuration on Windows."""
|
|
|
|
from pathlib import Path
|
|
from shutil import which
|
|
import subprocess
|
|
import time
|
|
import urllib.request
|
|
|
|
|
|
HOME = Path.home()
|
|
SCRIPT_PATH = Path(__file__).resolve().parent
|
|
DEFAULT_APP_PATH = HOME / ".dotfiles"
|
|
APP_PATH = SCRIPT_PATH if (SCRIPT_PATH / ".git").exists() else DEFAULT_APP_PATH
|
|
REPO_URL = "https://github.com/Donaldttt/dotfiles.git"
|
|
REPO_BRANCH = "main"
|
|
VIM_PATH = HOME / "vimfiles"
|
|
VIMRC = HOME / "_vimrc"
|
|
|
|
|
|
def run(command, cwd=None):
|
|
subprocess.run(command, cwd=cwd, check=True)
|
|
|
|
|
|
def require(program):
|
|
if which(program) is None:
|
|
raise SystemExit(f"Error: {program} must be installed.")
|
|
|
|
|
|
def ensure_repo():
|
|
if (APP_PATH / ".git").exists():
|
|
return
|
|
if APP_PATH.exists():
|
|
raise SystemExit(f"Error: {APP_PATH} exists but is not a Git repository.")
|
|
print(f"Cloning dotfiles into {APP_PATH}...")
|
|
run(
|
|
[
|
|
"git",
|
|
"clone",
|
|
"--branch",
|
|
REPO_BRANCH,
|
|
"--depth",
|
|
"1",
|
|
REPO_URL,
|
|
str(APP_PATH),
|
|
]
|
|
)
|
|
|
|
|
|
def check_vim_version():
|
|
result = subprocess.run(
|
|
["vim", "--clean", "-Nu", "NONE", "-n", "-es", "+if v:version < 900 | cquit | endif", "+qall"]
|
|
)
|
|
if result.returncode:
|
|
raise SystemExit("Error: Vim 9 or newer is required.")
|
|
|
|
|
|
def install_vim_plug():
|
|
target = VIM_PATH / "autoload" / "plug.vim"
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
urllib.request.urlretrieve(
|
|
"https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim",
|
|
target,
|
|
)
|
|
|
|
|
|
def install_vimrc():
|
|
config_path = (APP_PATH / ".vimrc").as_posix().replace("'", "''")
|
|
source_line = f"execute 'source ' . fnameescape('{config_path}')"
|
|
if VIMRC.exists() and VIMRC.read_text(encoding="utf-8", errors="ignore").strip() != source_line:
|
|
backup = VIMRC.with_name(f"{VIMRC.name}.{int(time.time())}.backup")
|
|
VIMRC.replace(backup)
|
|
print(f"Backed up existing Vim config to {backup}")
|
|
VIMRC.write_text(source_line + "\n", encoding="utf-8")
|
|
|
|
|
|
def main():
|
|
require("git")
|
|
require("vim")
|
|
check_vim_version()
|
|
ensure_repo()
|
|
install_vimrc()
|
|
install_vim_plug()
|
|
run(["vim", "-Nu", str(APP_PATH / ".vimrc"), "+set nomore", "+PlugInstall --sync", "+qall"])
|
|
print("Vim configuration installed.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|