diff options
author | QMK Bot <hello@qmk.fm> | 2021-11-04 21:15:20 +0000 |
---|---|---|
committer | QMK Bot <hello@qmk.fm> | 2021-11-04 21:15:20 +0000 |
commit | 4b6db33a1df798c8895f623937aa61233f1516cc (patch) | |
tree | 7b96e79236888aac8e8a3064c53dbedcc9f712b6 | |
parent | 94018367278f44fcf3eab8a86525f2b257ecf4ba (diff) | |
parent | fefd7fdc5145b677da7b0e1d75bab11eac9db605 (diff) | |
download | qmk_firmware-4b6db33a1df798c8895f623937aa61233f1516cc.tar.gz qmk_firmware-4b6db33a1df798c8895f623937aa61233f1516cc.zip |
Merge remote-tracking branch 'origin/master' into develop
-rw-r--r-- | data/templates/avr/config.h | 22 | ||||
-rw-r--r-- | data/templates/avr/readme.md | 10 | ||||
-rw-r--r-- | data/templates/base/%(KEYBOARD)s.c | 4 | ||||
-rw-r--r-- | data/templates/base/%(KEYBOARD)s.h | 22 | ||||
-rw-r--r-- | data/templates/base/info.json | 4 | ||||
-rw-r--r-- | data/templates/base/keyboard.c | 17 | ||||
-rw-r--r-- | data/templates/base/keyboard.h | 35 | ||||
-rw-r--r-- | data/templates/base/keymaps/default/keymap.c | 48 | ||||
-rw-r--r-- | data/templates/base/keymaps/default/readme.md | 2 | ||||
-rw-r--r-- | data/templates/ps2avrgb/config.h | 22 | ||||
-rw-r--r-- | data/templates/ps2avrgb/readme.md | 10 | ||||
-rw-r--r-- | docs/coding_conventions_python.md | 2 | ||||
-rw-r--r-- | lib/python/qmk/cli/new/keyboard.py | 79 |
13 files changed, 90 insertions, 187 deletions
diff --git a/data/templates/avr/config.h b/data/templates/avr/config.h index 4192bbcfa..7c15e8e70 100644 --- a/data/templates/avr/config.h +++ b/data/templates/avr/config.h | |||
@@ -1,19 +1,5 @@ | |||
1 | /* | 1 | // Copyright %(YEAR)s %(YOUR_NAME)s (@%(USER_NAME)s) |
2 | Copyright %YEAR% %YOUR_NAME% | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | 3 | ||
18 | #pragma once | 4 | #pragma once |
19 | 5 | ||
@@ -23,8 +9,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
23 | #define VENDOR_ID 0xFEED | 9 | #define VENDOR_ID 0xFEED |
24 | #define PRODUCT_ID 0x0000 | 10 | #define PRODUCT_ID 0x0000 |
25 | #define DEVICE_VER 0x0001 | 11 | #define DEVICE_VER 0x0001 |
26 | #define MANUFACTURER %YOUR_NAME% | 12 | #define MANUFACTURER %(USER_NAME)s |
27 | #define PRODUCT %KEYBOARD% | 13 | #define PRODUCT %(KEYBOARD)s |
28 | 14 | ||
29 | /* key matrix size */ | 15 | /* key matrix size */ |
30 | #define MATRIX_ROWS 2 | 16 | #define MATRIX_ROWS 2 |
diff --git a/data/templates/avr/readme.md b/data/templates/avr/readme.md index b099ed726..207850e06 100644 --- a/data/templates/avr/readme.md +++ b/data/templates/avr/readme.md | |||
@@ -1,20 +1,20 @@ | |||
1 | # %KEYBOARD% | 1 | # %(KEYBOARD)s |
2 | 2 | ||
3 |  | 3 |  |
4 | 4 | ||
5 | *A short description of the keyboard/project* | 5 | *A short description of the keyboard/project* |
6 | 6 | ||
7 | * Keyboard Maintainer: [%YOUR_NAME%](https://github.com/yourusername) | 7 | * Keyboard Maintainer: [%(YOUR_NAME)s](https://github.com/%(USER_NAME)s) |
8 | * Hardware Supported: *The PCBs, controllers supported* | 8 | * Hardware Supported: *The PCBs, controllers supported* |
9 | * Hardware Availability: *Links to where you can find this hardware* | 9 | * Hardware Availability: *Links to where you can find this hardware* |
10 | 10 | ||
11 | Make example for this keyboard (after setting up your build environment): | 11 | Make example for this keyboard (after setting up your build environment): |
12 | 12 | ||
13 | make %KEYBOARD%:default | 13 | make %(KEYBOARD)s:default |
14 | 14 | ||
15 | Flashing example for this keyboard: | 15 | Flashing example for this keyboard: |
16 | 16 | ||
17 | make %KEYBOARD%:default:flash | 17 | make %(KEYBOARD)s:default:flash |
18 | 18 | ||
19 | See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). | 19 | See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). |
20 | 20 | ||
diff --git a/data/templates/base/%(KEYBOARD)s.c b/data/templates/base/%(KEYBOARD)s.c new file mode 100644 index 000000000..0c509aff6 --- /dev/null +++ b/data/templates/base/%(KEYBOARD)s.c | |||
@@ -0,0 +1,4 @@ | |||
1 | // Copyright %(YEAR)s %(YOUR_NAME)s (@%(USER_NAME)s) | ||
2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
3 | |||
4 | #include "%(KEYBOARD)s.h" | ||
diff --git a/data/templates/base/%(KEYBOARD)s.h b/data/templates/base/%(KEYBOARD)s.h new file mode 100644 index 000000000..3e8ca3912 --- /dev/null +++ b/data/templates/base/%(KEYBOARD)s.h | |||
@@ -0,0 +1,22 @@ | |||
1 | // Copyright %(YEAR)s %(YOUR_NAME)s (@%(USER_NAME)s) | ||
2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
3 | |||
4 | #pragma once | ||
5 | |||
6 | #include "quantum.h" | ||
7 | |||
8 | /* This is a shortcut to help you visually see your layout. | ||
9 | * | ||
10 | * The first section contains all of the arguments representing the physical | ||
11 | * layout of the board and position of the keys. | ||
12 | * | ||
13 | * The second converts the arguments into a two-dimensional array which | ||
14 | * represents the switch matrix. | ||
15 | */ | ||
16 | #define LAYOUT( \ | ||
17 | k00, k01, k02, \ | ||
18 | k10, k12 \ | ||
19 | ) { \ | ||
20 | { k00, k01, k02 }, \ | ||
21 | { k10, KC_NO, k12 } \ | ||
22 | } | ||
diff --git a/data/templates/base/info.json b/data/templates/base/info.json index 3993d8546..a50ccba7b 100644 --- a/data/templates/base/info.json +++ b/data/templates/base/info.json | |||
@@ -1,7 +1,7 @@ | |||
1 | { | 1 | { |
2 | "keyboard_name": "%KEYBOARD%", | 2 | "keyboard_name": "%(KEYBOARD)s", |
3 | "url": "", | 3 | "url": "", |
4 | "maintainer": "%YOUR_NAME%", | 4 | "maintainer": "%(USER_NAME)s", |
5 | "layouts": { | 5 | "layouts": { |
6 | "LAYOUT": { | 6 | "LAYOUT": { |
7 | "layout": [ | 7 | "layout": [ |
diff --git a/data/templates/base/keyboard.c b/data/templates/base/keyboard.c deleted file mode 100644 index f69ae16ed..000000000 --- a/data/templates/base/keyboard.c +++ /dev/null | |||
@@ -1,17 +0,0 @@ | |||
1 | /* Copyright %YEAR% %YOUR_NAME% | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "%KEYBOARD%.h" | ||
diff --git a/data/templates/base/keyboard.h b/data/templates/base/keyboard.h deleted file mode 100644 index bd2e88d12..000000000 --- a/data/templates/base/keyboard.h +++ /dev/null | |||
@@ -1,35 +0,0 @@ | |||
1 | /* Copyright %YEAR% %YOUR_NAME% | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #pragma once | ||
18 | |||
19 | #include "quantum.h" | ||
20 | |||
21 | /* This is a shortcut to help you visually see your layout. | ||
22 | * | ||
23 | * The first section contains all of the arguments representing the physical | ||
24 | * layout of the board and position of the keys. | ||
25 | * | ||
26 | * The second converts the arguments into a two-dimensional array which | ||
27 | * represents the switch matrix. | ||
28 | */ | ||
29 | #define LAYOUT( \ | ||
30 | k00, k01, k02, \ | ||
31 | k10, k12 \ | ||
32 | ) { \ | ||
33 | { k00, k01, k02 }, \ | ||
34 | { k10, KC_NO, k12 } \ | ||
35 | } | ||
diff --git a/data/templates/base/keymaps/default/keymap.c b/data/templates/base/keymaps/default/keymap.c index d8020ab3e..3fec3d512 100644 --- a/data/templates/base/keymaps/default/keymap.c +++ b/data/templates/base/keymaps/default/keymap.c | |||
@@ -1,18 +1,6 @@ | |||
1 | /* Copyright %YEAR% %YOUR_NAME% | 1 | // Copyright %(YEAR)s %(YOUR_NAME)s (@%(USER_NAME)s) |
2 | * | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
3 | * This program is free software: you can redistribute it and/or modify | 3 | |
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | #include QMK_KEYBOARD_H | 4 | #include QMK_KEYBOARD_H |
17 | 5 | ||
18 | // Defines names for use in layer keycodes and the keymap | 6 | // Defines names for use in layer keycodes and the keymap |
@@ -21,12 +9,6 @@ enum layer_names { | |||
21 | _FN | 9 | _FN |
22 | }; | 10 | }; |
23 | 11 | ||
24 | // Defines the keycodes used by our macros in process_record_user | ||
25 | enum custom_keycodes { | ||
26 | QMKBEST = SAFE_RANGE, | ||
27 | QMKURL | ||
28 | }; | ||
29 | |||
30 | const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | 12 | const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { |
31 | /* Base */ | 13 | /* Base */ |
32 | [_BASE] = LAYOUT( | 14 | [_BASE] = LAYOUT( |
@@ -34,29 +16,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | |||
34 | KC_TAB, KC_SPC | 16 | KC_TAB, KC_SPC |
35 | ), | 17 | ), |
36 | [_FN] = LAYOUT( | 18 | [_FN] = LAYOUT( |
37 | QMKBEST, QMKURL, _______, | 19 | _______, _______, _______, |
38 | RESET, XXXXXXX | 20 | RESET, XXXXXXX |
39 | ) | 21 | ) |
40 | }; | 22 | }; |
41 | |||
42 | bool process_record_user(uint16_t keycode, keyrecord_t *record) { | ||
43 | switch (keycode) { | ||
44 | case QMKBEST: | ||
45 | if (record->event.pressed) { | ||
46 | // when keycode QMKBEST is pressed | ||
47 | SEND_STRING("QMK is the best thing ever!"); | ||
48 | } else { | ||
49 | // when keycode QMKBEST is released | ||
50 | } | ||
51 | break; | ||
52 | case QMKURL: | ||
53 | if (record->event.pressed) { | ||
54 | // when keycode QMKURL is pressed | ||
55 | SEND_STRING("https://qmk.fm/\n"); | ||
56 | } else { | ||
57 | // when keycode QMKURL is released | ||
58 | } | ||
59 | break; | ||
60 | } | ||
61 | return true; | ||
62 | } | ||
diff --git a/data/templates/base/keymaps/default/readme.md b/data/templates/base/keymaps/default/readme.md index e052ed80f..63a064327 100644 --- a/data/templates/base/keymaps/default/readme.md +++ b/data/templates/base/keymaps/default/readme.md | |||
@@ -1 +1 @@ | |||
# The default keymap for %KEYBOARD% | # The default keymap for %(KEYBOARD)s | ||
diff --git a/data/templates/ps2avrgb/config.h b/data/templates/ps2avrgb/config.h index 6150bcce6..876a60252 100644 --- a/data/templates/ps2avrgb/config.h +++ b/data/templates/ps2avrgb/config.h | |||
@@ -1,19 +1,5 @@ | |||
1 | /* | 1 | // Copyright %(YEAR)s %(YOUR_NAME)s (@%(USER_NAME)s) |
2 | Copyright %YEAR% %YOUR_NAME% | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | 3 | ||
18 | #pragma once | 4 | #pragma once |
19 | 5 | ||
@@ -23,8 +9,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
23 | #define VENDOR_ID 0xFEED | 9 | #define VENDOR_ID 0xFEED |
24 | #define PRODUCT_ID 0x0000 | 10 | #define PRODUCT_ID 0x0000 |
25 | #define DEVICE_VER 0x0001 | 11 | #define DEVICE_VER 0x0001 |
26 | #define MANUFACTURER %YOUR_NAME% | 12 | #define MANUFACTURER %(USER_NAME)s |
27 | #define PRODUCT %KEYBOARD% | 13 | #define PRODUCT %(KEYBOARD)s |
28 | 14 | ||
29 | /* key matrix size */ | 15 | /* key matrix size */ |
30 | #define MATRIX_ROWS 8 | 16 | #define MATRIX_ROWS 8 |
diff --git a/data/templates/ps2avrgb/readme.md b/data/templates/ps2avrgb/readme.md index 94063f9eb..a2ac4495b 100644 --- a/data/templates/ps2avrgb/readme.md +++ b/data/templates/ps2avrgb/readme.md | |||
@@ -1,20 +1,20 @@ | |||
1 | # %KEYBOARD% | 1 | # %(KEYBOARD)s |
2 | 2 | ||
3 |  | 3 |  |
4 | 4 | ||
5 | *A short description of the keyboard/project* | 5 | *A short description of the keyboard/project* |
6 | 6 | ||
7 | * Keyboard Maintainer: [%YOUR_NAME%](https://github.com/yourusername) | 7 | * Keyboard Maintainer: [%(YOUR_NAME)s](https://github.com/yourusername) |
8 | * Hardware Supported: *The PCBs, controllers supported* | 8 | * Hardware Supported: *The PCBs, controllers supported* |
9 | * Hardware Availability: *Links to where you can find this hardware* | 9 | * Hardware Availability: *Links to where you can find this hardware* |
10 | 10 | ||
11 | Make example for this keyboard (after setting up your build environment): | 11 | Make example for this keyboard (after setting up your build environment): |
12 | 12 | ||
13 | make %KEYBOARD%:default | 13 | make %(KEYBOARD)s:default |
14 | 14 | ||
15 | Flashing example for this keyboard ([after setting up the bootloadHID flashing environment](https://docs.qmk.fm/#/flashing_bootloadhid)) | 15 | Flashing example for this keyboard ([after setting up the bootloadHID flashing environment](https://docs.qmk.fm/#/flashing_bootloadhid)) |
16 | 16 | ||
17 | make %KEYBOARD%:default:flash | 17 | make %(KEYBOARD)s:default:flash |
18 | 18 | ||
19 | See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). | 19 | See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). |
20 | 20 | ||
diff --git a/docs/coding_conventions_python.md b/docs/coding_conventions_python.md index 47dff7f8e..960b9cb49 100644 --- a/docs/coding_conventions_python.md +++ b/docs/coding_conventions_python.md | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | Most of our style follows PEP8 with some local modifications to make things less nit-picky. | 3 | Most of our style follows PEP8 with some local modifications to make things less nit-picky. |
4 | 4 | ||
5 | * We target Python 3.6 for compatability with all supported platforms. | 5 | * We target Python 3.7 for compatability with all supported platforms. |
6 | * We indent using four (4) spaces (soft tabs) | 6 | * We indent using four (4) spaces (soft tabs) |
7 | * We encourage liberal use of comments | 7 | * We encourage liberal use of comments |
8 | * Think of them as a story describing the feature | 8 | * Think of them as a story describing the feature |
diff --git a/lib/python/qmk/cli/new/keyboard.py b/lib/python/qmk/cli/new/keyboard.py index 369d2bd7d..4093b8c90 100644 --- a/lib/python/qmk/cli/new/keyboard.py +++ b/lib/python/qmk/cli/new/keyboard.py | |||
@@ -1,10 +1,8 @@ | |||
1 | """This script automates the creation of new keyboard directories using a starter template. | 1 | """This script automates the creation of new keyboard directories using a starter template. |
2 | """ | 2 | """ |
3 | from datetime import date | 3 | from datetime import date |
4 | import fileinput | ||
5 | from pathlib import Path | 4 | from pathlib import Path |
6 | import re | 5 | import re |
7 | import shutil | ||
8 | 6 | ||
9 | from qmk.commands import git_get_username | 7 | from qmk.commands import git_get_username |
10 | import qmk.path | 8 | import qmk.path |
@@ -32,6 +30,7 @@ def validate_keyboard_name(name): | |||
32 | @cli.argument('-kb', '--keyboard', help='Specify the name for the new keyboard directory', arg_only=True, type=keyboard_name) | 30 | @cli.argument('-kb', '--keyboard', help='Specify the name for the new keyboard directory', arg_only=True, type=keyboard_name) |
33 | @cli.argument('-t', '--type', help='Specify the keyboard type', arg_only=True, choices=KEYBOARD_TYPES) | 31 | @cli.argument('-t', '--type', help='Specify the keyboard type', arg_only=True, choices=KEYBOARD_TYPES) |
34 | @cli.argument('-u', '--username', help='Specify your username (default from Git config)', arg_only=True) | 32 | @cli.argument('-u', '--username', help='Specify your username (default from Git config)', arg_only=True) |
33 | @cli.argument('-n', '--realname', help='Specify your real name if you want to use that. Defaults to username', arg_only=True) | ||
35 | @cli.subcommand('Creates a new keyboard directory') | 34 | @cli.subcommand('Creates a new keyboard directory') |
36 | def new_keyboard(cli): | 35 | def new_keyboard(cli): |
37 | """Creates a new keyboard. | 36 | """Creates a new keyboard. |
@@ -69,7 +68,7 @@ def new_keyboard(cli): | |||
69 | # Get username | 68 | # Get username |
70 | user_name = None | 69 | user_name = None |
71 | while not user_name: | 70 | while not user_name: |
72 | user_name = question('Your Name:', default=find_user_name()) | 71 | user_name = question('Your GitHub User Name:', default=find_user_name()) |
73 | 72 | ||
74 | if not user_name: | 73 | if not user_name: |
75 | cli.log.error('You didn\'t provide a username, and we couldn\'t find one set in your QMK or Git configs. Please try again.') | 74 | cli.log.error('You didn\'t provide a username, and we couldn\'t find one set in your QMK or Git configs. Please try again.') |
@@ -78,26 +77,21 @@ def new_keyboard(cli): | |||
78 | if cli.args.username: | 77 | if cli.args.username: |
79 | return False | 78 | return False |
80 | 79 | ||
81 | # Copy all the files | 80 | real_name = None |
82 | copy_templates(keyboard_type, keyboard_path) | 81 | while not real_name: |
82 | real_name = question('Your real name:', default=user_name) | ||
83 | 83 | ||
84 | # Replace all the placeholders | ||
85 | keyboard_basename = keyboard_path.name | 84 | keyboard_basename = keyboard_path.name |
86 | replacements = [ | 85 | replacements = { |
87 | ('%YEAR%', str(date.today().year)), | 86 | "YEAR": str(date.today().year), |
88 | ('%KEYBOARD%', keyboard_basename), | 87 | "KEYBOARD": keyboard_basename, |
89 | ('%YOUR_NAME%', user_name), | 88 | "USER_NAME": user_name, |
90 | ] | 89 | "YOUR_NAME": real_name, |
91 | filenames = [ | 90 | } |
92 | keyboard_path / 'config.h', | 91 | |
93 | keyboard_path / 'info.json', | 92 | template_dir = Path('data/templates') |
94 | keyboard_path / 'readme.md', | 93 | template_tree(template_dir / 'base', keyboard_path, replacements) |
95 | keyboard_path / f'{keyboard_basename}.c', | 94 | template_tree(template_dir / keyboard_type, keyboard_path, replacements) |
96 | keyboard_path / f'{keyboard_basename}.h', | ||
97 | keyboard_path / 'keymaps/default/readme.md', | ||
98 | keyboard_path / 'keymaps/default/keymap.c', | ||
99 | ] | ||
100 | replace_placeholders(replacements, filenames) | ||
101 | 95 | ||
102 | cli.echo('') | 96 | cli.echo('') |
103 | cli.log.info(f'{{fg_green}}Created a new keyboard called {{fg_cyan}}{new_keyboard_name}{{fg_green}}.{{fg_reset}}') | 97 | cli.log.info(f'{{fg_green}}Created a new keyboard called {{fg_cyan}}{new_keyboard_name}{{fg_green}}.{{fg_reset}}') |
@@ -114,29 +108,32 @@ def find_user_name(): | |||
114 | return git_get_username() | 108 | return git_get_username() |
115 | 109 | ||
116 | 110 | ||
117 | def copy_templates(keyboard_type, keyboard_path): | 111 | def template_tree(src: Path, dst: Path, replacements: dict): |
118 | """Copies the template files from data/templates to the new keyboard directory. | 112 | """Recursively copy template and replace placeholders |
119 | """ | ||
120 | template_base_path = Path('data/templates') | ||
121 | keyboard_basename = keyboard_path.name | ||
122 | 113 | ||
123 | cli.log.info('Copying base template files...') | 114 | Args: |
124 | shutil.copytree(template_base_path / 'base', keyboard_path) | 115 | src (Path) |
116 | The source folder to copy from | ||
117 | dst (Path) | ||
118 | The destination folder to copy to | ||
119 | replacements (dict) | ||
120 | a dictionary with "key":"value" pairs to replace. | ||
125 | 121 | ||
126 | cli.log.info(f'Copying {{fg_cyan}}{keyboard_type}{{fg_reset}} template files...') | 122 | Raises: |
127 | shutil.copytree(template_base_path / keyboard_type, keyboard_path, dirs_exist_ok=True) | 123 | FileExistsError |
124 | When trying to overwrite existing files | ||
125 | """ | ||
128 | 126 | ||
129 | cli.log.info(f'Renaming {{fg_cyan}}keyboard.[ch]{{fg_reset}} to {{fg_cyan}}{keyboard_basename}.[ch]{{fg_reset}}...') | 127 | dst.mkdir(parents=True, exist_ok=True) |
130 | shutil.move(keyboard_path / 'keyboard.c', keyboard_path / f'{keyboard_basename}.c') | ||
131 | shutil.move(keyboard_path / 'keyboard.h', keyboard_path / f'{keyboard_basename}.h') | ||
132 | 128 | ||
129 | for child in src.iterdir(): | ||
130 | if child.is_dir(): | ||
131 | template_tree(child, dst / child.name, replacements=replacements) | ||
133 | 132 | ||
134 | def replace_placeholders(replacements, filenames): | 133 | if child.is_file(): |
135 | """Replaces the given placeholders in each template file. | 134 | file_name = dst / (child.name % replacements) |
136 | """ | ||
137 | for replacement in replacements: | ||
138 | cli.log.info(f'Replacing {{fg_cyan}}{replacement[0]}{{fg_reset}} with {{fg_cyan}}{replacement[1]}{{fg_reset}}...') | ||
139 | 135 | ||
140 | with fileinput.input(files=filenames, inplace=True) as file: | 136 | with file_name.open(mode='x') as dst_f: |
141 | for line in file: | 137 | with child.open() as src_f: |
142 | print(line.replace(replacement[0], replacement[1]), end='') | 138 | template = src_f.read() |
139 | dst_f.write(template % replacements) | ||