You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
3.6 KiB
187 lines
3.6 KiB
7 years ago
|
#!/bin/sh
|
||
|
|
||
5 years ago
|
# Vim plugin manager
|
||
|
# Depends: git
|
||
|
|
||
7 years ago
|
#----
|
||
7 years ago
|
BASE="$XDG_CONFIG_HOME/vim/pack"
|
||
7 years ago
|
#----
|
||
|
DIR="$BASE/plugins"
|
||
|
LIST="$BASE/list"
|
||
7 years ago
|
START="$DIR/start"
|
||
|
OPT="$DIR/opt"
|
||
|
|
||
|
help() {
|
||
6 years ago
|
B=$(tput bold)
|
||
5 years ago
|
U=$(tput smul)
|
||
6 years ago
|
N=$(tput sgr0)
|
||
|
|
||
|
cat << EOF
|
||
|
${B}NAME${N}
|
||
5 years ago
|
vimplugin.sh - vim plugin manager
|
||
6 years ago
|
|
||
|
${B}SYNOPSIS${N}
|
||
5 years ago
|
${B}vimplugin.sh${N} [${U}OPTION${N}] [${U}COMMAND${N}] [<${U}ARG${N}>...]
|
||
|
|
||
|
${B}OPTIONS${N}
|
||
|
${B}-h${N} Display usage message and exit.
|
||
6 years ago
|
|
||
|
${B}COMMANDS${N}
|
||
|
${B}init${N}
|
||
|
Creates all the required directories/files.
|
||
|
|
||
5 years ago
|
${B}install${N} ${U}URL${N} <${U}LOCATION${N}>
|
||
|
Install a new plugin from a valid GitHub ${U}URL${N}.
|
||
|
Store this plugin in <${U}LOCATION${N}> (start/opt), default to start.
|
||
|
|
||
|
${B}list${N} <${U}NUMBER${N}>
|
||
|
Print all the installed plugins.
|
||
|
If <${U}NUMBER${N}> is provided, print only the plugin in that position.
|
||
6 years ago
|
|
||
|
${B}remove${N}
|
||
5 years ago
|
Remove plugin, pick from selection menu.
|
||
|
|
||
6 years ago
|
${B}update${N}
|
||
5 years ago
|
Install/update all plugins stored in the config file.
|
||
6 years ago
|
EOF
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
# Exit if no option is provided
|
||
|
[ "$#" -eq 0 ] && help && exit 1
|
||
|
|
||
|
SCRIPT="$(basename "$0")"
|
||
|
|
||
|
# Option handling
|
||
|
while getopts ':h?' opt; do
|
||
|
case $opt in
|
||
|
h)
|
||
|
help
|
||
|
exit 0
|
||
|
;;
|
||
|
\?)
|
||
|
echo "$SCRIPT: invalid option '$OPTARG'"
|
||
|
echo "Try '$SCRIPT -h' for more information."
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
done
|
||
|
|
||
7 years ago
|
init() {
|
||
6 years ago
|
sudo mkdir -p "$START"
|
||
|
sudo mkdir -p "$OPT"
|
||
|
sudo touch "$LIST"
|
||
|
if [ ! -s "$LIST" ]; then
|
||
7 years ago
|
# Append to empty file
|
||
6 years ago
|
sudo sh -c 'echo "start:" >> '"$LIST"
|
||
|
sudo sh -c 'echo "opt:" >> '"$LIST"
|
||
7 years ago
|
fi
|
||
7 years ago
|
}
|
||
|
|
||
|
install() {
|
||
7 years ago
|
# Check if correct git URL
|
||
6 years ago
|
if [ -z "$(echo "$1" | sed -nr \
|
||
7 years ago
|
's/^https:\/\/github\.com\/(.*\/.*).git$/\1/p')" ]; then
|
||
5 years ago
|
echo "$SCRIPT: url invalid: $1"
|
||
7 years ago
|
else
|
||
|
# cd to correct directory if called from the CLI
|
||
|
if [ -z "$2" ]; then
|
||
|
# Default is directory 'start'
|
||
6 years ago
|
cd "$START" || exit
|
||
7 years ago
|
elif [ "$2" = "start" ]; then
|
||
6 years ago
|
cd "$START" || exit
|
||
7 years ago
|
elif [ "$2" = "opt" ]; then
|
||
6 years ago
|
cd "$OPT" || exit
|
||
7 years ago
|
fi
|
||
|
|
||
6 years ago
|
REPO="$(basename "$1" .git)"
|
||
7 years ago
|
if [ -d "$REPO" ]; then
|
||
6 years ago
|
cd "$REPO" || exit
|
||
7 years ago
|
sudo git pull --force 1> /dev/null
|
||
|
cd ".."
|
||
|
echo "Updated: $REPO"
|
||
|
else
|
||
|
sudo git clone "$1" 2> /dev/null
|
||
|
|
||
|
# Add git URL to config file
|
||
|
if [ -z "$2" ]; then
|
||
|
# Append before 'opt:'
|
||
6 years ago
|
sudo sed -i '/opt:/ i '"$1" "$LIST"
|
||
7 years ago
|
elif [ "$2" = "start" ]; then
|
||
|
# Append before 'opt:'
|
||
6 years ago
|
sudo sed -i '/opt:/ i '"$1" "$LIST"
|
||
7 years ago
|
elif [ "$2" = "opt" ]; then
|
||
|
# Append at the end of the file
|
||
6 years ago
|
sudo sed -i '$ a '"$1" "$LIST"
|
||
7 years ago
|
fi
|
||
|
|
||
|
echo "Installed: $REPO"
|
||
|
fi
|
||
|
fi
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
list() {
|
||
|
if [ -z "$1" ]; then
|
||
|
echo "Listing all installed plugins.."
|
||
|
fi
|
||
7 years ago
|
|
||
5 years ago
|
PLUGIN="$(find "$DIR" -mindepth 2 -maxdepth 2 \
|
||
|
| sort | awk -F '/' '{ print $(NF-1)"/"$NF }')"
|
||
|
NUM=0
|
||
|
for p in $PLUGIN; do
|
||
|
NUM=$((NUM + 1))
|
||
7 years ago
|
|
||
5 years ago
|
if [ -z "$1" ]; then
|
||
|
echo "$NUM) $p"
|
||
|
elif [ "$NUM" = "$1" ]; then
|
||
|
echo "$p"
|
||
7 years ago
|
fi
|
||
5 years ago
|
done
|
||
7 years ago
|
}
|
||
|
|
||
|
remove() {
|
||
7 years ago
|
list
|
||
5 years ago
|
printf "Enter the number to remove: "
|
||
6 years ago
|
read -r OPTION
|
||
7 years ago
|
|
||
6 years ago
|
if [ -z "$(echo "$OPTION" | sed -nr 's/^([0-9]+)$/\1/p')" ]; then
|
||
7 years ago
|
echo "Please select a number"
|
||
|
else
|
||
6 years ago
|
TO_REMOVE="$(list "$OPTION")"
|
||
7 years ago
|
if [ -n "$TO_REMOVE" ]; then
|
||
6 years ago
|
sudo rm -rf "$DIR/$TO_REMOVE"
|
||
|
sudo sed -i '/'"$(basename "$TO_REMOVE")"'\.git/d' "$LIST"
|
||
5 years ago
|
echo "Removed plugin: $TO_REMOVE"
|
||
7 years ago
|
fi
|
||
|
fi
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
update() {
|
||
|
echo "Updating.."
|
||
|
|
||
|
init
|
||
|
|
||
|
cd "$START" || exit
|
||
|
while read -r l; do
|
||
|
if [ "$l" = "start:" ]; then
|
||
|
cd "$START" || exit
|
||
|
elif [ "$l" = "opt:" ]; then
|
||
|
cd "$OPT" || exit
|
||
|
else
|
||
|
install "$l" "script"
|
||
|
fi
|
||
|
done < "$LIST"
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
# Command handling
|
||
|
shift $((OPTIND - 1))
|
||
|
case "$1" in
|
||
|
init | install | list | remove | update)
|
||
5 years ago
|
"$@"
|
||
5 years ago
|
;;
|
||
|
*)
|
||
|
echo "$SCRIPT: invalid command '$1'"
|
||
|
echo "Try '$SCRIPT -h' for more information."
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|