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 refactoringspull/118/head
parent
b7e429b1f8
commit
2d69e8a2fe
115
lib/composure.sh
115
lib/composure.sh
|
|
@ -38,36 +38,47 @@ source_composure ()
|
||||||
example $ ls
|
example $ ls
|
||||||
example $ draft list
|
example $ draft list
|
||||||
example $ list
|
example $ list
|
||||||
local name=$1
|
local func=$1
|
||||||
eval 'function ' $name ' { ' $(fc -ln -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
|
about store function in ~/.composure git repository
|
||||||
param name of function or functions, separated by spaces
|
param 1: function name
|
||||||
example $ write myfunction
|
param 2: file containing function
|
||||||
example $ write func1 func2 func3 > ~/funcs.sh
|
param 3: operation label
|
||||||
local func
|
example $ transcribe myfunc /tmp/myfunc.sh 'scooby-doo version'
|
||||||
for func in $*
|
example stores your function changes with:
|
||||||
do
|
example master 7a7e524 scooby-doo version myfunc
|
||||||
# trim trailing semicolons generated by declare -f
|
local func=$1
|
||||||
declare -f $func | sed "s/^\(.*\);$/\1/"
|
local file=$2
|
||||||
echo
|
local operation="$3"
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
revise ()
|
if git --version >/dev/null 2>&1
|
||||||
{
|
then
|
||||||
about loads function into editor for revision
|
if [ -d ~/.composure ]
|
||||||
param name of function or functions, separated by spaces
|
then
|
||||||
example $ revise myfunction
|
(
|
||||||
example $ revise func1 func2 func3
|
cd ~/.composure
|
||||||
local temp=$(mktemp /tmp/revise.XXXX)
|
if git rev-parse 2>/dev/null
|
||||||
write $* > $temp
|
then
|
||||||
$EDITOR $temp
|
if [ ! -f $file ]
|
||||||
eval "$(cat $temp)"
|
then
|
||||||
rm $temp
|
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 ()
|
metafor ()
|
||||||
|
|
@ -77,7 +88,7 @@ source_composure ()
|
||||||
param 2: meta keyword
|
param 2: meta keyword
|
||||||
example $ metafor reference example
|
example $ metafor reference example
|
||||||
local func=$1 keyword=$2
|
local func=$1 keyword=$2
|
||||||
write $func | sed -n "s/^ *$keyword \([^([].*\)$/\1/p"
|
declare -f $func | sed -n "s/;$//;s/^ *$keyword \([^([].*\)*$/\1/p"
|
||||||
}
|
}
|
||||||
|
|
||||||
reference ()
|
reference ()
|
||||||
|
|
@ -89,7 +100,7 @@ source_composure ()
|
||||||
|
|
||||||
printline ()
|
printline ()
|
||||||
{
|
{
|
||||||
local metadata=$1 lhs=${2:- }
|
local metadata=$1 leftcol=${2:- } rightcol
|
||||||
|
|
||||||
if [[ -z "$metadata" ]]
|
if [[ -z "$metadata" ]]
|
||||||
then
|
then
|
||||||
|
|
@ -97,10 +108,9 @@ source_composure ()
|
||||||
fi
|
fi
|
||||||
|
|
||||||
OLD=$IFS; IFS=$'\n'
|
OLD=$IFS; IFS=$'\n'
|
||||||
local line
|
for rightcol in $metadata
|
||||||
for line in $metadata
|
|
||||||
do
|
do
|
||||||
printf "%-20s%s\n" $lhs $line
|
printf "%-20s%s\n" $leftcol $rightcol
|
||||||
done
|
done
|
||||||
IFS=$OLD
|
IFS=$OLD
|
||||||
}
|
}
|
||||||
|
|
@ -125,8 +135,6 @@ source_composure ()
|
||||||
echo "examples:"
|
echo "examples:"
|
||||||
printline "$examples"
|
printline "$examples"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unset printline
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ -n "$1" ]]
|
if [[ -n "$1" ]]
|
||||||
|
|
@ -142,6 +150,30 @@ source_composure ()
|
||||||
unset help printline
|
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 ()
|
install_composure ()
|
||||||
|
|
@ -178,6 +210,23 @@ install_composure ()
|
||||||
echo "source $DIR/$(basename $0)" >> ~/.bashrc
|
echo "source $DIR/$(basename $0)" >> ~/.bashrc
|
||||||
fi
|
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.'
|
echo 'composure installed.'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue