actions/.github/workflows/format.yml

128 lines
4.5 KiB
YAML

name: format
on:
workflow_call:
inputs:
language:
description: the language to target for code formatting
required: true
type: string
directory:
description: "the directory to format (default: .)"
required: false
type: string
command:
description: custom command to run for formatting
required: false
type: string
jobs:
format:
timeout-minutes: 15
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: fetch latest commit
run: git pull origin "$(git rev-parse --abbrev-ref HEAD)"
- name: set up csharp
if: inputs.language == 'csharp'
uses: c4patino/actions/.github/actions/setup-csharp@main
- name: set up c
if: inputs.language == 'c'
uses: c4patino/actions/.github/actions/setup-c@main
- name: set up cpp
if: inputs.language == 'cpp'
uses: c4patino/actions/.github/actions/setup-cpp@main
- name: set up go
if: inputs.language == 'go'
uses: c4patino/actions/.github/actions/setup-go@main
- name: set up javascript
if: inputs.language == 'js'
uses: c4patino/actions/.github/actions/setup-js@main
- name: set up lua
if: inputs.language == 'lua'
uses: c4patino/actions/.github/actions/setup-lua@main
- name: set up nix
if: inputs.language == 'nix'
uses: c4patino/actions/.github/actions/setup-nix@main
- name: set up racket
if: inputs.language == 'racket'
uses: c4patino/actions/.github/actions/setup-racket@main
- name: set up python
if: inputs.language == 'python'
uses: c4patino/actions/.github/actions/setup-python@main
- name: set up rust
if: inputs.language == 'rust'
uses: c4patino/actions/.github/actions/setup-rust@main
- name: run formatting command
if: inputs.command != ''
run: ${{ inputs.command }}
- name: format all files
if: inputs.command == ''
run: |
TARGET_DIR="${{ inputs.directory || '.' }}"
case "${{ inputs.language }}" in
"csharp")
(cd "$TARGET_DIR" && dotnet format)
;;
"go")
(cd "$TARGET_DIR" && go fmt ./...)
;;
"js")
prettier --write "$TARGET_DIR" --cache
;;
"lua")
stylua "$TARGET_DIR"
;;
"nix")
alejandra "$TARGET_DIR"
;;
"python")
if find "$TARGET_DIR" -type f \( -name "*.py" \) | grep -q .; then
yapf --in-place --parallel --recursive --exclude ".venv" "$TARGET_DIR"
else
echo "No Python files found in $TARGET_DIR. Skipping yapf."
fi
;;
"racket")
if find "$TARGET_DIR" -type f -name "*.rkt" | grep -q .; then
find "$TARGET_DIR" -type f -name "*.rkt" -exec raco fmt -i {} \;
else
echo "No Racket files found in $TARGET_DIR. Skipping raco fmt."
fi
;;
"rust")
(cd "$TARGET_DIR" && cargo fmt --all)
;;
"cpp")
if find "$TARGET_DIR" -type f \( -name "*.cpp" -o -name "*.hpp" \) -not -path "*/extern/*" | grep -q .; then
find "$TARGET_DIR" -type f \( -name "*.cpp" -o -name "*.hpp" \) -not -path "*/extern/*" -exec clang-format -i --style=file {} \;
else
echo "No C++ files found in $TARGET_DIR. Skipping clang-format."
fi
;;
"c")
if find "$TARGET_DIR" -type f \( -name "*.c" -o -name "*.h" \) -not -path "*/extern/*" | grep -q .; then
find "$TARGET_DIR" -type f \( -name "*.c" -o -name "*.h" \) -not -path "*/extern/*" -exec clang-format -i --style=file {} \;
else
echo "No C files found in $TARGET_DIR. Skipping clang-format."
fi
;;
*)
echo "unsupported language: ${{ inputs.language }}"
exit 1
;;
esac
- name: commit changes
uses: stefanzweifel/git-auto-commit-action@v6.0.1
with:
commit_message: |
style: autoformatting all ${{ inputs.language }} files
[skip ci]