-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from jakkra/auto_code_format
West extention for code formattng and github actions to check format before merge
- Loading branch information
Showing
5 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |