From da9ea31430849b140c59929c650ff97d5a568ddb Mon Sep 17 00:00:00 2001 From: Guangxiong Lin Date: Sun, 12 Dec 2021 11:51:49 +0800 Subject: Use shell script instead of python to update vim package --- bin/vim-pack | 146 +++++++++++++++++------------------------------------------ 1 file changed, 42 insertions(+), 104 deletions(-) diff --git a/bin/vim-pack b/bin/vim-pack index 25bc165..65c7fb4 100755 --- a/bin/vim-pack +++ b/bin/vim-pack @@ -1,104 +1,42 @@ -#!/usr/bin/env python - -import os -import glob -import asyncio -import subprocess - -INTSTALL_DIR=os.path.expandvars("$HOME/.vim/pack/vendor") - -class Package(object): - def __init__(self, name, location, type='opt', posthook=None): - self.name = name - self.location = location - self.type = type - self.posthook = posthook - self.path = self.get_path() - - def fail(self): - print("%s : FAIL" % self.name) - - def get_path(self): - return os.path.expandvars("$HOME/.vim/pack/vendor/%s/%s" % (self.type, self.name)) - - async def update(self): - cmd = "cd %s && git pull" % self.path - print("%s : Updating ..." % self.name) - - if not os.path.exists(self.path): - os.mkdir(self.path) - cmd = "git clone --depth 1 %s %s" % (self.location, self.path) - if not os.path.isdir(self.path): - print("ERROR: package directory error: %s" % self.name) - self.fail() - return - - process = await asyncio.create_subprocess_shell( - cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, - ) - - stdout, stderr = await process.communicate() - - if process.returncode != 0: - print("ERROR: git command error") - print(stdout) - print(stderr) - self.fail() - return - - if self.posthook: - self.posthook() - - print("%s : Done" % (self.name)) - - return "" - -packages = [ - Package( - "nnn.vim", - "git@github.com:mcchrish/nnn.vim.git", - "opt", - ), -] - -def update_packages(packages): - tasks = [ package.update() for package in packages ] - loop = asyncio.get_event_loop() - commands = asyncio.gather(*tasks) - result = loop.run_until_complete(commands) - loop.close() - # print(result) - -def clean(packages): - paths = [ package.path for package in packages ] - to_clean = [ - path - for path in glob.glob(os.path.expandvars("$HOME/.vim/pack/vendor/*/*")) - if path not in paths - ] - - if not to_clean: - return - - print("The following directory will be cleaned: [y/N]") - print(to_clean) - if input() not in ["y", "Yes", "yes"]: - return - - cmd = ["rm", "-rf"] + to_clean - result = subprocess.run(cmd, capture_output=True, shell=False) - print(' '.join(cmd)) - if result.returncode != 0: - print("FAIL on cleaning the following directory:") - print(result.stdout) - print(result.stderr) - else: - print("DONE") - - -def main(): - update_packages(packages) - clean(packages) - -if __name__ == "__main__": - main() +#!/usr/bin/env bash + +# package_dirs records directories of all installed packages. It is used +# to help clean unused packages. +package_dirs=() + +update_package () { + name=$1 + remote_url=$2 + load_type=$3 + + package_dir="$HOME/.vim/pack/vendor/$load_type/$name" + package_dirs+=($package_dir) + if [ ! -d $package_dir ]; then + echo "$name : Installing ... " + mkdir -p $package_dir + git clone --depth 1 $remote_url $package_dir + else + cd $package_dir + echo "$name : Updating ... " + git pull && echo "$name : Done" || echo "$name : FAIL" + fi +} + +clean () { + for load_type in 'opt' 'start'; do + for package_name in $(ls $HOME/.vim/pack/vendor/$load_type); do + package_dir=$HOME/.vim/pack/vendor/$load_type/$package_name + if [[ ! " ${package_dirs[*]} " =~ " ${package_dir} " ]]; then + read -r -p "rm -rf $package_dir? [y/N]: " + response=${response,,} + if [[ "$response" =~ ^(yes|y)$ ]]; then + rm -rf $package_dir + fi + fi + done + done +} + +update_package "nnn.vim" "git@github.com:mcchrish/nnn.vim.git" "opt" + +clean -- cgit v1.2.3