diff options
Diffstat (limited to 'util/new_keyboard.sh')
| -rwxr-xr-x | util/new_keyboard.sh | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/util/new_keyboard.sh b/util/new_keyboard.sh new file mode 100755 index 000000000..e9ce30978 --- /dev/null +++ b/util/new_keyboard.sh | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | # This script generates a new keyboard directory under keyboards/, | ||
| 4 | # and copies the template files from quantum/template/ into it. | ||
| 5 | |||
| 6 | # Print an error message with the word "ERROR" in red. | ||
| 7 | echo_error() { | ||
| 8 | echo -e "[\033[0;91mERROR\033[m]: $1" | ||
| 9 | } | ||
| 10 | |||
| 11 | # Print a message in bold. | ||
| 12 | echo_bold() { | ||
| 13 | echo -e "\033[1m$1\033[m" | ||
| 14 | } | ||
| 15 | |||
| 16 | # Prompt the user for information, showing the default value in brackets. | ||
| 17 | prompt() { | ||
| 18 | local message="$1" | ||
| 19 | local default="$2" | ||
| 20 | |||
| 21 | [ -n "$default" ] && message+=" [$default]" | ||
| 22 | message+=": " | ||
| 23 | |||
| 24 | read -rp "$message" prompt_return | ||
| 25 | [ -z "$prompt_return" ] && prompt_return="$default" | ||
| 26 | } | ||
| 27 | |||
| 28 | # Grab a username from Git config. | ||
| 29 | set_git_username() { | ||
| 30 | git_username="$(git config --get user.name)" | ||
| 31 | } | ||
| 32 | |||
| 33 | # Copy the template files to the new keyboard directory. | ||
| 34 | copy_templates() { | ||
| 35 | echo -n "Copying base template files..." | ||
| 36 | cp -r "quantum/template/base" "${keyboard_dir}" | ||
| 37 | echo " done" | ||
| 38 | |||
| 39 | echo -n "Copying $keyboard_type template files..." | ||
| 40 | cp -r "quantum/template/${keyboard_type}/." "${keyboard_dir}" | ||
| 41 | echo " done" | ||
| 42 | |||
| 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" | ||
| 46 | echo " done" | ||
| 47 | } | ||
| 48 | |||
| 49 | # Set the inplace editing parameter for sed. | ||
| 50 | # macOS/BSD sed expects a file extension immediately following -i. | ||
| 51 | set_sed_i() { | ||
| 52 | sed_i=(-i) | ||
| 53 | |||
| 54 | case $(uname -a) in | ||
| 55 | *Darwin*) sed_i=(-i "") | ||
| 56 | esac | ||
| 57 | } | ||
| 58 | |||
| 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" | ||
| 63 | shift 2 | ||
| 64 | local replace_filenames=("$@") | ||
| 65 | |||
| 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" | ||
| 69 | done | ||
| 70 | echo " done" | ||
| 71 | } | ||
| 72 | |||
| 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" | ||
| 80 | ) | ||
| 81 | replace_placeholders "%KEYBOARD%" "$keyboard_name" "${replace_keyboard_filenames[@]}" | ||
| 82 | } | ||
| 83 | |||
| 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" | ||
| 93 | ) | ||
| 94 | replace_placeholders "%YOUR_NAME%" "$username" "${replace_name_filenames[@]}" | ||
| 95 | } | ||
| 96 | |||
| 97 | # Check if an array contains an element. | ||
| 98 | array_contains() { | ||
| 99 | local e match="$1" | ||
| 100 | shift | ||
| 101 | for e; do | ||
| 102 | [[ "$e" == "$match" ]] && return 0; | ||
| 103 | done | ||
| 104 | |||
| 105 | return 1 | ||
| 106 | } | ||
| 107 | |||
| 108 | # If we've been started from util/, we want to be in qmk_firmware/ | ||
| 109 | [[ "$PWD" == *util ]] && cd .. | ||
| 110 | |||
| 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?" | ||
| 115 | exit 1 | ||
| 116 | fi | ||
| 117 | |||
| 118 | echo_bold "Generating a new QMK keyboard directory" | ||
| 119 | echo | ||
| 120 | |||
| 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 | ||
| 125 | done | ||
| 126 | |||
| 127 | keyboard_dir="keyboards/$keyboard_name" | ||
| 128 | |||
| 129 | if [ -d "$keyboard_dir" ]; then | ||
| 130 | echo_error "Keyboard $keyboard_name already exists!" | ||
| 131 | exit 1 | ||
| 132 | fi | ||
| 133 | |||
| 134 | KEYBOARD_TYPES=("avr" "ps2avrgb") | ||
| 135 | |||
| 136 | prompt "Keyboard Type" "avr" | ||
| 137 | keyboard_type=$prompt_return | ||
| 138 | |||
| 139 | if ! array_contains "$keyboard_type" "${KEYBOARD_TYPES[@]}"; then | ||
| 140 | echo_error "Keyboard type must be one of: ${KEYBOARD_TYPES[*]}" | ||
| 141 | exit 1 | ||
| 142 | fi | ||
| 143 | |||
| 144 | set_git_username | ||
| 145 | prompt "Your Name" "$git_username" | ||
| 146 | username=$prompt_return | ||
| 147 | |||
| 148 | echo | ||
| 149 | |||
| 150 | copy_templates | ||
| 151 | set_sed_i | ||
| 152 | replace_keyboard_placeholders | ||
| 153 | [ -n "$username" ] && replace_name_placeholders | ||
| 154 | |||
| 155 | echo | ||
| 156 | echo_bold "Created a new keyboard called $keyboard_name." | ||
| 157 | echo | ||
| 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." | ||
