refactor: simplify dotfiles configuration
check / validate (push) Canceled after 0s

This commit is contained in:
2026-08-12 15:57:01 +00:00
parent 56b90dad19
commit 2928ef3bc0
97 changed files with 740 additions and 5610 deletions
+72 -71
View File
@@ -1,87 +1,88 @@
""" cross platform installation script
"""
"""Install the Vim 9 configuration on Windows."""
from pathlib import Path
from shutil import which
import subprocess
import os
import time
import urllib.request
from os.path import expanduser, join
from shutil import which
home = expanduser("~")
app_name = 'dotfiles'
APP_PATH = join(home, '.' + app_name)
REPO_URL = 'https://github.com/Donaldttt/' + app_name
REPO_BRANCH = 'main'
VIM_RTPATH = join(home, '.vim')
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 exist(app):
return which(app) is not None
def program_must_exist(app):
if not exist(app):
fatal_error("must be installed to continue")
def run(command, cwd=None):
subprocess.run(command, cwd=cwd, check=True)
def fatal_error(msg):
print('Error:', msg)
exit()
def msg(msg):
print(msg)
def require(program):
if which(program) is None:
raise SystemExit(f"Error: {program} must be installed.")
def execute(cmd, cwd=None):
out = subprocess.check_output(
cmd,
stderr=subprocess.STDOUT,
shell=True,
cwd=None
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),
]
)
print(out)
def sync_repo(repo_path, repo_url, repo_branch, repo_name):
if not os.path.exists(repo_path):
msg('Trying to clone ' + repo_name)
cmd = "git clone -b {} {} {}".format(repo_branch, repo_url, repo_path)
execute(cmd)
else:
msg('Trying to update ' + repo_name)
cmd = "git pull origin {}".format(repo_branch)
execute(cmd, cwd=APP_PATH)
def setup_vimplug():
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.")
vimplug_path = VIM_RTPATH + "\\autoload\\plug.vim"
if not os.path.isdir(os.path.dirname(vimplug_path)):
os.makedirs(os.path.dirname(vimplug_path))
urllib.request.urlretrieve("https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim", vimplug_path)
print("Please execute :PlugInstall in vim")
def setup_nvim():
if exist('nvim'):
nvim_dir = home + '\\AppData\\Local\\nvim\\'
nvim_config = nvim_dir + 'init.vim'
if not os.path.isdir(nvim_dir):
os.mkdir(nvim_dir)
msg('Setting up neovim config')
with open(nvim_config, 'w') as f:
f.write('''set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath=&runtimepath
source ~/.vimrc''')
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 setup_vim():
if not exist('vim') and not exist('nvim'):
fatal_error('Error: vim or nvim has to be installed.')
program_must_exist('git')
sync_repo(APP_PATH, REPO_URL, REPO_BRANCH, app_name)
path = join(home, '.vimrc')
if os.path.exists(path):
os.remove(path)
os.symlink(join(APP_PATH, '.vimrc'), join(home, '.vimrc'))
path = join(home, '.vimrc.bundles')
if os.path.exists(path):
os.remove(path)
os.symlink(join(APP_PATH, '.vimrc.bundles'), join(home, '.vimrc.bundles'))
setup_nvim()
setup_vimplug()
msg("Done!")
setup_vim()
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()