Skip to content

Commit bfcb235

Browse files
committed
Add exclude_file function
1 parent 3f8aa89 commit bfcb235

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/fprime/util/code_formatter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ def stage_file(self, filepath: Path) -> None:
7373
else:
7474
self._files_to_format.append(filepath)
7575

76+
def exclude_file(self, filepath: Path) -> None:
77+
"""Request ClangFormatter to exclude the file for formatting.
78+
If the file exists and its extension matches a known C/C++ format,
79+
it will be excluded to clang-format when the execute() function is called.
80+
81+
Args:
82+
filepath (str): file path to file to be excluded.
83+
"""
84+
if not filepath.is_file():
85+
if self.verbose:
86+
print(f"[INFO] Skipping {filepath} : is not a file.")
87+
elif filepath in self._files_to_format:
88+
self._files_to_format.remove(filepath)
89+
7690
def execute(
7791
self, builder: "Build", context: "Path", args: Tuple[Dict[str, str], List[str]]
7892
):

src/fprime/util/commands.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,25 @@ def run_code_format(
192192
# Stage all files that are passed through --files
193193
for filename in parsed.files:
194194
clang_formatter.stage_file(Path(filename))
195-
# List all excluded directories
196-
excluded_dirs = [Path(directory) for directory in parsed.exclude]
197195
# Search for files within --dirs and stage them
198196
for dirname in parsed.dirs:
199197
dir_path = Path(dirname)
200198
if not dir_path.is_dir():
201199
print(f"[INFO] {dir_path} is not a directory. Skipping.")
202200
continue
203-
if dir_path in excluded_dirs:
204-
print(f"[INFO] Excluded {dir_path} from formatting.")
205-
continue
206201
for allowed_ext in clang_formatter.allowed_extensions:
207202
for file in dir_path.rglob(f"*{allowed_ext}"):
208203
clang_formatter.stage_file(file)
204+
# Remove staged files that are within excluded directories
205+
for excluded_dir in parsed.excluded:
206+
excluded_path = Path(excluded_dir)
207+
if not excluded_path.is_dir():
208+
print(f"[INFO] {excluded_path} is not a directory. Skipping it from excluded directories.")
209+
continue
210+
for allowed_ext in clang_formatter.allowed_extensions:
211+
for file in excluded_path.rglob(f"*{allowed_ext}"):
212+
clang_formatter.exclude_file(file)
213+
209214
return clang_formatter.execute(build, parsed.path, ({}, parsed.pass_through))
210215

211216

0 commit comments

Comments
 (0)