#!/bin/bash # # Templater basic functions. # # Initialize project function templater_init { if [ ! -d "$PROJECT" ]; then templater_echo "Initializing $PROJECT..." mkdir -p $PROJECT fi } # Read a parameter from user function templater_ask { local input local function="$1" local default="n" shift 2 read -rep "Setup $function? (defaults to $default): " input if [ "$input" == "y" ]; then templater_$function fi } # Return list of implementations function templater_implementations { # Do not sort this list: the order in which functions are present in the code is important #grep "^function templater_" $PROGRAM | cut -d ' ' -f 2 | sed -e 's/templater_//' ls $SHARE } # Message function templater_echo { #echo "" echo "-> $*" } # Checkout to develop branch if available function templater_checkout_develop { if git branch --list develop | grep -q develop; then git checkout develop fi } # Copy or append source file into destination function templater_copy_or_append { local implementation="$1" local file="$2" if [ -z "$file" ]; then return fi if [ ! -e "$file" ]; then cp $SHARE/$implementation/files/$file . elif ! grep -q -f $SHARE/$implementation/files/$file $file; then cat $SHARE/$implementation/files/$file >> $file fi } # Read a parameter from user function templater_user_input { local input local param="$1" local default="$2" shift 2 if echo $param | grep -q 'passwd'; then read -s -rep "$* (defaults to $default): " input else read -rep "$* (defaults to $default): " input fi if [ -z "$input" ]; then export $param="$default" else export $param="$input" fi } # Install the global Makefile function templater_install_makefile { if [ -z "$1" ]; then return fi local src="$1" local name="`basename $src`" # First ensure we have the main Makefile if [ ! -e "Makefile" ]; then cp $SHARE/templater/files/Makefile . fi # Then copy the custom Makefile if [ ! -e "$name" ]; then cp $src . fi #if ! grep -q "^Makefile.local" .gitignore; then # echo Makefile.local >> .gitignore #fi } # Check differences function templater_diff { local module="$1" local cwd="`pwd`" if [ -z "$module" ]; then return fi if [ ! -d "$SHARE/$module/files" ]; then return fi ( cd $SHARE/$module/files find . -type f | while read file; do # File exists, check differences if [ -e "$cwd/$file" ]; then diff -u $cwd/$file $file else # Use a templaterignore instead if echo $file | grep -q -v 'example'; then templater_echo "Missing $file" fi fi done ) }