Skip to content

Commit

Permalink
Add option to control which files are formatted via regex and switch …
Browse files Browse the repository at this point in the history
…to using entrypoint.py instead of entrypoint.sh.
  • Loading branch information
puneetmatharu committed Dec 16, 2024
1 parent fb55982 commit 824df29
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 49 deletions.
26 changes: 17 additions & 9 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@ name: CI

on:
push:
schedule:
- cron: 0 11 * * 2
pull_request:
branches:
- main
schedule:
- cron: "0 11 * * 2" # Every Tuesday at 11:00 UTC

jobs:
docker:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
# Checkout repository code
- name: Checkout code
uses: actions/checkout@v4

# Gather Docker metadata
- name: Gather metadata
id: meta
uses: docker/metadata-action@v4
with:
images: |
PuneetMatharu/cmake-format
images: PuneetMatharu/cmake-format

# Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and export to Docker

# Build and export Docker image
- name: Build and export Docker image
uses: docker/build-push-action@v4
with:
push: false
load: true
tags: ${{ steps.meta.outputs.tags }}
push: false # Don't push the image
load: true # Load the image into Docker daemon
tags: ${{ steps.meta.outputs.tags }} # Use metadata tags
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/cmake-format-lint-action@v1.0.4
uses: puneetmatharu/cmake-format-lint-action@v1.0.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/cmake-format-lint-action@v1.0.4
uses: puneetmatharu/cmake-format-lint-action@v1.0.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: Format CMake files
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 }}'
54 changes: 54 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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()

print(args.file_regex)
print(repr(args.file_regex))
print(args.cmake_format_args)
print(repr(args.cmake_format_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 824df29

Please sign in to comment.