Skip to content

Commit

Permalink
Merge pull request #92 from jakkra/auto_code_format
Browse files Browse the repository at this point in the history
West extention for code formattng and github actions to check format before merge
  • Loading branch information
jakkra authored Oct 18, 2023
2 parents a565789 + ad97a49 commit 1c1bc41
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions app/scripts/astyle_format_west_command.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 6 additions & 1 deletion app/scripts/west-commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion app/tools/astyle.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
--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
8 changes: 0 additions & 8 deletions app/tools/format_code.sh
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 1c1bc41

Please sign in to comment.