Skip to content

Commit

Permalink
unify the compile and flash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
skullydazed committed Jun 27, 2021
1 parent b911645 commit 7509c21
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 79 deletions.
30 changes: 29 additions & 1 deletion lib/python/qmk/cli/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from qmk.commands import do_compile
from qmk.keyboard import keyboard_completer, is_keyboard_target
from qmk.keymap import keymap_completer
from qmk.metadata import true_values, false_values


@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='The configurator export to compile')
Expand All @@ -34,4 +35,31 @@ def compile(cli):
If a keyboard and keymap are provided this command will build a firmware based on that.
"""
do_compile(cli.config.compile.keyboard, cli.config.compile.keymap, cli.config.compile.parallel, cli.config.compile.target)
# If -f has been specified without a keyboard target, assume -kb all
keyboard = cli.config.compile.keyboard or ''

if cli.args.filter and not cli.args.keyboard:
cli.log.debug('--filter supplied without --keyboard, assuming --keyboard all.')
keyboard = 'all'

if cli.args.filename and cli.args.filter:
cli.log.warning('Ignoring --filter because a keymap.json was provided.')

filters = {}

for filter in cli.args.filter:
if '=' in filter:
key, value = filter.split('=', 1)

if value in true_values:
value = True
elif value in false_values:
value = False
elif value.isdigit():
value = int(value)
elif '.' in value and value.replace('.').isdigit():
value = float(value)

filters[key] = value

return do_compile(keyboard, cli.config.compile.keymap, cli.config.compile.parallel, cli.config.compile.target, filters)
51 changes: 3 additions & 48 deletions lib/python/qmk/cli/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
from qmk.commands import do_compile
from qmk.keyboard import keyboard_completer, is_keyboard_target


Expand Down Expand Up @@ -54,55 +54,10 @@ def flash(cli):
If bootloader is omitted the make system will use the configured bootloader for that keyboard.
"""
if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean')
cli.run(command, capture_output=False, stdin=DEVNULL)

# Build the environment vars
envs = {}
for env in cli.args.env:
if '=' in env:
key, value = env.split('=', 1)
envs[key] = value
else:
cli.log.warning('Invalid environment variable: %s', env)

# Determine the compile command
command = ''

if cli.args.bootloaders:
# Provide usage and list bootloaders
cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
cli.print_usage()
print_bootloader_help()
return False

if cli.args.filename:
# Handle compiling a configurator JSON
user_keymap = parse_configurator_json(cli.args.filename)
keymap_path = qmk.path.keymap(user_keymap['keyboard'])
command = compile_configurator_json(user_keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)

cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])

else:
if cli.config.flash.keyboard and cli.config.flash.keymap:
# Generate the make command for a specific keyboard/keymap.
command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)

elif not cli.config.flash.keyboard:
cli.log.error('Could not determine keyboard!')
elif not cli.config.flash.keymap:
cli.log.error('Could not determine keymap!')

# Compile the firmware, if we're able to
if command:
cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
if not cli.args.dry_run:
cli.echo('\n')
compile = cli.run(command, capture_output=False, stdin=DEVNULL)
return compile.returncode

else:
cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
return False
return do_compile(cli.config.flash.keyboard, cli.config.flash.keymap, cli.config.flash.parallel, cli.config.flash.bootloader)
47 changes: 17 additions & 30 deletions lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,20 @@ def parse_configurator_json(configurator_file):
return user_keymap


def do_compile(keyboard, keymap, parallel, target=None):
def do_compile(keyboard, keymap, parallel, target=None, filters=None, environment=None):
"""Shared code between `qmk compile` and `qmk flash`.
"""
if keyboard is None:
keyboard = ''

if environment is None:
environment = {}

envs = {'REQUIRE_PLATFORM_KEY': ''}
silent = keyboard == 'all' or keyboard.startswith('all-') or keymap == 'all'

# Setup the environment
for env in cli.args.env:
for env in environment:
if '=' in env:
key, value = env.split('=', 1)
if key in envs:
Expand All @@ -260,18 +263,10 @@ def do_compile(keyboard, keymap, parallel, target=None):
_, _, make_cmd = create_make_command(keyboard, keymap, 'clean', parallel, silent, **envs)
cli.run(make_cmd, capture_output=False, stdin=DEVNULL)

# If -f has been specified without a keyboard target, assume -kb all
if cli.args.filter and not cli.args.keyboard:
cli.log.debug('--filter supplied without --keyboard, assuming --keyboard all.')
keyboard = 'all'

# Determine the compile command(s)
commands = None

if cli.args.filename:
if cli.args.filter:
cli.log.warning('Ignoring --filter because a keymap.json was provided.')

if cli.args.keyboard:
cli.log.warning('Ignoring --keyboard because a keymap.json was provided.')

Expand All @@ -283,10 +278,10 @@ def do_compile(keyboard, keymap, parallel, target=None):
commands = [compile_configurator_json(user_keymap, parallel=parallel, **envs)]

elif keyboard and keymap:
if cli.args.filter:
if filters:
cli.log.info('Generating the list of keyboards to compile, this may take some time.')

commands = [create_make_command(keyboard, keymap, target=target, parallel=parallel, silent=silent, **envs) for keyboard, keymap in keyboard_keymap_iter(keyboard, keymap)]
commands = [create_make_command(keyboard, keymap, target=target, parallel=parallel, silent=silent, **envs) for keyboard, keymap in keyboard_keymap_iter(keyboard, keymap, filters)]

elif not keyboard:
cli.log.error('Could not determine keyboard!')
Expand Down Expand Up @@ -326,7 +321,7 @@ def do_compile(keyboard, keymap, parallel, target=None):
keyboard, keymap, command = commands[i]
cli.echo('\tkeyboard: {fg_cyan}%s{fg_reset} keymap: {fg_cyan}%s', keyboard, keymap)

elif cli.args.filter:
elif filters:
cli.log.error('No keyboards found after applying filter(s)!')
return False

Expand All @@ -346,34 +341,26 @@ def _keyboard_list(keyboard):
return [keyboard]


def keyboard_keymap_iter(cli_keyboard, cli_keymap):
def keyboard_keymap_iter(cli_keyboard, cli_keymap, filters):
"""Iterates over the keyboard/keymap for this command and yields a pairing of each.
"""
for keyboard in _keyboard_list(cli_keyboard):
continue_flag = False
if cli.args.filter:

if filters:
info_data = dotty(info_json(keyboard))
for filter in cli.args.filter:
if '=' in filter:
key, value = filter.split('=', 1)
if value in true_values:
value = True
elif value in false_values:
value = False
elif value.isdigit():
value = int(value)
elif '.' in value and value.replace('.').isdigit():
value = float(value)

if info_data.get(key) != value:
continue_flag = True
break

for key, value in filters.items():
if info_data.get(key) != value:
continue_flag = True
break

if continue_flag:
continue

if cli_keymap == 'all':
for keymap in qmk.keymap.list_keymaps(keyboard):
yield keyboard, keymap

else:
yield keyboard, cli_keymap

0 comments on commit 7509c21

Please sign in to comment.