From 2d69e8a2fe7147480d8c350895cc61afbe0dcaff Mon Sep 17 00:00:00 2001 From: Erich Smith Date: Tue, 1 May 2012 22:36:21 -0400 Subject: [PATCH] update composure.sh automatically version shell functions with git: * treat all shell functions as micro shell scripts * store functions as .sh files in a ~/.composure repo * revise () edits functions from last commit state * re-source functions from ~/.composure at any time, in any shell numerous fixes and refactorings --- lib/composure.sh | 125 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 38 deletions(-) diff --git a/lib/composure.sh b/lib/composure.sh index b4b740b5..359ab2cf 100755 --- a/lib/composure.sh +++ b/lib/composure.sh @@ -33,41 +33,52 @@ source_composure () draft () { - about wraps last command into a new function - param 1: name to give function - example $ ls - example $ draft list - example $ list - local name=$1 - eval 'function ' $name ' { ' $(fc -ln -1) '; }' + about wraps last command into a new function + param 1: name to give function + example $ ls + example $ draft list + example $ list + local func=$1 + eval 'function ' $func ' { ' $(fc -ln -1) '; }' + local file=$(mktemp /tmp/draft.XXXX) + declare -f $func > $file + transcribe $func $file draft + rm $file 2>/dev/null } - write () + transcribe () { - about prints function declaration to stdout - param name of function or functions, separated by spaces - example $ write myfunction - example $ write func1 func2 func3 > ~/funcs.sh - local func - for func in $* - do - # trim trailing semicolons generated by declare -f - declare -f $func | sed "s/^\(.*\);$/\1/" - echo - done - } + about store function in ~/.composure git repository + param 1: function name + param 2: file containing function + param 3: operation label + example $ transcribe myfunc /tmp/myfunc.sh 'scooby-doo version' + example stores your function changes with: + example master 7a7e524 scooby-doo version myfunc + local func=$1 + local file=$2 + local operation="$3" - revise () - { - about loads function into editor for revision - param name of function or functions, separated by spaces - example $ revise myfunction - example $ revise func1 func2 func3 - local temp=$(mktemp /tmp/revise.XXXX) - write $* > $temp - $EDITOR $temp - eval "$(cat $temp)" - rm $temp + if git --version >/dev/null 2>&1 + then + if [ -d ~/.composure ] + then + ( + cd ~/.composure + if git rev-parse 2>/dev/null + then + if [ ! -f $file ] + then + echo "Oops! Couldn't find $file to version it for you..." + return + fi + cp $file ~/.composure/$func.sh + git add --all . + git commit -m "$operation $func" + fi + ) + fi + fi } metafor () @@ -77,7 +88,7 @@ source_composure () param 2: meta keyword example $ metafor reference example local func=$1 keyword=$2 - write $func | sed -n "s/^ *$keyword \([^([].*\)$/\1/p" + declare -f $func | sed -n "s/;$//;s/^ *$keyword \([^([].*\)*$/\1/p" } reference () @@ -89,7 +100,7 @@ source_composure () printline () { - local metadata=$1 lhs=${2:- } + local metadata=$1 leftcol=${2:- } rightcol if [[ -z "$metadata" ]] then @@ -97,10 +108,9 @@ source_composure () fi OLD=$IFS; IFS=$'\n' - local line - for line in $metadata + for rightcol in $metadata do - printf "%-20s%s\n" $lhs $line + printf "%-20s%s\n" $leftcol $rightcol done IFS=$OLD } @@ -125,8 +135,6 @@ source_composure () echo "examples:" printline "$examples" fi - - unset printline } if [[ -n "$1" ]] @@ -142,6 +150,30 @@ source_composure () unset help printline } + revise () + { + about loads function into editor for revision + param 1: name of function + example $ revise myfunction + + local func=$1 + local temp=$(mktemp /tmp/revise.XXXX) + + # populate tempfile... + if [ -f ~/.composure/$func.sh ]; then + # ...with contents of latest git revision... + cat ~/.composure/$func.sh >> $temp + else + # ...or from ENV if not previously versioned + declare -f $func >> $temp + fi + $EDITOR $temp + source $temp + + transcribe $func $temp revise + rm $temp + } + } install_composure () @@ -178,6 +210,23 @@ install_composure () echo "source $DIR/$(basename $0)" >> ~/.bashrc fi + # prepare git repo + if git --version >/dev/null 2>&1 + then + if [ ! -d ~/.composure ] + then + ( + echo 'creating git repository for your functions...' + mkdir ~/.composure + cd ~/.composure + git init + echo "composure stores your function definitions here" > README.txt + git add README.txt + git commit -m 'initial commit' + ) + fi + fi + echo 'composure installed.' }