Skip to content

Commit

Permalink
Switch to a composite workflow instead of a Docker-based Action so it…
Browse files Browse the repository at this point in the history
… 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
puneetmatharu committed Dec 16, 2024
1 parent fb55982 commit b0de67a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 69 deletions.
29 changes: 0 additions & 29 deletions .github/workflows/CI.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions Dockerfile

This file was deleted.

12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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
Expand All @@ -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.'
Expand Down
41 changes: 30 additions & 11 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: 'cmake-format lint action'
author: 'Puneet Matharu'
description: 'A Github Action to format CMake-specific files using cmake-format (v0.6.13)'
name: "cmake-format lint action"
author: "Puneet Matharu"
description: "A GitHub Action to format CMake-specific files using cmake-format (v0.6.13)"
branding:
icon: 'code'
color: 'blue'
icon: "code"
color: "blue"

inputs:
args:
description: |
Expand All @@ -24,11 +25,29 @@ inputs:
Where to write the formatted file. Default is stdout.
-c CONFIG_FILES [CONFIG_FILES ...], --config-files CONFIG_FILES [CONFIG_FILES ...]
path to configuration file(s)
required: true
default: '--help'
default: "--help"

file-regex:
description: |
Regex to select which files to apply cmake-format on.
Defaults to '(.*\.cmake$|CMakeLists.txt$)'
required: false
default: '(.*\.cmake$|CMakeLists.txt$)'

runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.args }}
using: "composite"
steps:
- name: Set up python3
uses: actions/setup-python@v5
with:
python-version: 3.9

- name: Install cmake-format
shell: bash
run: python3 -m pip install --no-cache-dir "Cython<3" "cmakelang[YAML]==0.6.13"

- name: Format CMake files
shell: bash
run: python3 ${{ github.action_path }}/entrypoint.py --cmake-format-args '${{ inputs.args }}' --file-regex '${{ inputs.file-regex }}'
49 changes: 49 additions & 0 deletions entrypoint.py
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.")
7 changes: 0 additions & 7 deletions entrypoint.sh

This file was deleted.

0 comments on commit b0de67a

Please sign in to comment.