diff --git a/README.md b/README.md index 8d6f7e4..23a4506 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -spawncamping-bears +userscript hooks ================== -Userscripts I made over the years, and used to be up on userscripts.org +These are some hooks that make updating the userscripts a bit less of a chore + +###Installation + +Simply run install-hooks from this directory, which will symlink all hooks to .git/hooks + diff --git a/install-hooks b/install-hooks new file mode 100755 index 0000000..473718e --- /dev/null +++ b/install-hooks @@ -0,0 +1,8 @@ +#!/bin/bash + +echo "Adding hooks" + +shopt -s nullglob +for hook in *.hook; do + install -vm755 "$(pwd)/${hook}" ".git/hooks/${hook%.hook}" +done diff --git a/pre-commit.hook b/pre-commit.hook new file mode 100755 index 0000000..198faf1 --- /dev/null +++ b/pre-commit.hook @@ -0,0 +1,20 @@ +#!/bin/bash + + +## Check commit for userscripts and run update-metablocks + +for path in $(git diff --name-only --cached); do + if [[ $path =~ ./*user.js$ ]]; then + echo -e "\033[0;32m *** Generating Meta-file *** \033[0m" + + ./update-metablocks -w $path + git add -v $path ${path%user.js}meta.js + fi +done + +for path in $(git diff --name-only --cached); do + if [[ $path =~ ./*.hook$ ]]; then + echo -e "\033[0;32m *** Updating Hooks *** \033[0m" + ./install-hooks + fi +done diff --git a/update-metablocks b/update-metablocks new file mode 100755 index 0000000..0314417 --- /dev/null +++ b/update-metablocks @@ -0,0 +1,148 @@ +#!/bin/sh + +PROGRAM='update-metablocks'; +VERSION='1.2.3'; + +LICENCE=' +The MIT License (MIT) + +Copyright (c) 2014 Joost Bremmer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +' + +HELPMSG=" +################################################################### +## ## +## ${PROGRAM} ${VERSION}: ## +## ## +## writes userscript metablock data to *.meta.js files. ## +## ## +## Takes as arguments: ## +## -w: Write to file(s), without this it will do a ## +## --write: dryrun and outputs to stdout ## +## ## +## --help: Prints a help message. ## +## ## +## -o: Optional output file, default is inputfile.meta.js ## +## --output: ## +## ## +## --licence: Prints MIT Licence ## +## ## +## --version: Print the current version ## +## ## +## file: Input userscript file. ## +## ## +################################################################### +" + +DRYRUN=true; +INPUTFILE=""; +OUTPUTFILE=""; +OUTPUTFILEBOOL=false; +DATE=`date +%d-%m-%Y`; + +writeoutput () { + if $DRYRUN; then + if [[ $(egrep "\@date" ${INPUTFILE}) ]]; then + cat ${INPUTFILE} | sed -r "s/[0-9]{2}\-[0-9]{2}\-[0-9]{4}/$DATE/"| \ + egrep -B 100 "==/UserScript==" ; + echo ""; + else + cat ${INPUTFILE} | sed -r "/\@version/a // @date $DATE\ " |\ + egrep -B 100 "==/UserScript==" ; + fi + + else + echo "Writing metablock from \"${INPUTFILE}\" into \"${OUTPUTFILE}\""; + + if [[ $(egrep "\@date" ${INPUTFILE}) ]]; then + sed -i -r "s/[0-9]{2}\-[0-9]{2}\-[0-9]{4}/$DATE/" "${INPUTFILE}"; + else + sed -i -r "/\@version/a // @date $DATE \ " "${INPUTFILE}"; + fi + + egrep -B 100 "==/UserScript==" ${INPUTFILE} > ${OUTPUTFILE} 2>/dev/null; + fi + +} + + +if test $# -eq 0; then + echo "No arguments specified!"; + echo "${HELPMSG}"; +fi + + +while test $# -gt 0; do + + case $1 in + -w|--write) + DRYRUN=false; + shift 1; + ;; + + --help) + echo "$HELPMSG"; + exit 0; + ;; + + -o|--output) + shift 1; + if test $# -gt 0; then + OUTPUTFILE=$1; + OUTPUTFILEBOOL=true; + else + echo "no outputfile specified"; + echo "$HELPMSG"; + exit 1; + fi + shift 1; + ;; + + --licence) + echo "$LICENCE"; + exit 0; + ;; + + --version) + echo "$VERSION"; + exit 0; + ;; + *) + if test $# -gt 0; then + INPUTFILE="$1" + + if [[ $OUTPUTFILEBOOL == false ]]; then + OUTPUTFILE=$(echo "$INPUTFILE" | sed 's/user/meta/g'); + fi + writeoutput; + + else + echo "No files given!"; + echo "$HELPMSG"; + exit 1; + fi + shift 1; + ;; + esac + +done + +#vim: set ts=2 sw=2