diff options
Diffstat (limited to 'lib/python/qmk/cli/generate')
| -rw-r--r-- | lib/python/qmk/cli/generate/__init__.py | 4 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/api.py | 14 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 152 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/info_json.py | 65 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/layouts.py | 98 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 91 |
6 files changed, 418 insertions, 6 deletions
diff --git a/lib/python/qmk/cli/generate/__init__.py b/lib/python/qmk/cli/generate/__init__.py index f9585bfb5..bd75b044c 100644 --- a/lib/python/qmk/cli/generate/__init__.py +++ b/lib/python/qmk/cli/generate/__init__.py | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | from . import api | 1 | from . import api |
| 2 | from . import config_h | ||
| 2 | from . import docs | 3 | from . import docs |
| 4 | from . import info_json | ||
| 5 | from . import layouts | ||
| 3 | from . import rgb_breathe_table | 6 | from . import rgb_breathe_table |
| 7 | from . import rules_mk | ||
diff --git a/lib/python/qmk/cli/generate/api.py b/lib/python/qmk/cli/generate/api.py index 66db37cb5..6d111f244 100755 --- a/lib/python/qmk/cli/generate/api.py +++ b/lib/python/qmk/cli/generate/api.py | |||
| @@ -8,6 +8,7 @@ from milc import cli | |||
| 8 | 8 | ||
| 9 | from qmk.datetime import current_datetime | 9 | from qmk.datetime import current_datetime |
| 10 | from qmk.info import info_json | 10 | from qmk.info import info_json |
| 11 | from qmk.info_json_encoder import InfoJSONEncoder | ||
| 11 | from qmk.keyboard import list_keyboards | 12 | from qmk.keyboard import list_keyboards |
| 12 | 13 | ||
| 13 | 14 | ||
| @@ -44,15 +45,16 @@ def generate_api(cli): | |||
| 44 | if 'usb' in kb_all['keyboards'][keyboard_name]: | 45 | if 'usb' in kb_all['keyboards'][keyboard_name]: |
| 45 | usb = kb_all['keyboards'][keyboard_name]['usb'] | 46 | usb = kb_all['keyboards'][keyboard_name]['usb'] |
| 46 | 47 | ||
| 47 | if usb['vid'] not in usb_list['devices']: | 48 | if 'vid' in usb and usb['vid'] not in usb_list['devices']: |
| 48 | usb_list['devices'][usb['vid']] = {} | 49 | usb_list['devices'][usb['vid']] = {} |
| 49 | 50 | ||
| 50 | if usb['pid'] not in usb_list['devices'][usb['vid']]: | 51 | if 'pid' in usb and usb['pid'] not in usb_list['devices'][usb['vid']]: |
| 51 | usb_list['devices'][usb['vid']][usb['pid']] = {} | 52 | usb_list['devices'][usb['vid']][usb['pid']] = {} |
| 52 | 53 | ||
| 53 | usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb | 54 | if 'vid' in usb and 'pid' in usb: |
| 55 | usb_list['devices'][usb['vid']][usb['pid']][keyboard_name] = usb | ||
| 54 | 56 | ||
| 55 | # Write the global JSON files | 57 | # Write the global JSON files |
| 56 | keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])})) | 58 | keyboard_list.write_text(json.dumps({'last_updated': current_datetime(), 'keyboards': sorted(kb_all['keyboards'])}, cls=InfoJSONEncoder)) |
| 57 | keyboard_all.write_text(json.dumps(kb_all)) | 59 | keyboard_all.write_text(json.dumps(kb_all, cls=InfoJSONEncoder)) |
| 58 | usb_file.write_text(json.dumps(usb_list)) | 60 | usb_file.write_text(json.dumps(usb_list, cls=InfoJSONEncoder)) |
diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py new file mode 100755 index 000000000..7ddad745d --- /dev/null +++ b/lib/python/qmk/cli/generate/config_h.py | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | """Used by the make system to generate info_config.h from info.json. | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from dotty_dict import dotty | ||
| 6 | from milc import cli | ||
| 7 | |||
| 8 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 9 | from qmk.info import _json_load, info_json | ||
| 10 | from qmk.path import is_keyboard, normpath | ||
| 11 | |||
| 12 | |||
| 13 | def direct_pins(direct_pins): | ||
| 14 | """Return the config.h lines that set the direct pins. | ||
| 15 | """ | ||
| 16 | rows = [] | ||
| 17 | |||
| 18 | for row in direct_pins: | ||
| 19 | cols = ','.join(map(str, [col or 'NO_PIN' for col in row])) | ||
| 20 | rows.append('{' + cols + '}') | ||
| 21 | |||
| 22 | col_count = len(direct_pins[0]) | ||
| 23 | row_count = len(direct_pins) | ||
| 24 | |||
| 25 | return """ | ||
| 26 | #ifndef MATRIX_COLS | ||
| 27 | # define MATRIX_COLS %s | ||
| 28 | #endif // MATRIX_COLS | ||
| 29 | |||
| 30 | #ifndef MATRIX_ROWS | ||
| 31 | # define MATRIX_ROWS %s | ||
| 32 | #endif // MATRIX_ROWS | ||
| 33 | |||
| 34 | #ifndef DIRECT_PINS | ||
| 35 | # define DIRECT_PINS {%s} | ||
| 36 | #endif // DIRECT_PINS | ||
| 37 | """ % (col_count, row_count, ','.join(rows)) | ||
| 38 | |||
| 39 | |||
| 40 | def pin_array(define, pins): | ||
| 41 | """Return the config.h lines that set a pin array. | ||
| 42 | """ | ||
| 43 | pin_num = len(pins) | ||
| 44 | pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins])) | ||
| 45 | |||
| 46 | return f""" | ||
| 47 | #ifndef {define}S | ||
| 48 | # define {define}S {pin_num} | ||
| 49 | #endif // {define}S | ||
| 50 | |||
| 51 | #ifndef {define}_PINS | ||
| 52 | # define {define}_PINS {{ {pin_array} }} | ||
| 53 | #endif // {define}_PINS | ||
| 54 | """ | ||
| 55 | |||
| 56 | |||
| 57 | def matrix_pins(matrix_pins): | ||
| 58 | """Add the matrix config to the config.h. | ||
| 59 | """ | ||
| 60 | pins = [] | ||
| 61 | |||
| 62 | if 'direct' in matrix_pins: | ||
| 63 | pins.append(direct_pins(matrix_pins['direct'])) | ||
| 64 | |||
| 65 | if 'cols' in matrix_pins: | ||
| 66 | pins.append(pin_array('MATRIX_COL', matrix_pins['cols'])) | ||
| 67 | |||
| 68 | if 'rows' in matrix_pins: | ||
| 69 | pins.append(pin_array('MATRIX_ROW', matrix_pins['rows'])) | ||
| 70 | |||
| 71 | return '\n'.join(pins) | ||
| 72 | |||
| 73 | |||
| 74 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 75 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 76 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 77 | @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) | ||
| 78 | @automagic_keyboard | ||
| 79 | @automagic_keymap | ||
| 80 | def generate_config_h(cli): | ||
| 81 | """Generates the info_config.h file. | ||
| 82 | """ | ||
| 83 | # Determine our keyboard(s) | ||
| 84 | if not cli.config.generate_config_h.keyboard: | ||
| 85 | cli.log.error('Missing paramater: --keyboard') | ||
| 86 | cli.subcommands['info'].print_help() | ||
| 87 | return False | ||
| 88 | |||
| 89 | if not is_keyboard(cli.config.generate_config_h.keyboard): | ||
| 90 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) | ||
| 91 | return False | ||
| 92 | |||
| 93 | # Build the info_config.h file. | ||
| 94 | kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard)) | ||
| 95 | info_config_map = _json_load(Path('data/mappings/info_config.json')) | ||
| 96 | |||
| 97 | config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] | ||
| 98 | |||
| 99 | # Iterate through the info_config map to generate basic things | ||
| 100 | for config_key, info_dict in info_config_map.items(): | ||
| 101 | info_key = info_dict['info_key'] | ||
| 102 | key_type = info_dict.get('value_type', 'str') | ||
| 103 | to_config = info_dict.get('to_config', True) | ||
| 104 | |||
| 105 | if not to_config: | ||
| 106 | continue | ||
| 107 | |||
| 108 | try: | ||
| 109 | config_value = kb_info_json[info_key] | ||
| 110 | except KeyError: | ||
| 111 | continue | ||
| 112 | |||
| 113 | if key_type.startswith('array'): | ||
| 114 | config_h_lines.append('') | ||
| 115 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 116 | config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}') | ||
| 117 | config_h_lines.append(f'#endif // {config_key}') | ||
| 118 | elif key_type == 'bool': | ||
| 119 | if config_value: | ||
| 120 | config_h_lines.append('') | ||
| 121 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 122 | config_h_lines.append(f'# define {config_key}') | ||
| 123 | config_h_lines.append(f'#endif // {config_key}') | ||
| 124 | elif key_type == 'mapping': | ||
| 125 | for key, value in config_value.items(): | ||
| 126 | config_h_lines.append('') | ||
| 127 | config_h_lines.append(f'#ifndef {key}') | ||
| 128 | config_h_lines.append(f'# define {key} {value}') | ||
| 129 | config_h_lines.append(f'#endif // {key}') | ||
| 130 | else: | ||
| 131 | config_h_lines.append('') | ||
| 132 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 133 | config_h_lines.append(f'# define {config_key} {config_value}') | ||
| 134 | config_h_lines.append(f'#endif // {config_key}') | ||
| 135 | |||
| 136 | if 'matrix_pins' in kb_info_json: | ||
| 137 | config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) | ||
| 138 | |||
| 139 | # Show the results | ||
| 140 | config_h = '\n'.join(config_h_lines) | ||
| 141 | |||
| 142 | if cli.args.output: | ||
| 143 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 144 | if cli.args.output.exists(): | ||
| 145 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 146 | cli.args.output.write_text(config_h) | ||
| 147 | |||
| 148 | if not cli.args.quiet: | ||
| 149 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 150 | |||
| 151 | else: | ||
| 152 | print(config_h) | ||
diff --git a/lib/python/qmk/cli/generate/info_json.py b/lib/python/qmk/cli/generate/info_json.py new file mode 100755 index 000000000..f3fc54ddc --- /dev/null +++ b/lib/python/qmk/cli/generate/info_json.py | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | """Keyboard information script. | ||
| 2 | |||
| 3 | Compile an info.json for a particular keyboard and pretty-print it. | ||
| 4 | """ | ||
| 5 | import json | ||
| 6 | |||
| 7 | from jsonschema import Draft7Validator, validators | ||
| 8 | from milc import cli | ||
| 9 | |||
| 10 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 11 | from qmk.info import info_json, _jsonschema | ||
| 12 | from qmk.info_json_encoder import InfoJSONEncoder | ||
| 13 | from qmk.path import is_keyboard | ||
| 14 | |||
| 15 | |||
| 16 | def pruning_validator(validator_class): | ||
| 17 | """Extends Draft7Validator to remove properties that aren't specified in the schema. | ||
| 18 | """ | ||
| 19 | validate_properties = validator_class.VALIDATORS["properties"] | ||
| 20 | |||
| 21 | def remove_additional_properties(validator, properties, instance, schema): | ||
| 22 | for prop in list(instance.keys()): | ||
| 23 | if prop not in properties: | ||
| 24 | del instance[prop] | ||
| 25 | |||
| 26 | for error in validate_properties(validator, properties, instance, schema): | ||
| 27 | yield error | ||
| 28 | |||
| 29 | return validators.extend(validator_class, {"properties": remove_additional_properties}) | ||
| 30 | |||
| 31 | |||
| 32 | def strip_info_json(kb_info_json): | ||
| 33 | """Remove the API-only properties from the info.json. | ||
| 34 | """ | ||
| 35 | pruning_draft_7_validator = pruning_validator(Draft7Validator) | ||
| 36 | schema = _jsonschema('keyboard') | ||
| 37 | validator = pruning_draft_7_validator(schema).validate | ||
| 38 | |||
| 39 | return validator(kb_info_json) | ||
| 40 | |||
| 41 | |||
| 42 | @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.') | ||
| 43 | @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.') | ||
| 44 | @cli.subcommand('Generate an info.json file for a keyboard.', hidden=False if cli.config.user.developer else True) | ||
| 45 | @automagic_keyboard | ||
| 46 | @automagic_keymap | ||
| 47 | def generate_info_json(cli): | ||
| 48 | """Generate an info.json file for a keyboard | ||
| 49 | """ | ||
| 50 | # Determine our keyboard(s) | ||
| 51 | if not cli.config.generate_info_json.keyboard: | ||
| 52 | cli.log.error('Missing parameter: --keyboard') | ||
| 53 | cli.subcommands['info'].print_help() | ||
| 54 | return False | ||
| 55 | |||
| 56 | if not is_keyboard(cli.config.generate_info_json.keyboard): | ||
| 57 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard) | ||
| 58 | return False | ||
| 59 | |||
| 60 | # Build the info.json file | ||
| 61 | kb_info_json = info_json(cli.config.generate_info_json.keyboard) | ||
| 62 | strip_info_json(kb_info_json) | ||
| 63 | |||
| 64 | # Display the results | ||
| 65 | print(json.dumps(kb_info_json, indent=2, cls=InfoJSONEncoder)) | ||
diff --git a/lib/python/qmk/cli/generate/layouts.py b/lib/python/qmk/cli/generate/layouts.py new file mode 100755 index 000000000..b7baae065 --- /dev/null +++ b/lib/python/qmk/cli/generate/layouts.py | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | """Used by the make system to generate layouts.h from info.json. | ||
| 2 | """ | ||
| 3 | from milc import cli | ||
| 4 | |||
| 5 | from qmk.constants import COL_LETTERS, ROW_LETTERS | ||
| 6 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 7 | from qmk.info import info_json | ||
| 8 | from qmk.path import is_keyboard, normpath | ||
| 9 | |||
| 10 | usb_properties = { | ||
| 11 | 'vid': 'VENDOR_ID', | ||
| 12 | 'pid': 'PRODUCT_ID', | ||
| 13 | 'device_ver': 'DEVICE_VER', | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 18 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 19 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 20 | @cli.subcommand('Used by the make system to generate layouts.h from info.json', hidden=True) | ||
| 21 | @automagic_keyboard | ||
| 22 | @automagic_keymap | ||
| 23 | def generate_layouts(cli): | ||
| 24 | """Generates the layouts.h file. | ||
| 25 | """ | ||
| 26 | # Determine our keyboard(s) | ||
| 27 | if not cli.config.generate_layouts.keyboard: | ||
| 28 | cli.log.error('Missing paramater: --keyboard') | ||
| 29 | cli.subcommands['info'].print_help() | ||
| 30 | return False | ||
| 31 | |||
| 32 | if not is_keyboard(cli.config.generate_layouts.keyboard): | ||
| 33 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_layouts.keyboard) | ||
| 34 | return False | ||
| 35 | |||
| 36 | # Build the info.json file | ||
| 37 | kb_info_json = info_json(cli.config.generate_layouts.keyboard) | ||
| 38 | |||
| 39 | # Build the layouts.h file. | ||
| 40 | layouts_h_lines = ['/* This file was generated by `qmk generate-layouts`. Do not edit or copy.' ' */', '', '#pragma once'] | ||
| 41 | |||
| 42 | if 'matrix_pins' in kb_info_json: | ||
| 43 | if 'direct' in kb_info_json['matrix_pins']: | ||
| 44 | col_num = len(kb_info_json['matrix_pins']['direct'][0]) | ||
| 45 | row_num = len(kb_info_json['matrix_pins']['direct']) | ||
| 46 | elif 'cols' in kb_info_json['matrix_pins'] and 'rows' in kb_info_json['matrix_pins']: | ||
| 47 | col_num = len(kb_info_json['matrix_pins']['cols']) | ||
| 48 | row_num = len(kb_info_json['matrix_pins']['rows']) | ||
| 49 | else: | ||
| 50 | cli.log.error('%s: Invalid matrix config.', cli.config.generate_layouts.keyboard) | ||
| 51 | return False | ||
| 52 | |||
| 53 | for layout_name in kb_info_json['layouts']: | ||
| 54 | if kb_info_json['layouts'][layout_name]['c_macro']: | ||
| 55 | continue | ||
| 56 | |||
| 57 | if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]: | ||
| 58 | cli.log.debug('%s/%s: No matrix data!', cli.config.generate_layouts.keyboard, layout_name) | ||
| 59 | continue | ||
| 60 | |||
| 61 | layout_keys = [] | ||
| 62 | layout_matrix = [['KC_NO' for i in range(col_num)] for i in range(row_num)] | ||
| 63 | |||
| 64 | for i, key in enumerate(kb_info_json['layouts'][layout_name]['layout']): | ||
| 65 | row = key['matrix'][0] | ||
| 66 | col = key['matrix'][1] | ||
| 67 | identifier = 'k%s%s' % (ROW_LETTERS[row], COL_LETTERS[col]) | ||
| 68 | |||
| 69 | try: | ||
| 70 | layout_matrix[row][col] = identifier | ||
| 71 | layout_keys.append(identifier) | ||
| 72 | except IndexError: | ||
| 73 | key_name = key.get('label', identifier) | ||
| 74 | cli.log.error('Matrix data out of bounds for layout %s at index %s (%s): %s, %s', layout_name, i, key_name, row, col) | ||
| 75 | return False | ||
| 76 | |||
| 77 | layouts_h_lines.append('') | ||
| 78 | layouts_h_lines.append('#define %s(%s) {\\' % (layout_name, ', '.join(layout_keys))) | ||
| 79 | |||
| 80 | rows = ', \\\n'.join(['\t {' + ', '.join(row) + '}' for row in layout_matrix]) | ||
| 81 | rows += ' \\' | ||
| 82 | layouts_h_lines.append(rows) | ||
| 83 | layouts_h_lines.append('}') | ||
| 84 | |||
| 85 | # Show the results | ||
| 86 | layouts_h = '\n'.join(layouts_h_lines) + '\n' | ||
| 87 | |||
| 88 | if cli.args.output: | ||
| 89 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 90 | if cli.args.output.exists(): | ||
| 91 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 92 | cli.args.output.write_text(layouts_h) | ||
| 93 | |||
| 94 | if not cli.args.quiet: | ||
| 95 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | ||
| 96 | |||
| 97 | else: | ||
| 98 | print(layouts_h) | ||
diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py new file mode 100755 index 000000000..af740f341 --- /dev/null +++ b/lib/python/qmk/cli/generate/rules_mk.py | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | """Used by the make system to generate a rules.mk | ||
| 2 | """ | ||
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from dotty_dict import dotty | ||
| 6 | from milc import cli | ||
| 7 | |||
| 8 | from qmk.decorators import automagic_keyboard, automagic_keymap | ||
| 9 | from qmk.info import _json_load, info_json | ||
| 10 | from qmk.path import is_keyboard, normpath | ||
| 11 | |||
| 12 | |||
| 13 | def process_mapping_rule(kb_info_json, rules_key, info_dict): | ||
| 14 | """Return the rules.mk line(s) for a mapping rule. | ||
| 15 | """ | ||
| 16 | if not info_dict.get('to_c', True): | ||
| 17 | return None | ||
| 18 | |||
| 19 | info_key = info_dict['info_key'] | ||
| 20 | key_type = info_dict.get('value_type', 'str') | ||
| 21 | |||
| 22 | try: | ||
| 23 | rules_value = kb_info_json[info_key] | ||
| 24 | except KeyError: | ||
| 25 | return None | ||
| 26 | |||
| 27 | if key_type == 'array': | ||
| 28 | return f'{rules_key} ?= {" ".join(rules_value)}' | ||
| 29 | elif key_type == 'bool': | ||
| 30 | return f'{rules_key} ?= {"on" if rules_value else "off"}' | ||
| 31 | elif key_type == 'mapping': | ||
| 32 | return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()]) | ||
| 33 | |||
| 34 | return f'{rules_key} ?= {rules_value}' | ||
| 35 | |||
| 36 | |||
| 37 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | ||
| 38 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | ||
| 39 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | ||
| 40 | @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True) | ||
| 41 | @automagic_keyboard | ||
| 42 | @automagic_keymap | ||
| 43 | def generate_rules_mk(cli): | ||
| 44 | """Generates a rules.mk file from info.json. | ||
| 45 | """ | ||
| 46 | if not cli.config.generate_rules_mk.keyboard: | ||
| 47 | cli.log.error('Missing paramater: --keyboard') | ||
| 48 | cli.subcommands['info'].print_help() | ||
| 49 | return False | ||
| 50 | |||
| 51 | if not is_keyboard(cli.config.generate_rules_mk.keyboard): | ||
| 52 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) | ||
| 53 | return False | ||
| 54 | |||
| 55 | kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard)) | ||
| 56 | info_rules_map = _json_load(Path('data/mappings/info_rules.json')) | ||
| 57 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] | ||
| 58 | |||
| 59 | # Iterate through the info_rules map to generate basic rules | ||
| 60 | for rules_key, info_dict in info_rules_map.items(): | ||
| 61 | new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict) | ||
| 62 | |||
| 63 | if new_entry: | ||
| 64 | rules_mk_lines.append(new_entry) | ||
| 65 | |||
| 66 | # Iterate through features to enable/disable them | ||
| 67 | if 'features' in kb_info_json: | ||
| 68 | for feature, enabled in kb_info_json['features'].items(): | ||
| 69 | if feature == 'bootmagic_lite' and enabled: | ||
| 70 | rules_mk_lines.append('BOOTMAGIC_ENABLE ?= lite') | ||
| 71 | else: | ||
| 72 | feature = feature.upper() | ||
| 73 | enabled = 'yes' if enabled else 'no' | ||
| 74 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') | ||
| 75 | |||
| 76 | # Show the results | ||
| 77 | rules_mk = '\n'.join(rules_mk_lines) + '\n' | ||
| 78 | |||
| 79 | if cli.args.output: | ||
| 80 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 81 | if cli.args.output.exists(): | ||
| 82 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 83 | cli.args.output.write_text(rules_mk) | ||
| 84 | |||
| 85 | if cli.args.quiet: | ||
| 86 | print(cli.args.output) | ||
| 87 | else: | ||
| 88 | cli.log.info('Wrote rules.mk to %s.', cli.args.output) | ||
| 89 | |||
| 90 | else: | ||
| 91 | print(rules_mk) | ||
