Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve new_project script #4373

Merged
merged 4 commits into from
Nov 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
yanfali marked this conversation as resolved.
Show resolved Hide resolved
IS_GIT_REPO=$($GIT log >>/dev/null 2>&1; echo $?)
if [ "$IS_GIT_REPO" -eq 0 ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While updating #4222, it occurred to me that this can be written without test, since you just want to check the exit code (which is what if does by default):

Suggested change
if [ "$IS_GIT_REPO" -eq 0 ]; then
if $GIT log >/dev/null 2>&1; then # Check that we're inside a repo

This is simpler than the existing code and doesn't have the potentially misleading "$IS_GIT_REPO" -eq 0 check, but it's not necessarily better since it requires a comment to be as clear as the original.

To clarify, my problem with "$IS_GIT_REPO" -eq 0 is that to someone who's not used to reading shell scripts, it might look like it's checking that this is not a Git repo, due to the variable name. I think something like GIT_REPO_STATUS would be more appropriate.

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