From ad97a49676752db256f23624aa1ca401e232967b Mon Sep 17 00:00:00 2001 From: Jakob Krantz Date: Wed, 18 Oct 2023 08:45:30 +0200 Subject: [PATCH] Add west extension 'west format' that run code formatting and new github actions to check format on PRs before they can be merged. Fixes https://github.com/jakkra/ZSWatch/issues/89 --- .github/workflows/build.yml | 10 ++++ app/scripts/astyle_format_west_command.py | 63 +++++++++++++++++++++++ app/scripts/west-commands.yml | 7 ++- app/tools/astyle.cfg | 12 ++++- app/tools/format_code.sh | 8 --- 5 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 app/scripts/astyle_format_west_command.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4cb9fe3d..3d43d600 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,11 +21,21 @@ jobs: path: ZSWatch submodules: recursive + - name: Dependencies + run: | + sudo apt update + sudo apt install astyle + - name: Initialize working-directory: ZSWatch run: | west init -l app west update -o=--depth=1 -n + + - name: Style + working-directory: ZSWatch + run: | + west format --dry-run - name: Build firmware working-directory: ZSWatch diff --git a/app/scripts/astyle_format_west_command.py b/app/scripts/astyle_format_west_command.py new file mode 100644 index 00000000..cf9a094b --- /dev/null +++ b/app/scripts/astyle_format_west_command.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +"""Run AStyle on all code.""" + +from west.commands import WestCommand +from west import log +import subprocess +import sys +import os + + +class FormatCodeWestCommand(WestCommand): + def __init__(self): + super().__init__( + "format", + "Format the codebase", + """Use to format all .c and .h giles""", + ) + + def do_add_parser(self, parser_adder): + parser = parser_adder.add_parser( + self.name, help=self.help, description=self.description + ) + + parser.add_argument( + "--dry-run", + action="store_true", + help="Dry run won't actually format the code.", + ) + + return parser + + def do_run(self, args, unknown_args): + astyle_args = ["astyle"] + if args.dry_run == True: + astyle_args.append("--dry-run") + astyle_args.extend( + [ + "--options={}/../app/tools/astyle.cfg".format( + os.environ["ZEPHYR_BASE"] + ), + "--recursive", + "{}/../app/src/*.c,*.h".format(os.environ["ZEPHYR_BASE"]), + "--recursive", + "{}/../app/drivers/*.c,*.h".format(os.environ["ZEPHYR_BASE"]), + ] + ) + with subprocess.Popen( + astyle_args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) as astyle: + fail = False + output = astyle.communicate() + output = output[0].split("\n") + for line in output: + if line.startswith("Formatted"): + fail = True + print(line, end="\n") + if astyle.returncode != 0: + fail = True + sys.exit(1 if fail else 0) diff --git a/app/scripts/west-commands.yml b/app/scripts/west-commands.yml index 652e3743..d641704e 100644 --- a/app/scripts/west-commands.yml +++ b/app/scripts/west-commands.yml @@ -3,4 +3,9 @@ west-commands: commands: - name: upload_fs class: UploadFsWestCommand - help: an example west extension command + help: Upload filesystem to external SPI flash + - file: scripts/astyle_format_west_command.py + commands: + - name: format + class: FormatCodeWestCommand + help: Format the code diff --git a/app/tools/astyle.cfg b/app/tools/astyle.cfg index eb16e5ad..b63a86f8 100644 --- a/app/tools/astyle.cfg +++ b/app/tools/astyle.cfg @@ -25,4 +25,14 @@ # When breaking lines, leave the logical operator on the end of the line --break-after-logical # Ignore unfound excludes ---ignore-exclude-errors \ No newline at end of file +--ignore-exclude-errors + +# Other config used +--suffix=none +--verbose +--errors-to-stdout +--exclude=src/images +--exclude=src/ext_drivers +--exclude=drivers/input +--exclude=drivers/sensor +--exclude=src/applications/2048/2048_lib diff --git a/app/tools/format_code.sh b/app/tools/format_code.sh index 18fbbb3c..8ecdcb72 100755 --- a/app/tools/format_code.sh +++ b/app/tools/format_code.sh @@ -1,13 +1,5 @@ astyle \ --options=tools/astyle.cfg \ ---suffix=none \ ---verbose \ ---errors-to-stdout \ --recursive src/*.c,*.h \ --recursive drivers/*.c,*.h \ ---exclude=src/images \ ---exclude=src/ext_drivers \ ---exclude=drivers/input \ ---exclude=drivers/sensor \ ---exclude=src/applications/2048/2048_lib \ $1 $2 $3 # addtional args such as --dry-run etc. \ No newline at end of file