From 6274980e6188c6f074e51b6655afd9efa991863e Mon Sep 17 00:00:00 2001 From: Yan-Fa Li Date: Sun, 11 Nov 2018 19:54:10 -0800 Subject: [PATCH] Improve new_project script (#4373) * Improve new_project script - use git user.name if available to replace the boilerplate name in files we generate - fix shellshock warnings * Test for git repo Suggestion by @skullydazed * Fix shellshock warning * Incorporate feedback from reviewers - thanks @vomindoraan - use a heredoc instead of echo for console - factor out common paths --- util/new_project.sh | 51 ++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/util/new_project.sh b/util/new_project.sh index a85e83322..d1d1ed82c 100755 --- a/util/new_project.sh +++ b/util/new_project.sh @@ -14,7 +14,7 @@ elif [ -z "$KEYBOARD_TYPE" ]; then KEYBOARD_TYPE=avr fi -if [ $KEYBOARD_TYPE != "avr" -a $KEYBOARD_TYPE != "ps2avrgb" ]; then +if [ "$KEYBOARD_TYPE" != "avr" ] && [ "$KEYBOARD_TYPE" != "ps2avrgb" ]; then echo "Invalid keyboard type target" exit 1 fi @@ -24,22 +24,43 @@ if [ -e "keyboards/$1" ]; then exit 1 fi -cd "$(dirname "$0")/.." +cd "$(dirname "$0")/.." || exit -KEYBOARD_UPPERCASE=$(echo $1 | awk '{print toupper($0)}') -KEYBOARD_NAME=$(basename $1) -KEYBOARD_NAME_UPPERCASE=$(echo $KEYBOARD_NAME | awk '{print toupper($0)}') +KEYBOARD_NAME=$(basename "$1") +KEYBOARD_NAME_UPPERCASE=$(echo "$KEYBOARD_NAME" | awk '{print toupper($0)}') +NEW_KBD=keyboards/${KEYBOARD} -cp -r quantum/template/base keyboards/$KEYBOARD -cp -r quantum/template/$KEYBOARD_TYPE/. keyboards/$KEYBOARD +cp -r quantum/template/base "$NEW_KBD" +cp -r "quantum/template/$KEYBOARD_TYPE/." "$NEW_KBD" -mv keyboards/${KEYBOARD}/template.c keyboards/${KEYBOARD}/${KEYBOARD_NAME}.c -mv keyboards/${KEYBOARD}/template.h keyboards/${KEYBOARD}/${KEYBOARD_NAME}.h -find keyboards/${KEYBOARD} -type f -exec sed -i '' -e "s;%KEYBOARD%;${KEYBOARD_NAME};g" {} \; -find keyboards/${KEYBOARD} -type f -exec sed -i '' -e "s;%KEYBOARD_UPPERCASE%;${KEYBOARD_NAME_UPPERCASE};g" {} \; +mv "${NEW_KBD}/template.c" "${NEW_KBD}/${KEYBOARD_NAME}.c" +mv "${NEW_KBD}/template.h" "${NEW_KBD}/${KEYBOARD_NAME}.h" +find "${NEW_KBD}" -type f -exec sed -i '' -e "s;%KEYBOARD%;${KEYBOARD_NAME};g" {} \; +find "${NEW_KBD}" -type f -exec sed -i '' -e "s;%KEYBOARD_UPPERCASE%;${KEYBOARD_NAME_UPPERCASE};g" {} \; -echo "######################################################" -echo "# /keyboards/$KEYBOARD project created. To start" -echo "# working on things, cd into keyboards/$KEYBOARD" -echo "######################################################" +GIT=$(whereis git) +if [ "$GIT" != "" ]; then + IS_GIT_REPO=$($GIT log >>/dev/null 2>&1; echo $?) + if [ "$IS_GIT_REPO" -eq 0 ]; then + ID="'$($GIT config --get user.name)'" + echo "Using $ID as user name" + + for i in "$NEW_KBD/config.h" \ + "$NEW_KBD/$KEYBOARD_NAME.c" \ + "$NEW_KBD/$KEYBOARD_NAME.h" \ + "$NEW_KBD/keymaps/default/config.h" \ + "$NEW_KBD/keymaps/default/keymap.c" + do + awk -v id="$ID" '{sub(/REPLACE_WITH_YOUR_NAME/,id); print}' < "$i" > "$i.$$" + mv "$i.$$" "$i" + done + fi +fi + +cat <<-EOF +###################################################### +# $NEW_KBD project created. To start +# working on things, cd into $NEW_KBD +###################################################### +EOF