From 75f5a12f4d7a3b85f48ece206877aed079986bd3 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Wed, 29 May 2024 16:05:11 +0800 Subject: [PATCH] Update format_sources.py --- tools/format_sources.py | 62 ++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/tools/format_sources.py b/tools/format_sources.py index 1b9f9e45..42448976 100644 --- a/tools/format_sources.py +++ b/tools/format_sources.py @@ -3,30 +3,46 @@ import os import subprocess -# Base directory of the script -script_path = os.path.abspath(__file__) +def format_sources(): + """ + Formats the source code files in the ESP3D project using clang-format with Google style. -# Extract dir path -script_dir = os.path.dirname(script_path) + This script recursively searches for C, C++, H, and INO files in the ESP3D project directory + and its subdirectories. It then applies the clang-format tool to each file, using the Google + style for formatting. -# Build path of sources dir : ../esp3d -base_dir = os.path.abspath(os.path.normpath(os.path.join(script_dir, '..', 'esp3d'))) + Note: Make sure you have clang-format installed and available in your system's PATH. -# Parse all c, h , cpp files in all directories and sub directories -file_paths = [] -for root, dirs, files in os.walk(base_dir): - for file in files: - if file.endswith(('.c', '.cpp', '.h', '.ino')): - file_path = os.path.join(root, file) - file_paths.append(os.path.abspath(os.path.normpath(file_path))) + Returns: + None + """ + # Base directory of the script + script_path = os.path.abspath(__file__) -# Now format all files one by one with clang-format -for file_path in file_paths: - tmpPath = '"' + file_path + '"' - print("Formating " + tmpPath, end="") - try: - command = ['clang-format', '-i', '--style=Google', file_path] - subprocess.run(command, check=False) - print("=> Ok") - except subprocess.CalledProcessError as e: - print(f'=>Error : {e}') + # Extract dir path + script_dir = os.path.dirname(script_path) + + # Build path of sources dir: ../esp3d + base_dir = os.path.abspath(os.path.normpath(os.path.join(script_dir, '..', 'esp3d'))) + + # Parse all c, h, cpp, and ino files in all directories and subdirectories + file_paths = [] + for root, dirs, files in os.walk(base_dir): + for file in files: + if file.endswith(('.c', '.cpp', '.h', '.ino')): + file_path = os.path.join(root, file) + file_paths.append(os.path.abspath(os.path.normpath(file_path))) + + # Now format all files one by one with clang-format + for file_path in file_paths: + tmpPath = '"' + file_path + '"' + print("Formatting " + tmpPath, end="") + try: + command = ['clang-format', '-i', '--style=Google', file_path] + subprocess.run(command, check=False) + print(" => Ok") + except subprocess.CalledProcessError as e: + print(f'=> Error: {e}') + +# Call the format_sources function to format the source code +format_sources()