Skip to content

Commit

Permalink
Fully intraged Github Action with the python script to check cmake fo…
Browse files Browse the repository at this point in the history
…rmatting (#2)

* list files on entry point

* Test with script run

* Remove default value for inplace input

* Set inplace default value to False

* Remove all inputs except for source dir

* install pip package

* Delegate cmakelang package to the Dockerfile

* Fix source input to support multiple values

* Fix config inplace flag

* Fix exclude/source parsing

* Parse exclude dirs the same way as source dirs

* Update Action README
  • Loading branch information
Genci Berisha authored Oct 14, 2023
1 parent 5687f9f commit e07a16b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 59 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ COPY cmake-format/ /cmake-format/

RUN chmod +x entrypoint.sh

RUN python -m pip install --no-cache-dir --upgrade pip && \
python -m pip install --no-cache-dir "Cython<3" "cmakelang[YAML]==0.6.13"

ENTRYPOINT ["/entrypoint.sh"]
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
# cmake-format with diff output
# cmake-format lint GitHub Action

This action checks if the cmake files matches the .cmake-format file.
GitHub Action to check if your code is formatted correctly using cmake-format.

```
$ python3 cmake_format_runner.py ./
```
This action also provides a Git diff display, making it easy to review code formatting changes.

TBA

## Usage

### Inputs

- `source` (optional): Source file/folder to check formatting or directory path. Defaults to "." (current directory). You can specify multiple values separated by spaces.
- `exclude` (optional): Folders to exclude from the formatting check. Defaults to "none." You can specify multiple values separated by spaces.
- `config` (optional): Use .cmake-format style. Defaults to False.
- `inplace` (optional): Just fix files (`cmake-format -i`) instead of returning a diff. Defaults to False.

### Example Workflow

Here's an example of how to use this action in your GitHub Actions workflow:

```yaml
name: Check Code Formatting

on:
push:
branches:
- main

jobs:
format-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: cmake-format lint
uses: neg-c/[email protected]
with:
source: "src/module1 src/module2"
exclude: "thirdparty external"
config: true
inplace: true

```
16 changes: 7 additions & 9 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,29 @@ branding:
color: "orange"
inputs:
source:
description: "Source folder to check formatting or directoryPath"
description: "Source file/folder to check formatting or directoryPath"
required: false
default: "."
exclude:
description: "Folder to exclude from formatting check"
description: "Folders to exclude from formatting check"
required: false
default: "none"
config:
description: "Formatting style to use"
description: "Use .cmake-format style"
required: false
default: ".cmake-format"
default: False
inplace:
description: "Just fix files (`cmake-format -i`) instead of returning a diff"
required: false
default: "False"
default: False
runs:
using: "docker"
image: "Dockerfile"
args:
- --color
- always
- --config
- ${{ inputs.config }}
- --inplace
- ${{ inputs.inplace }}
- --config
- ${{ inputs.config }}
- --exclude
- ${{ inputs.exclude }}
- ${{ inputs.source }}
35 changes: 10 additions & 25 deletions cmake-format/arg_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from distutils.util import strtobool


def parse_cmake_format_args():
Expand All @@ -21,18 +22,9 @@ def parse_cmake_format_args():
parser.add_argument(
"-e",
"--exclude",
metavar="PATTERN",
type=lambda x: [pattern for line in x.split("\n") for pattern in line.split()],
default=[],
)

# parser.add_argument(
# "-e",
# "--exclude",
# nargs="+",
# default=[],
# help="exclude paths matching the given glob-like pattern(s)"
# " from recursive search",
# )
parser.add_argument(
"--tab-size",
type=int,
Expand Down Expand Up @@ -70,31 +62,24 @@ def parse_cmake_format_args():

parser.add_argument(
"--config",
type=lambda x: bool(strtobool(x)),
default=False,
help="Formatting style to use (default: file)",
)

parser.add_argument(
"-i",
"--inplace",
action="store_true",
# type=lambda x: bool(strtobool(x)),
type=lambda x: bool(strtobool(x)),
default=False,
help="Just fix files (`clang-format -i`) instead of returning a diff",
)

parser.add_argument("files", metavar="file")
parser.add_argument(
"files",
type=lambda x: [path for line in x.split("\n") for path in line.split()],
metavar="file",
)

args = parser.parse_args()
return args


def split_list_arg(arg):
"""
If arg is a list containing a single argument it is split into multiple elements.
Otherwise it is returned unchanged
Workaround for GHA not allowing list arguments
"""
if not arg:
return None
split_args = arg.split(",")
return split_args
2 changes: 1 addition & 1 deletion cmake-format/cmake_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def run_cmake_format_diff(args, file):

invocation = ["cmake-format", file]
if args.config:
invocation.append('--config='+args.config)
invocation.append("--config=.cmake-format")
if args.inplace:
invocation.append("-i")

Expand Down
32 changes: 18 additions & 14 deletions cmake-format/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@

def list_files(files, exclude=None):
cmake_files = []

if exclude is None:
exclude = []

exclude.append("build")
for root, dirs, files in os.walk(files):
dirs[:] = [
d
for d in dirs
if not any(fnmatch.fnmatch(d, pattern) for pattern in exclude)
]
files[:] = [
f
for f in files
if not any(fnmatch.fnmatch(f, pattern) for pattern in exclude)
]

for file in files:
if file.lower().endswith(".cmake") or file == "CMakeLists.txt":
cmake_files.append(os.path.join(root, file))
for file in files:
for root, dirs, files in os.walk(file):
# Filter out directories and files based on the exclusion patterns
dirs[:] = [
d
for d in dirs
if not any(fnmatch.fnmatch(d, pattern) for pattern in exclude)
]
files[:] = [
f
for f in files
if not any(fnmatch.fnmatch(f, pattern) for pattern in exclude)
]

for file in files:
if file.lower().endswith(".cmake") or file == "CMakeLists.txt":
cmake_files.append(os.path.join(root, file))

return cmake_files
5 changes: 2 additions & 3 deletions cmake-format/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
from arg_parser import parse_cmake_format_args, split_list_arg
from arg_parser import parse_cmake_format_args
from file_operations import list_files
from cmake_format import run_cmake_format_diff_wrapper, ExitStatus, print_diff
from output import print_trouble
Expand Down Expand Up @@ -31,9 +31,8 @@ def main():

retcode = ExitStatus.SUCCESS

excludes = split_list_arg(args.exclude)

files = list_files(args.files, exclude=excludes)
files = list_files(args.files, exclude=args.exclude)

if not files:
print_trouble("No files found", use_colors=colored_stderr)
Expand Down
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cd "$GITHUB_WORKSPACE"

python3 cmake-format/main.py
python3 /cmake-format/main.py "$@"

0 comments on commit e07a16b

Please sign in to comment.