diff --git a/scripts/code-format.cfg b/scripts/code-format.cfg index 0f6fe5fd6..eb488758e 100644 --- a/scripts/code-format.cfg +++ b/scripts/code-format.cfg @@ -18,7 +18,14 @@ --min-conditional-indent=0 --max-continuation-indent=120 --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 --preserve-date --formatted diff --git a/scripts/code-format.py b/scripts/code-format.py index 97acc725d..c51f1cb5c 100755 --- a/scripts/code-format.py +++ b/scripts/code-format.py @@ -1,19 +1,65 @@ #!/usr/bin/env python3 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.dirname(script_dir) cfg_file = os.path.join(script_dir, 'code-format.cfg') -print("\nFormatting demos") -os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../demos/*.c,*.cpp,*.h"') +if include_demos: + print("\nFormatting demos") + os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../demos/*.c,*.cpp,*.h"') -print("\nFormatting examples") -os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../examples/*.c,*.cpp,*.h"') +if include_examples: + print("\nFormatting examples") + os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../examples/*.c,*.cpp,*.h"') -print("Formatting src") -os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../src/*.c,*.cpp,*.h"') +if include_src: + print("\nFormatting src") + os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../src/*.c,*.cpp,*.h"') -print("\nFormatting tests") -os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../tests/*.c,*.cpp,*.h"') +if include_tests: + print("\nFormatting tests") + os.system(f'astyle --options={cfg_file} --recursive "{script_dir}/../tests/*.c,*.cpp,*.h"')