|
| 1 | +name: Code Formatting Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - ".github/workflows/code-formatting-check.yml" |
| 7 | + - "examples/**" |
| 8 | + push: |
| 9 | + paths: |
| 10 | + - ".github/workflows/code-formatting-check.yml" |
| 11 | + - "examples/**" |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-formatting: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + env: |
| 18 | + # See: https://sourceforge.net/projects/astyle/files/astyle/ |
| 19 | + ASTYLE_VERSION: 3.1 |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v2 |
| 24 | + |
| 25 | + # See: http://astyle.sourceforge.net/ |
| 26 | + - name: Download Artistic Style |
| 27 | + uses: carlosperate/[email protected] |
| 28 | + with: |
| 29 | + file-url: https://iweb.dl.sourceforge.net/project/astyle/astyle/astyle%20${{ env.ASTYLE_VERSION }}/astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz |
| 30 | + location: ${{ runner.temp }}/astyle |
| 31 | + |
| 32 | + # See: http://astyle.sourceforge.net/install.html#_GCC_Makefile |
| 33 | + - name: Build Artistic Style |
| 34 | + run: | |
| 35 | + # Build Artistic Style |
| 36 | + cd "${{ runner.temp }}/astyle" |
| 37 | + tar --extract --file="astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz" |
| 38 | + cd "astyle/build/gcc" |
| 39 | + make |
| 40 | +
|
| 41 | + # GITHUB_WORKSPACE |
| 42 | + - name: Check code formatting |
| 43 | + run: | |
| 44 | + # Check code formatting of example sketches |
| 45 | + # Don't exit on first formatting check fail |
| 46 | + set +e |
| 47 | + # Set default exit status |
| 48 | + EXIT_STATUS=0 |
| 49 | + while read -r filePath; do |
| 50 | + # Check if it's a file (find matches on pruned folders) |
| 51 | + if [[ -f "$filePath" ]]; then |
| 52 | + if ! diff --strip-trailing-cr "$filePath" <("${{ runner.temp }}/astyle/astyle/build/gcc/bin/astyle" --options="${GITHUB_WORKSPACE}/examples_formatter.conf" --dry-run <"$filePath"); then |
| 53 | + echo "ERROR: Non-compliant code formatting in $filePath" |
| 54 | + EXIT_STATUS=1 |
| 55 | + fi |
| 56 | + fi |
| 57 | + done <<<"$(find "${GITHUB_WORKSPACE}/examples" -regextype posix-extended \( -regex '.*\.((ino)|(h)|(cpp)|(c))$' -and -type f \))" |
| 58 | + if [[ "$EXIT_STATUS" != "0" ]]; then |
| 59 | + echo "Please do an Auto Format on the sketches:" |
| 60 | + echo "Arduino IDE: Tools > Auto Format" |
| 61 | + echo "Arduino Web Editor: Ctrl + B" |
| 62 | + fi |
| 63 | + exit "$EXIT_STATUS" |
0 commit comments