diff options
Diffstat (limited to 'lib/python/qmk/cli/new/keyboard.py')
-rw-r--r-- | lib/python/qmk/cli/new/keyboard.py | 141 |
1 files changed, 136 insertions, 5 deletions
diff --git a/lib/python/qmk/cli/new/keyboard.py b/lib/python/qmk/cli/new/keyboard.py index ae4445ca4..9e4232679 100644 --- a/lib/python/qmk/cli/new/keyboard.py +++ b/lib/python/qmk/cli/new/keyboard.py | |||
@@ -1,11 +1,142 @@ | |||
1 | """This script automates the creation of keyboards. | 1 | """This script automates the creation of new keyboard directories using a starter template. |
2 | """ | 2 | """ |
3 | from datetime import date | ||
4 | import fileinput | ||
5 | from pathlib import Path | ||
6 | import re | ||
7 | import shutil | ||
8 | |||
9 | from qmk.commands import git_get_username | ||
10 | import qmk.path | ||
3 | from milc import cli | 11 | from milc import cli |
12 | from milc.questions import choice, question | ||
13 | |||
14 | KEYBOARD_TYPES = ['avr', 'ps2avrgb'] | ||
15 | |||
16 | |||
17 | def keyboard_name(name): | ||
18 | """Callable for argparse validation. | ||
19 | """ | ||
20 | if not validate_keyboard_name(name): | ||
21 | raise ValueError | ||
22 | return name | ||
4 | 23 | ||
5 | 24 | ||
6 | @cli.subcommand('Creates a new keyboard') | 25 | def validate_keyboard_name(name): |
26 | """Returns True if the given keyboard name contains only lowercase a-z, 0-9 and underscore characters. | ||
27 | """ | ||
28 | regex = re.compile(r'^[a-z0-9][a-z0-9/_]+$') | ||
29 | return bool(regex.match(name)) | ||
30 | |||
31 | |||
32 | @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) | ||
34 | @cli.argument('-u', '--username', help='Specify your username (default from Git config)', arg_only=True) | ||
35 | @cli.subcommand('Creates a new keyboard directory') | ||
7 | def new_keyboard(cli): | 36 | def new_keyboard(cli): |
8 | """Creates a new keyboard | 37 | """Creates a new keyboard. |
9 | """ | 38 | """ |
10 | # TODO: replace this bodge to the existing script | 39 | cli.log.info('{style_bright}Generating a new QMK keyboard directory{style_normal}') |
11 | cli.run(['util/new_keyboard.sh'], stdin=None, capture_output=False) | 40 | cli.echo('') |
41 | |||
42 | # Get keyboard name | ||
43 | new_keyboard_name = None | ||
44 | while not new_keyboard_name: | ||
45 | new_keyboard_name = cli.args.keyboard if cli.args.keyboard else question('Keyboard Name:') | ||
46 | if not validate_keyboard_name(new_keyboard_name): | ||
47 | cli.log.error('Keyboard names must contain only {fg_cyan}lowercase a-z{fg_reset}, {fg_cyan}0-9{fg_reset}, and {fg_cyan}_{fg_reset}! Please choose a different name.') | ||
48 | |||
49 | # Exit if passed by arg | ||
50 | if cli.args.keyboard: | ||
51 | return False | ||
52 | |||
53 | new_keyboard_name = None | ||
54 | continue | ||
55 | |||
56 | keyboard_path = qmk.path.keyboard(new_keyboard_name) | ||
57 | if keyboard_path.exists(): | ||
58 | cli.log.error(f'Keyboard {{fg_cyan}}{new_keyboard_name}{{fg_reset}} already exists! Please choose a different name.') | ||
59 | |||
60 | # Exit if passed by arg | ||
61 | if cli.args.keyboard: | ||
62 | return False | ||
63 | |||
64 | new_keyboard_name = None | ||
65 | |||
66 | # Get keyboard type | ||
67 | keyboard_type = cli.args.type if cli.args.type else choice('Keyboard Type:', KEYBOARD_TYPES, default=0) | ||
68 | |||
69 | # Get username | ||
70 | user_name = None | ||
71 | while not user_name: | ||
72 | user_name = question('Your Name:', default=find_user_name()) | ||
73 | |||
74 | 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.') | ||
76 | |||
77 | # Exit if passed by arg | ||
78 | if cli.args.username: | ||
79 | return False | ||
80 | |||
81 | # Copy all the files | ||
82 | copy_templates(keyboard_type, keyboard_path) | ||
83 | |||
84 | # Replace all the placeholders | ||
85 | keyboard_basename = keyboard_path.name | ||
86 | replacements = [ | ||
87 | ('%YEAR%', str(date.today().year)), | ||
88 | ('%KEYBOARD%', keyboard_basename), | ||
89 | ('%YOUR_NAME%', user_name), | ||
90 | ] | ||
91 | filenames = [ | ||
92 | keyboard_path / 'config.h', | ||
93 | keyboard_path / 'info.json', | ||
94 | keyboard_path / 'readme.md', | ||
95 | keyboard_path / f'{keyboard_basename}.c', | ||
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 | |||
102 | cli.echo('') | ||
103 | cli.log.info(f'{{fg_green}}Created a new keyboard called {{fg_cyan}}{new_keyboard_name}{{fg_green}}.{{fg_reset}}') | ||
104 | cli.log.info(f'To start working on things, `cd` into {{fg_cyan}}{keyboard_path}{{fg_reset}},') | ||
105 | cli.log.info('or open the directory in your preferred text editor.') | ||
106 | |||
107 | |||
108 | def find_user_name(): | ||
109 | if cli.args.username: | ||
110 | return cli.args.username | ||
111 | elif cli.config.user.name: | ||
112 | return cli.config.user.name | ||
113 | else: | ||
114 | return git_get_username() | ||
115 | |||
116 | |||
117 | def copy_templates(keyboard_type, keyboard_path): | ||
118 | """Copies the template files from quantum/template to the new keyboard directory. | ||
119 | """ | ||
120 | template_base_path = Path('quantum/template') | ||
121 | keyboard_basename = keyboard_path.name | ||
122 | |||
123 | cli.log.info('Copying base template files...') | ||
124 | shutil.copytree(template_base_path / 'base', keyboard_path) | ||
125 | |||
126 | cli.log.info(f'Copying {{fg_cyan}}{keyboard_type}{{fg_reset}} template files...') | ||
127 | shutil.copytree(template_base_path / keyboard_type, keyboard_path, dirs_exist_ok=True) | ||
128 | |||
129 | cli.log.info(f'Renaming {{fg_cyan}}keyboard.[ch]{{fg_reset}} to {{fg_cyan}}{keyboard_basename}.[ch]{{fg_reset}}...') | ||
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 | |||
133 | |||
134 | def replace_placeholders(replacements, filenames): | ||
135 | """Replaces the given placeholders in each template file. | ||
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 | |||
140 | with fileinput.input(files=filenames, inplace=True) as file: | ||
141 | for line in file: | ||
142 | print(line.replace(replacement[0], replacement[1]), end='') | ||