diff --git a/Dockerfile b/Dockerfile index 007e7e5..b20fad3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index 23be85f..ea3e4e8 100644 --- a/README.md +++ b/README.md @@ -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/cmake-format-action@v0.1 + with: + source: "src/module1 src/module2" + exclude: "thirdparty external" + config: true + inplace: true + +``` \ No newline at end of file diff --git a/action.yml b/action.yml index bfe5fb6..910e4a8 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/cmake-format/arg_parser.py b/cmake-format/arg_parser.py index fdcf7c2..633e3ca 100644 --- a/cmake-format/arg_parser.py +++ b/cmake-format/arg_parser.py @@ -1,4 +1,5 @@ import argparse +from distutils.util import strtobool def parse_cmake_format_args(): @@ -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, @@ -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 diff --git a/cmake-format/cmake_format.py b/cmake-format/cmake_format.py index 6b24ba0..9a27c04 100644 --- a/cmake-format/cmake_format.py +++ b/cmake-format/cmake_format.py @@ -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") diff --git a/cmake-format/file_operations.py b/cmake-format/file_operations.py index 5872c68..4e01581 100644 --- a/cmake-format/file_operations.py +++ b/cmake-format/file_operations.py @@ -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 diff --git a/cmake-format/main.py b/cmake-format/main.py index 233af59..e6ac882 100644 --- a/cmake-format/main.py +++ b/cmake-format/main.py @@ -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 @@ -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) diff --git a/entrypoint.sh b/entrypoint.sh index 43cd3ef..3bd2d6a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,4 +2,4 @@ cd "$GITHUB_WORKSPACE" -python3 cmake-format/main.py +python3 /cmake-format/main.py "$@"