]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/python/qmk/cli/compile/json.py
QMK CLI and JSON keymap support (#6176)
[qmk_firmware.git] / lib / python / qmk / cli / compile / json.py
1 """Create a keymap directory from a configurator export.
2 """
3 import json
4 import os
5 import sys
6 import subprocess
7
8 from milc import cli
9
10 import qmk.keymap
11 import qmk.path
12
13
14 @cli.argument('filename', help='Configurator JSON export')
15 @cli.entrypoint('Compile a QMK Configurator export.')
16 def main(cli):
17     """Compile a QMK Configurator export.
18
19     This command creates a new keymap from a configurator export, overwriting an existing keymap if one exists.
20
21     FIXME(skullydazed): add code to check and warn if the keymap already exists
22     """
23     # Error checking
24     if cli.args.filename == ('-'):
25         cli.log.error('Reading from STDIN is not (yet) supported.')
26         exit(1)
27     if not os.path.exists(qmk.path.normpath(cli.args.filename)):
28         cli.log.error('JSON file does not exist!')
29         exit(1)
30
31     # Parse the configurator json
32     with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
33         user_keymap = json.load(fd)
34
35     # Generate the keymap
36     keymap_path = qmk.path.keymap(user_keymap['keyboard'])
37     cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path)
38     qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
39     cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
40
41     # Compile the keymap
42     command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))]
43     cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command))
44     subprocess.run(command)