From aeb369c6f7fb59481d0925fd72a6a544c1373733 Mon Sep 17 00:00:00 2001 From: Jakob Krantz Date: Wed, 18 Oct 2023 08:45:30 +0200 Subject: [PATCH] WIP --- .github/workflows/build.yml | 14 ++++++++ app/scripts/astyle_format_west_command.py | 44 +++++++++++++++++++++++ app/scripts/west-commands.yml | 7 +++- app/tools/astyle.cfg | 12 ++++++- app/tools/astyle.py | 21 +++++++++++ app/tools/format_code.sh | 8 ----- 6 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 app/scripts/astyle_format_west_command.py create mode 100644 app/tools/astyle.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4cb9fe3d..6068bbba 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,10 +21,24 @@ 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 + + - name: Style + working-directory: ZSWatch + run: | + west format --dry-run + + - name: West update + working-directory: ZSWatch + run: | west update -o=--depth=1 -n - name: Build firmware diff --git a/app/scripts/astyle_format_west_command.py b/app/scripts/astyle_format_west_command.py new file mode 100644 index 00000000..0c308cc7 --- /dev/null +++ b/app/scripts/astyle_format_west_command.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +'''Run AStyle on all code.''' + +from west.commands import WestCommand +from west import log +import subprocess +import sys + +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", type=bool, default=False, help="Dry run won't actually format the code" + ) + + return parser + + def do_run(self, args, unknown_args): + astyle_args = ["astyle", "--options=tools/astyle.cfg", "--recursive", "src/*.c,*.h", "--recursive", "drivers/*.c,*.h"] + if (args.dry_run == True): + astyle_args = astyle_args + "--dry-run" + with subprocess.Popen(["astyle", "--options=tools/astyle.cfg", "--recursive", "src/*.c,*.h", "--recursive", "drivers/*.c,*.h",], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True) as astyle: + fail = False + output = astyle.communicate() + for line in output: + if line.startswith("Formatted"): + fail = True + print (line, end="") + 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/astyle.py b/app/tools/astyle.py new file mode 100644 index 00000000..740d8b4a --- /dev/null +++ b/app/tools/astyle.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +'''Run AStyle on all code.''' + +# Run this from the root directory to run AStyle, + +import subprocess +import sys + +with subprocess.Popen(["astyle", "--options=tools/astyle.cfg", "--recursive", "src/*.c,*.h", "--recursive", "drivers/*.c,*.h", "--dry-run"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True) as astyle: + fail = False + output = astyle.communicate() + for line in output: + if line.startswith("Formatted"): + fail = True + print (line, end="") + if astyle.returncode != 0: + fail = True + sys.exit(1 if fail else 0) \ No newline at end of file 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