feat(code-format): minor enhancements (#7311)

Co-authored-by: Liam <30486941+liamHowatt@users.noreply.github.com>
This commit is contained in:
Victor Wheeler 2024-11-21 13:59:30 -07:00 committed by GitHub
parent a848a0bf1d
commit afccd777da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 9 deletions

View File

@ -18,7 +18,14 @@
--min-conditional-indent=0 --min-conditional-indent=0
--max-continuation-indent=120 --max-continuation-indent=120
--mode=c --mode=c
--lineend=linux # Allows each platform to use its own line endings. This then does
# what Git does out of the box: uses platform line endings in the
# working directory, but uses only LF line endings in the repository.
# `astyle` also natively uses line the endings it finds in the files,
# so this will be LF-only on Linux and CR/LF on Windows, in alignment
# with Git's default behavior. This prevents `astyle` from modifying
# every source file, which Git perceives as a change on Windows platforms.
#--lineend=linux
--suffix=none --suffix=none
--preserve-date --preserve-date
--formatted --formatted

View File

@ -1,19 +1,65 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import sys
# Argument enhancement: to only run `astyle` on a specified directory, to
# only include changed source code, these arguments have been added. If
# run with no arguments, all the normal directories are examined as before:
# - /demos/
# - /examples/
# - /src/
# - /tests/
#
# Args:
# If ANY args are specified, ONLY run `astyle` on the specified directories.
# Any combination can be specified.
include_demos = True
include_examples = True
include_src = True
include_tests = True
args = sys.argv[1:]
# Have any args been specified?
if args:
include_demos = False
include_examples = False
include_src = False
include_tests = False
for arg in args:
if arg == "demos":
include_demos = True
elif arg == "examples":
include_examples = True
elif arg == "src":
include_src = True
elif arg == "tests":
include_tests = True
else:
print(f'Argument [{arg}] not recognized.')
print('Usage:')
print(' python code-format.py [dir [dir ...]]')
print(' where: dir can be demos, examples, src or tests.')
exit(1)
script_dir = os.path.realpath(__file__) script_dir = os.path.realpath(__file__)
script_dir = os.path.dirname(script_dir) script_dir = os.path.dirname(script_dir)
cfg_file = os.path.join(script_dir, 'code-format.cfg') cfg_file = os.path.join(script_dir, 'code-format.cfg')
print("\nFormatting demos") if include_demos:
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../demos/*.c,*.cpp,*.h"') print("\nFormatting demos")
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../demos/*.c,*.cpp,*.h"')
print("\nFormatting examples") if include_examples:
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../examples/*.c,*.cpp,*.h"') print("\nFormatting examples")
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../examples/*.c,*.cpp,*.h"')
print("Formatting src") if include_src:
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../src/*.c,*.cpp,*.h"') print("\nFormatting src")
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../src/*.c,*.cpp,*.h"')
print("\nFormatting tests") if include_tests:
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../tests/*.c,*.cpp,*.h"') print("\nFormatting tests")
os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../tests/*.c,*.cpp,*.h"')