A better new_project.sh (#5191)

* A better new_project.sh

* Fix docstrings

* Use single quotes for anything not shown to user

* Missed this docstring

* Simplify get_git_username()

Thanks @vomindoraan

* chmod +x

* Add docstring for print_error()

* Break up git username call into multiple lines

* Use with statement here

* Conform to PEP 8 even more

* Turn it back into a shell script

* chmod +x again

* Update docs to reflect new keyboard generator usage

* Tweak wording slightly

* Trim trailing whitespace

* Don't actually need to escape the newlines here

* As I suspected, you can pass shift a number

* Prepend ./ to match the other code block

* Minor syntax tweaks

* The username token has changed

* Replace name in the readme too

* Make some reasonable assumptions about the presence of Git
This commit is contained in:
fauxpark 2019-04-28 09:42:16 +10:00 committed by MechMerlin
parent c3be0520c4
commit 53c51f1d16
5 changed files with 203 additions and 90 deletions

View file

@ -198,15 +198,17 @@ From here, you should have a working keyboard once you program a firmware. Befor
To start out, download [the firmware](https://github.com/qmk/qmk_firmware/) - we'll be using my (Jack's) fork of TMK called QMK/Quantum. We'll be doing a lot from the Terminal/command prompt, so get that open, along with a decent text editor like [Sublime Text](http://www.sublimetext.com/) (paid) or [Visual Studio Code](https://code.visualstudio.com) (free). To start out, download [the firmware](https://github.com/qmk/qmk_firmware/) - we'll be using my (Jack's) fork of TMK called QMK/Quantum. We'll be doing a lot from the Terminal/command prompt, so get that open, along with a decent text editor like [Sublime Text](http://www.sublimetext.com/) (paid) or [Visual Studio Code](https://code.visualstudio.com) (free).
The first thing we're going to do is create a new project using the script in the root directory of the firmware. In your terminal, run this command with `<project_name>` replaced by the name of your project - it'll need to be different from any other project in the `keyboards/` folder: The first thing we're going to do is create a new keyboard. In your terminal, run this command, which will ask you some questions and generate a basic keyboard project:
``` ```
util/new_project.sh <project_name> ./util/new_keyboard.sh
``` ```
You'll want to navigate to the `keyboards/<project_name>/` folder by typing, like the print-out from the script specifies: You'll want to navigate to the `keyboards/<project_name>/` folder by typing, like the print-out from the script specifies:
```
cd keyboards/<project_name> cd keyboards/<project_name>
```
### `config.h` ### `config.h`
@ -326,7 +328,7 @@ Carefully flip your keyboard over, open up a new text document, and try typing -
2. Check the solder joints on the diode - if the diode is loose, part of your row may register, while the other may not. 2. Check the solder joints on the diode - if the diode is loose, part of your row may register, while the other may not.
3. Check the solder joints on the columns - if your column wiring is loose, part or all of the column may not work. 3. Check the solder joints on the columns - if your column wiring is loose, part or all of the column may not work.
4. Check the solder joints on both sides of the wires going to/from the Teensy - the wires need to be fully soldered and connect to both sides. 4. Check the solder joints on both sides of the wires going to/from the Teensy - the wires need to be fully soldered and connect to both sides.
5. Check the <project_name>.h file for errors and incorrectly placed `KC_NO`s - if you're unsure where they should be, instead duplicate a k*xy* variable. 5. Check the `<project_name>.h` file for errors and incorrectly placed `KC_NO`s - if you're unsure where they should be, instead duplicate a k*xy* variable.
6. Check to make sure you actually compiled the firmware and flashed the Teensy correctly. Unless you got error messages in the terminal, or a pop-up during flashing, you probably did everything correctly. 6. Check to make sure you actually compiled the firmware and flashed the Teensy correctly. Unless you got error messages in the terminal, or a pop-up during flashing, you probably did everything correctly.
If you've done all of these things, keep in mind that sometimes you might have had multiple things affecting the keyswitch, so it doesn't hurt to test the keyswitch by shorting it out at the end. If you've done all of these things, keep in mind that sometimes you might have had multiple things affecting the keyswitch, so it doesn't hurt to test the keyswitch by shorting it out at the end.
@ -335,4 +337,4 @@ If you've done all of these things, keep in mind that sometimes you might have h
Now that you have a working board, it's time to get things in their permanent positions. I've often used liberal amounts of hot glue to secure and insulate things, so if that's your style, start spreading that stuff like butter. Otherwise, double-sided tape is always an elegant solution, and electrical tape is a distant second. Due to the nature of these builds, a lot of this part is up to you and how you planned (or didn't plan) things out. Now that you have a working board, it's time to get things in their permanent positions. I've often used liberal amounts of hot glue to secure and insulate things, so if that's your style, start spreading that stuff like butter. Otherwise, double-sided tape is always an elegant solution, and electrical tape is a distant second. Due to the nature of these builds, a lot of this part is up to you and how you planned (or didn't plan) things out.
There are a lot of possibilities inside the firmware - explore [docs.qmk.fm](http://docs.qmk.fm) for a full feature list, and dive into the different project (Planck, Clueboard, Ergodox EZ, etc) to see how people use all of them. You can always stop by [the OLKB subreddit for help!](http://reddit.com/r/olkb) There are a lot of possibilities inside the firmware - explore [docs.qmk.fm](http://docs.qmk.fm) for a full feature list, and dive into the different keyboards (Planck, Clueboard, Ergodox EZ, etc) to see how people use all of them. You can always stop by [the OLKB subreddit for help!](http://reddit.com/r/olkb)

View file

@ -6,14 +6,26 @@ If you have not yet you should read the [Keyboard Guidelines](hardware_keyboard_
## Adding Your AVR Keyboard to QMK ## Adding Your AVR Keyboard to QMK
QMK has a number of features to simplify working with AVR keyboards. For most keyboards you don't have to write a single line of code. To get started run the `util/new_project.sh` script: QMK has a number of features to simplify working with AVR keyboards. For most keyboards you don't have to write a single line of code. To get started, run the `util/new_keyboard.sh` script:
```bash ```
$ util/new_project.sh my_awesome_keyboard $ ./util/new_keyboard.sh
###################################################### Generating a new QMK keyboard directory
# /keyboards/my_awesome_keyboard project created. To start
# working on things, cd into keyboards/my_awesome_keyboard Keyboard Name: mycoolkb
###################################################### Keyboard Type [avr]:
Your Name [John Smith]:
Copying base template files... done
Copying avr template files... done
Renaming keyboard files... done
Replacing %KEYBOARD% with mycoolkb... done
Replacing %YOUR_NAME% with John Smith... done
Created a new keyboard called mycoolkb.
To start working on things, cd into keyboards/mycoolkb,
or open the directory in your favourite text editor.
``` ```
This will create all the files needed to support your new keyboard, and populate the settings with default values. Now you just need to customize it for your keyboard. This will create all the files needed to support your new keyboard, and populate the settings with default values. Now you just need to customize it for your keyboard.

View file

@ -1,15 +1,25 @@
Setting up your ARM based PCB is a little more involved than an Atmel MCU, but is easy enough. Start by using `util/new_project.sh <keyboard>` to create a new project: Setting up your ARM based PCB is a little more involved than an Atmel MCU, but is easy enough. Start by running `util/new_keyboard.sh`:
``` ```
$ util/new_project.sh simontester $ ./util/new_keyboard.sh
###################################################### Generating a new QMK keyboard directory
# /keyboards/simontester project created. To start
# working on things, cd into keyboards/simontester Keyboard Name: mycoolkb
###################################################### Keyboard Type [avr]:
Your Name [John Smith]:
Copying base template files... done
Copying avr template files... done
Renaming keyboard files... done
Replacing %KEYBOARD% with mycoolkb... done
Replacing %YOUR_NAME% with John Smith... done
Created a new keyboard called mycoolkb.
To start working on things, cd into keyboards/mycoolkb,
or open the directory in your favourite text editor.
``` ```
# END OF NEW ARM DOC, OLD ATMEL DOC FOLLOWS # END OF NEW ARM DOC, OLD ATMEL DOC FOLLOWS
## `/keyboards/<keyboard>/config.h` ## `/keyboards/<keyboard>/config.h`

159
util/new_keyboard.sh Executable file
View file

@ -0,0 +1,159 @@
#!/bin/bash
# This script generates a new keyboard directory under keyboards/,
# and copies the template files from quantum/template/ into it.
# Print an error message with the word "ERROR" in red.
echo_error() {
echo -e "[\033[0;91mERROR\033[m]: $1"
}
# Print a message in bold.
echo_bold() {
echo -e "\033[1m$1\033[m"
}
# Prompt the user for information, showing the default value in brackets.
prompt() {
local message="$1"
local default="$2"
[ -n "$default" ] && message+=" [$default]"
message+=": "
read -rp "$message" prompt_return
[ -z "$prompt_return" ] && prompt_return="$default"
}
# Grab a username from Git config.
set_git_username() {
git_username="$(git config --get user.name)"
}
# Copy the template files to the new keyboard directory.
copy_templates() {
echo -n "Copying base template files..."
cp -r "quantum/template/base" "${keyboard_dir}"
echo " done"
echo -n "Copying $keyboard_type template files..."
cp -r "quantum/template/${keyboard_type}/." "${keyboard_dir}"
echo " done"
echo -n "Renaming keyboard files..."
mv "${keyboard_dir}/template.c" "${keyboard_dir}/${keyboard_name}.c"
mv "${keyboard_dir}/template.h" "${keyboard_dir}/${keyboard_name}.h"
echo " done"
}
# Set the inplace editing parameter for sed.
# macOS/BSD sed expects a file extension immediately following -i.
set_sed_i() {
sed_i=(-i)
case $(uname -a) in
*Darwin*) sed_i=(-i "")
esac
}
# Replace a token with a value in the given list of files.
replace_placeholders() {
local replace_token="$1"
local replace_value="$2"
shift 2
local replace_filenames=("$@")
echo -n "Replacing $replace_token with $replace_value..."
for replace_filename in "${replace_filenames[@]}"; do
sed "${sed_i[@]}" -e "s/${replace_token}/${replace_value}/g" "$replace_filename"
done
echo " done"
}
# Replace %KEYBOARD% with the keyboard name.
replace_keyboard_placeholders() {
local replace_keyboard_filenames=(
"${keyboard_dir}/config.h"
"${keyboard_dir}/readme.md"
"${keyboard_dir}/${keyboard_name}.c"
"${keyboard_dir}/keymaps/default/readme.md"
)
replace_placeholders "%KEYBOARD%" "$keyboard_name" "${replace_keyboard_filenames[@]}"
}
# Replace %YOUR_NAME% with the username.
replace_name_placeholders() {
local replace_name_filenames=(
"${keyboard_dir}/config.h"
"${keyboard_dir}/readme.md"
"${keyboard_dir}/${keyboard_name}.c"
"${keyboard_dir}/${keyboard_name}.h"
"${keyboard_dir}/keymaps/default/config.h"
"${keyboard_dir}/keymaps/default/keymap.c"
)
replace_placeholders "%YOUR_NAME%" "$username" "${replace_name_filenames[@]}"
}
# Check if an array contains an element.
array_contains() {
local e match="$1"
shift
for e; do
[[ "$e" == "$match" ]] && return 0;
done
return 1
}
# If we've been started from util/, we want to be in qmk_firmware/
[[ "$PWD" == *util ]] && cd ..
# The root qmk_firmware/ directory should have a subdirectory called quantum/
if [ ! -d "quantum" ]; then
echo_error "Could not detect the QMK firmware directory!"
echo_error "Are you sure you're in the right place?"
exit 1
fi
echo_bold "Generating a new QMK keyboard directory"
echo
# Keyboard name is required, so keep prompting until we get one
while [ -z "$keyboard_name" ]; do
prompt "Keyboard Name" ""
keyboard_name=$prompt_return
done
keyboard_dir="keyboards/$keyboard_name"
if [ -d "$keyboard_dir" ]; then
echo_error "Keyboard $keyboard_name already exists!"
exit 1
fi
KEYBOARD_TYPES=("avr" "ps2avrgb")
prompt "Keyboard Type" "avr"
keyboard_type=$prompt_return
if ! array_contains "$keyboard_type" "${KEYBOARD_TYPES[@]}"; then
echo_error "Keyboard type must be one of: ${KEYBOARD_TYPES[*]}"
exit 1
fi
set_git_username
prompt "Your Name" "$git_username"
username=$prompt_return
echo
copy_templates
set_sed_i
replace_keyboard_placeholders
[ -n "$username" ] && replace_name_placeholders
echo
echo_bold "Created a new keyboard called $keyboard_name."
echo
echo_bold "To start working on things, cd into keyboards/$keyboard_name,"
echo_bold "or open the directory in your favourite text editor."

View file

@ -1,70 +0,0 @@
#!/bin/sh
# Script to make a new quantum project
# Jack Humbert 2015
KEYBOARD=$1
KEYBOARD_TYPE=$2
if [ -z "$KEYBOARD" ]; then
echo "Usage: $0 <keyboard_name> <keyboard_type>"
echo "Example: $0 gh60 avr"
echo "Example: $0 bfake ps2avrgb"
exit 1
elif [ -z "$KEYBOARD_TYPE" ]; then
KEYBOARD_TYPE=avr
fi
if [ "$KEYBOARD_TYPE" != "avr" ] && [ "$KEYBOARD_TYPE" != "ps2avrgb" ]; then
echo "Invalid keyboard type target"
exit 1
fi
if [ -e "keyboards/$1" ]; then
echo "Error! keyboards/$1 already exists!"
exit 1
fi
cd "$(dirname "$0")/.." || exit
KEYBOARD_NAME=$(basename "$1")
KEYBOARD_NAME_UPPERCASE=$(echo "$KEYBOARD_NAME" | awk '{print toupper($0)}')
NEW_KBD=keyboards/${KEYBOARD}
cp -r quantum/template/base "$NEW_KBD"
cp -r "quantum/template/$KEYBOARD_TYPE/." "$NEW_KBD"
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" {} \;
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)"
read -rp "What is your name? [$ID] " YOUR_NAME
if [ -n "$YOUR_NAME" ]; then
ID=$YOUR_NAME
fi
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(/%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