Skip to content

Commit

Permalink
Improve new_project script (#4373)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yanfali authored and mechmerlin committed Nov 12, 2018
1 parent 4083614 commit 6274980
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions util/new_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 6274980

Please sign in to comment.