3 # This script generates a new keyboard directory under keyboards/,
4 # and copies the template files from quantum/template/ into it.
6 # Print an error message with the word "ERROR" in red.
8 echo -e "[\033[0;91mERROR\033[m]: $1"
11 # Print a message in bold.
13 echo -e "\033[1m$1\033[m"
16 # Prompt the user for information, showing the default value in brackets.
21 [ -n "$default" ] && message+=" [$default]"
24 read -rp "$message" prompt_return
25 [ -z "$prompt_return" ] && prompt_return="$default"
28 # Grab a username from Git config.
30 git_username="$(git config --get user.name)"
33 # Copy the template files to the new keyboard directory.
35 echo -n "Copying base template files..."
36 cp -r "quantum/template/base" "${keyboard_dir}"
39 echo -n "Copying $keyboard_type template files..."
40 cp -r "quantum/template/${keyboard_type}/." "${keyboard_dir}"
43 echo -n "Renaming keyboard files..."
44 mv "${keyboard_dir}/template.c" "${keyboard_dir}/${keyboard_name}.c"
45 mv "${keyboard_dir}/template.h" "${keyboard_dir}/${keyboard_name}.h"
49 # Set the inplace editing parameter for sed.
50 # macOS/BSD sed expects a file extension immediately following -i.
55 *Darwin*) sed_i=(-i "")
59 # Replace a token with a value in the given list of files.
60 replace_placeholders() {
61 local replace_token="$1"
62 local replace_value="$2"
64 local replace_filenames=("$@")
66 echo -n "Replacing $replace_token with $replace_value..."
67 for replace_filename in "${replace_filenames[@]}"; do
68 sed "${sed_i[@]}" -e "s/${replace_token}/${replace_value}/g" "$replace_filename"
73 # Replace %KEYBOARD% with the keyboard name.
74 replace_keyboard_placeholders() {
75 local replace_keyboard_filenames=(
76 "${keyboard_dir}/config.h"
77 "${keyboard_dir}/readme.md"
78 "${keyboard_dir}/${keyboard_name}.c"
79 "${keyboard_dir}/keymaps/default/readme.md"
81 replace_placeholders "%KEYBOARD%" "$keyboard_name" "${replace_keyboard_filenames[@]}"
84 # Replace %YOUR_NAME% with the username.
85 replace_name_placeholders() {
86 local replace_name_filenames=(
87 "${keyboard_dir}/config.h"
88 "${keyboard_dir}/readme.md"
89 "${keyboard_dir}/${keyboard_name}.c"
90 "${keyboard_dir}/${keyboard_name}.h"
91 "${keyboard_dir}/keymaps/default/config.h"
92 "${keyboard_dir}/keymaps/default/keymap.c"
94 replace_placeholders "%YOUR_NAME%" "$username" "${replace_name_filenames[@]}"
97 # Check if an array contains an element.
102 [[ "$e" == "$match" ]] && return 0;
108 # If we've been started from util/, we want to be in qmk_firmware/
109 [[ "$PWD" == *util ]] && cd ..
111 # The root qmk_firmware/ directory should have a subdirectory called quantum/
112 if [ ! -d "quantum" ]; then
113 echo_error "Could not detect the QMK firmware directory!"
114 echo_error "Are you sure you're in the right place?"
118 echo_bold "Generating a new QMK keyboard directory"
121 # Keyboard name is required, so keep prompting until we get one
122 while [ -z "$keyboard_name" ]; do
123 prompt "Keyboard Name" ""
124 keyboard_name=$prompt_return
127 keyboard_dir="keyboards/$keyboard_name"
129 if [ -d "$keyboard_dir" ]; then
130 echo_error "Keyboard $keyboard_name already exists!"
134 KEYBOARD_TYPES=("avr" "ps2avrgb")
136 prompt "Keyboard Type" "avr"
137 keyboard_type=$prompt_return
139 if ! array_contains "$keyboard_type" "${KEYBOARD_TYPES[@]}"; then
140 echo_error "Keyboard type must be one of: ${KEYBOARD_TYPES[*]}"
145 prompt "Your Name" "$git_username"
146 username=$prompt_return
152 replace_keyboard_placeholders
153 [ -n "$username" ] && replace_name_placeholders
156 echo_bold "Created a new keyboard called $keyboard_name."
158 echo_bold "To start working on things, cd into keyboards/$keyboard_name,"
159 echo_bold "or open the directory in your favourite text editor."