Created
February 15, 2015 15:45
-
-
Save miekg/1283eb7f31bc60752f2d to your computer and use it in GitHub Desktop.
zbundle: bundle code and zip to have one unit of deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
getopts "s" show && shift | |
if [[ $show == "s" ]]; then | |
while read line; do | |
hex="$(echo $line | od -N4 -x | head -1)" | |
byte=(${hex}) | |
if [[ ${byte[1]} == "4b50" && ${byte[2]} == "0403" ]]; then | |
break | |
fi | |
fragment="$fragment$newline$line" | |
newline="\n" | |
done < ${1:-/dev/stdin} | |
echo -e $fragment | |
exit | |
fi | |
if [[ -z "$2" ]]; then | |
cat << EOF | |
$0 FRAGMENT FILE [FILES]... | |
$0 -s FILE.zip | |
Prefix a zip file created of FILEs with shell FRAGMENT. | |
The zip file is self extracting and after doing so it executes the shell fragment. | |
The name of the zip file is FILE.zip. | |
Note the first command of the script must be: | |
unzip -qo $0 2>/dev/null | |
When the -s flag is given the shell fragment is echoed on standard output. | |
EOF | |
exit | |
fi | |
fragment="$1" | |
shift | |
if ! grep -v '^#' "$fragment" | head -5 | grep -q 'unzip -qo \$0 2>/dev/null'; then | |
echo "$0: command 'unzip -qo \$0 2>/dev/null' should be at the top of the fragment" | |
exit | |
fi | |
cat $fragment <(zip - "$@") > "$1".zip && chmod +x "$1".zip | |
echo "$1".zip >&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment