From 95477749629407e2a9e33c6ccf26ecc8b24ab07a Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 10:18:52 -0700 Subject: CLI command to format C code --- lib/python/qmk/cli/cformat.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/python/qmk/cli/cformat.py (limited to 'lib/python/qmk/cli/cformat.py') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py new file mode 100644 index 0000000000..f7020f4c5f --- /dev/null +++ b/lib/python/qmk/cli/cformat.py @@ -0,0 +1,27 @@ +"""Format C code according to QMK's style. +""" +import os +import subprocess + +from milc import cli + + +@cli.entrypoint("Format C code according to QMK's style.") +def main(cli): + """Format C code according to QMK's style. + """ + clang_format = ['clang-format', '-i'] + code_files = [] + for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: + for dirpath, dirnames, filenames in os.walk(dir): + if 'tmk_core/protocol/usb_hid' in dirpath: + continue + for name in filenames: + if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): + code_files.append(os.path.join(dirpath, name)) + + try: + subprocess.run(clang_format + code_files, check=True) + cli.log.info('Successfully formatted the C code.') + except subprocess.CalledProcessError: + cli.log.error('Error formatting C code!') -- cgit v1.2.3 From 1784d1bfac44a63bf343b6e2098f0cba81d58cb2 Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 13:30:50 -0700 Subject: Add support for passing files at the command line --- lib/python/qmk/cli/cformat.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'lib/python/qmk/cli/cformat.py') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index f7020f4c5f..0c209247d9 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -6,22 +6,24 @@ import subprocess from milc import cli +@cli.argument('files', nargs='*', help='Filename(s) to format.') @cli.entrypoint("Format C code according to QMK's style.") def main(cli): """Format C code according to QMK's style. """ clang_format = ['clang-format', '-i'] - code_files = [] - for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: - for dirpath, dirnames, filenames in os.walk(dir): - if 'tmk_core/protocol/usb_hid' in dirpath: - continue - for name in filenames: - if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): - code_files.append(os.path.join(dirpath, name)) + if not cli.args.files: + for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: + for dirpath, dirnames, filenames in os.walk(dir): + if 'tmk_core/protocol/usb_hid' in dirpath: + continue + for name in filenames: + if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): + cli.args.files.append(os.path.join(dirpath, name)) try: - subprocess.run(clang_format + code_files, check=True) + subprocess.run(clang_format + cli.args.files, check=True) cli.log.info('Successfully formatted the C code.') except subprocess.CalledProcessError: cli.log.error('Error formatting C code!') + return False -- cgit v1.2.3 From 2d688ad14e727bd3437f26a53bd3d92079e5b3c2 Mon Sep 17 00:00:00 2001 From: skullY Date: Thu, 22 Aug 2019 13:33:34 -0700 Subject: readability enhancements --- lib/python/qmk/cli/cformat.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/python/qmk/cli/cformat.py') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index 0c209247d9..91e650368b 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -12,18 +12,23 @@ def main(cli): """Format C code according to QMK's style. """ clang_format = ['clang-format', '-i'] + + # Find the list of files to format if not cli.args.files: for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: for dirpath, dirnames, filenames in os.walk(dir): if 'tmk_core/protocol/usb_hid' in dirpath: continue + for name in filenames: if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): cli.args.files.append(os.path.join(dirpath, name)) + # Run clang-format on the files we've found try: subprocess.run(clang_format + cli.args.files, check=True) cli.log.info('Successfully formatted the C code.') + except subprocess.CalledProcessError: cli.log.error('Error formatting C code!') return False -- cgit v1.2.3 From d569f0877155efc752994f8a21f5cf001f9d6ae6 Mon Sep 17 00:00:00 2001 From: skullydazed Date: Sun, 22 Sep 2019 13:25:33 -0700 Subject: Configuration system for CLI (#6708) * Rework how bin/qmk handles subcommands * qmk config wip * Code to show all configs * Fully working `qmk config` command * Mark some CLI arguments so they don't pollute the config file * Fleshed out config support, nicer subcommand support * sync with installable cli * pyformat * Add a test for subcommand_modules * Documentation for the `qmk config` command * split config_token on space so qmk config is more predictable * Rework how subcommands are imported * Document `arg_only` * Document deleting from CLI * Document how multiple operations work * Add cli config to the doc index * Add tests for the cli commands * Make running the tests more reliable * Be more selective about building all default keymaps * Update new-keymap to fit the new subcommand style * Add documentation about writing CLI scripts * Document new-keyboard * Update docs/cli_configuration.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Update docs/cli_development.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Update docs/cli_development.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Update docs/cli_development.md Co-Authored-By: noroadsleft <18669334+noroadsleft@users.noreply.github.com> * Address yan's comments. * Apply suggestions from code review suggestions from @noahfrederick Co-Authored-By: Noah Frederick * Apply suggestions from code review Co-Authored-By: Noah Frederick * Remove pip3 from the test runner --- lib/python/qmk/cli/cformat.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/python/qmk/cli/cformat.py') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index 91e650368b..d2382bdbde 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -6,9 +6,9 @@ import subprocess from milc import cli -@cli.argument('files', nargs='*', help='Filename(s) to format.') -@cli.entrypoint("Format C code according to QMK's style.") -def main(cli): +@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.') +@cli.subcommand("Format C code according to QMK's style.") +def cformat(cli): """Format C code according to QMK's style. """ clang_format = ['clang-format', '-i'] -- cgit v1.2.3 From 5421ba11dedd9912967b1fa5ed1f664687221143 Mon Sep 17 00:00:00 2001 From: skullY Date: Tue, 12 Nov 2019 17:17:12 -0800 Subject: Add support for newer versions of clang-format, if installed --- lib/python/qmk/cli/cformat.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/python/qmk/cli/cformat.py') diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py index d2382bdbde..17ca91b3b5 100644 --- a/lib/python/qmk/cli/cformat.py +++ b/lib/python/qmk/cli/cformat.py @@ -2,6 +2,7 @@ """ import os import subprocess +from shutil import which from milc import cli @@ -11,10 +12,18 @@ from milc import cli def cformat(cli): """Format C code according to QMK's style. """ + # Determine which version of clang-format to use clang_format = ['clang-format', '-i'] + for clang_version in [10, 9, 8, 7]: + binary = 'clang-format-%d' % clang_version + if which(binary): + clang_format[0] = binary + break # Find the list of files to format - if not cli.args.files: + if cli.args.files: + cli.args.files = [os.path.join(os.environ['ORIG_CWD'], file) for file in cli.args.files] + else: for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: for dirpath, dirnames, filenames in os.walk(dir): if 'tmk_core/protocol/usb_hid' in dirpath: -- cgit v1.2.3