Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Oct 18, 2023
1 parent a565789 commit aeb369c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 10 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions app/scripts/astyle_format_west_command.py
Original file line number Diff line number Diff line change
@@ -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)
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
21 changes: 21 additions & 0 deletions app/tools/astyle.py
Original file line number Diff line number Diff line change
@@ -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)
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 aeb369c

Please sign in to comment.