-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to a composite workflow instead of a Docker-based Action so it…
… can be used in a macOS runner too. Also add an option to control which files are formatted via regex and switch to using entrypoint.py instead of entrypoint.sh (thanks to @JBludau for the inspiration on this). Removed the current workflow too. Probably worth creating a new one soon...
- Loading branch information
1 parent
fb55982
commit b0de67a
Showing
6 changed files
with
88 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ To use this action, pass arguments to the `args` element as you would to `cmake- | |
```yaml | ||
- name: Format CMake files | ||
id: cmake-format | ||
uses: PuneetMatharu/[email protected].4 | ||
uses: puneetmatharu/[email protected].5 | ||
with: | ||
# Arguments to be passed to cmake-format. | ||
# | ||
|
@@ -31,6 +31,11 @@ To use this action, pass arguments to the `args` element as you would to `cmake- | |
# -c CONFIG_FILES [CONFIG_FILES ...], --config-files CONFIG_FILES [CONFIG_FILES ...] | ||
# path to configuration file(s) | ||
args: --config-files .cmake-format.json --in-place | ||
|
||
# Regex to select which files to apply cmake-format on. | ||
# | ||
# Defaults to '(.*\.cmake$|CMakeLists.txt$)' | ||
file-regex: '(.*\.cmake$|.*\.cmake\.in$|CMakeLists.txt$)' | ||
``` | ||
You will probably want to pair this with a GitHub Action (such as | ||
|
@@ -52,12 +57,13 @@ jobs: | |
- name: Format CMake files | ||
id: cmake-format | ||
uses: PuneetMatharu/[email protected].4 | ||
uses: puneetmatharu/[email protected].5 | ||
with: | ||
args: --config-files .cmake-format.json --in-place | ||
file-regex: '(.*\.cmake$|.*\.cmake\.in$|CMakeLists.txt$)' | ||
- name: Commit changes | ||
uses: stefanzweifel/git-auto-commit-action@v4 | ||
uses: stefanzweifel/git-auto-commit-action@v5 | ||
with: | ||
commit_user_name: cmake-format-bot | ||
commit_message: 'Automated commit of cmake-format changes.' | ||
|
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,49 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import os | ||
import re | ||
import subprocess | ||
from pathlib import Path | ||
from multiprocessing import Pool | ||
from typing import Tuple, Optional | ||
|
||
|
||
def format_cmake_file(file_and_args: Tuple[Path, str]) -> Optional[str]: | ||
(file_path, cmake_format_args) = file_and_args | ||
cmake_command = f"cmake-format {cmake_format_args} {file_path}" | ||
cmake_format_result = subprocess.run(cmake_command, shell=True, capture_output=True, text=True) | ||
if cmake_format_result.returncode != 0: | ||
return f"Error formatting {file_path}: {cmake_format_result.stderr}" | ||
return f"Successfully formatted {file_path}!" | ||
|
||
|
||
def parse_args() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser(description="Format CMake files using cmake-format.") | ||
parser.add_argument('--file-regex', required=True, help="Regex pattern to match filenames.") | ||
parser.add_argument('--cmake-format-args', required=True, help="Additional arguments for cmake-format.") | ||
args = parser.parse_args() | ||
if len(args.file_regex) == 0: | ||
parser.error("The --file-regex argument cannot be empty.") | ||
return args | ||
|
||
|
||
if __name__ == "__main__": | ||
args = parse_args() | ||
|
||
file_regex = re.compile(args.file_regex) | ||
workspace_path = Path(os.environ['GITHUB_WORKSPACE']) | ||
|
||
matched_files = [ | ||
(file_path, args.cmake_format_args) | ||
for file_path in workspace_path.rglob('*') | ||
if file_path.is_file() and file_regex.match(file_path.name) | ||
] | ||
|
||
if len(matched_files) > 0: | ||
with Pool() as pool: | ||
format_results = pool.map(format_cmake_file, matched_files) | ||
for result in format_results: | ||
print(result) | ||
else: | ||
print("No files matched the given regex.") |
This file was deleted.
Oops, something went wrong.