aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorfauxpark <fauxpark@gmail.com>2019-04-28 09:42:16 +1000
committerMechMerlin <30334081+mechmerlin@users.noreply.github.com>2019-04-27 16:42:16 -0700
commit53c51f1d16b40fdd3e68a6afc5844917d3d58640 (patch)
treeb17a8b084e538b66a6ccb4893e9c7fa75217c519 /util
parentc3be0520c4c6d4799670ab3639a3de561b4c6c1d (diff)
downloadqmk_firmware-53c51f1d16b40fdd3e68a6afc5844917d3d58640.tar.gz
qmk_firmware-53c51f1d16b40fdd3e68a6afc5844917d3d58640.zip
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
Diffstat (limited to 'util')
-rwxr-xr-xutil/new_keyboard.sh159
-rwxr-xr-xutil/new_project.sh70
2 files changed, 159 insertions, 70 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.
7echo_error() {
8 echo -e "[\033[0;91mERROR\033[m]: $1"
9}
10
11# Print a message in bold.
12echo_bold() {
13 echo -e "\033[1m$1\033[m"
14}
15
16# Prompt the user for information, showing the default value in brackets.
17prompt() {
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.
29set_git_username() {
30 git_username="$(git config --get user.name)"
31}
32
33# Copy the template files to the new keyboard directory.
34copy_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.
51set_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.
60replace_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.
74replace_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.
85replace_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.
98array_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/
112if [ ! -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
116fi
117
118echo_bold "Generating a new QMK keyboard directory"
119echo
120
121# Keyboard name is required, so keep prompting until we get one
122while [ -z "$keyboard_name" ]; do
123 prompt "Keyboard Name" ""
124 keyboard_name=$prompt_return
125done
126
127keyboard_dir="keyboards/$keyboard_name"
128
129if [ -d "$keyboard_dir" ]; then
130 echo_error "Keyboard $keyboard_name already exists!"
131 exit 1
132fi
133
134KEYBOARD_TYPES=("avr" "ps2avrgb")
135
136prompt "Keyboard Type" "avr"
137keyboard_type=$prompt_return
138
139if ! array_contains "$keyboard_type" "${KEYBOARD_TYPES[@]}"; then
140 echo_error "Keyboard type must be one of: ${KEYBOARD_TYPES[*]}"
141 exit 1
142fi
143
144set_git_username
145prompt "Your Name" "$git_username"
146username=$prompt_return
147
148echo
149
150copy_templates
151set_sed_i
152replace_keyboard_placeholders
153[ -n "$username" ] && replace_name_placeholders
154
155echo
156echo_bold "Created a new keyboard called $keyboard_name."
157echo
158echo_bold "To start working on things, cd into keyboards/$keyboard_name,"
159echo_bold "or open the directory in your favourite text editor."
diff --git a/util/new_project.sh b/util/new_project.sh
deleted file mode 100755
index 9dec714b0..000000000
--- a/util/new_project.sh
+++ /dev/null
@@ -1,70 +0,0 @@
1#!/bin/sh
2# Script to make a new quantum project
3# Jack Humbert 2015
4
5KEYBOARD=$1
6KEYBOARD_TYPE=$2
7
8if [ -z "$KEYBOARD" ]; then
9 echo "Usage: $0 <keyboard_name> <keyboard_type>"
10 echo "Example: $0 gh60 avr"
11 echo "Example: $0 bfake ps2avrgb"
12 exit 1
13elif [ -z "$KEYBOARD_TYPE" ]; then
14 KEYBOARD_TYPE=avr
15fi
16
17if [ "$KEYBOARD_TYPE" != "avr" ] && [ "$KEYBOARD_TYPE" != "ps2avrgb" ]; then
18 echo "Invalid keyboard type target"
19 exit 1
20fi
21
22if [ -e "keyboards/$1" ]; then
23 echo "Error! keyboards/$1 already exists!"
24 exit 1
25fi
26
27cd "$(dirname "$0")/.." || exit
28
29KEYBOARD_NAME=$(basename "$1")
30KEYBOARD_NAME_UPPERCASE=$(echo "$KEYBOARD_NAME" | awk '{print toupper($0)}')
31NEW_KBD=keyboards/${KEYBOARD}
32
33
34cp -r quantum/template/base "$NEW_KBD"
35cp -r "quantum/template/$KEYBOARD_TYPE/." "$NEW_KBD"
36
37mv "${NEW_KBD}/template.c" "${NEW_KBD}/${KEYBOARD_NAME}.c"
38mv "${NEW_KBD}/template.h" "${NEW_KBD}/${KEYBOARD_NAME}.h"
39find "${NEW_KBD}" -type f -exec sed -i '' -e "s;%KEYBOARD%;${KEYBOARD_NAME};g" {} \;
40find "${NEW_KBD}" -type f -exec sed -i '' -e "s;%KEYBOARD_UPPERCASE%;${KEYBOARD_NAME_UPPERCASE};g" {} \;
41
42GIT=$(whereis git)
43if [ "$GIT" != "" ]; then
44 IS_GIT_REPO=$($GIT log >>/dev/null 2>&1; echo $?)
45 if [ "$IS_GIT_REPO" -eq 0 ]; then
46 ID="$($GIT config --get user.name)"
47 read -rp "What is your name? [$ID] " YOUR_NAME
48 if [ -n "$YOUR_NAME" ]; then
49 ID=$YOUR_NAME
50 fi
51 echo "Using $ID as user name"
52
53 for i in "$NEW_KBD/config.h" \
54 "$NEW_KBD/$KEYBOARD_NAME.c" \
55 "$NEW_KBD/$KEYBOARD_NAME.h" \
56 "$NEW_KBD/keymaps/default/config.h" \
57 "$NEW_KBD/keymaps/default/keymap.c"
58 do
59 awk -v id="$ID" '{sub(/%YOUR_NAME%/,id); print}' < "$i" > "$i.$$"
60 mv "$i.$$" "$i"
61 done
62 fi
63fi
64
65cat <<-EOF
66######################################################
67# $NEW_KBD project created. To start
68# working on things, cd into $NEW_KBD
69######################################################
70EOF