Skip to content

Commit 6b63700

Browse files
committed
Use formatter script in CI workflow
The repository includes a script that formats the code in the example sketches. Previously, this script was only provided to make it easy for contributors to achieve compliant code formatting while preparing a pull request. In addition to this effort to facilitate compliance, enforcement is implemented by a GitHub Actions workflow. Since the formatter tool does not have the directory tree recursion capability needed to process all the files in the repository, it is necessary to implement such capability using a `find` command. Previously this was done both in the script and the workflow. `find` is ridiculously complex and unintuitive, prone to confusing bugs. For this reason, it will be best to avoid maintaining redundant code for recursing through the example code files. This is accomplished by simply running the script from the workflow. Even though the two have different goals (making formatting compliant vs checking whether formatting is compliant), the two are easily aligned by formatting the files in the workflow, then checking for a diff. This approach has been widely used in other Arduino Tooling Team formatting check workflows and I find the diff is very effective at communicating which changes are required, likely more so than the approach previously used in the workflow.
1 parent 124bead commit 6b63700

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Diff for: .github/workflows/code-formatting-check.yml

+11-18
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ on:
44
pull_request:
55
paths:
66
- ".github/workflows/code-formatting-check.yml"
7+
- "examples_formatter.sh"
78
- "examples/**"
89
push:
910
paths:
1011
- ".github/workflows/code-formatting-check.yml"
12+
- "examples_formatter.sh"
1113
- "examples/**"
1214

1315
jobs:
@@ -37,27 +39,18 @@ jobs:
3739
tar --extract --file="astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz"
3840
cd "astyle/build/gcc"
3941
make
42+
# Add installation to PATH:
43+
# See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path
44+
echo "${{ runner.temp }}/astyle/astyle/build/gcc/bin" >> "$GITHUB_PATH"
4045
41-
# GITHUB_WORKSPACE
42-
- name: Check code formatting
46+
- name: Format examples
47+
run: ./examples_formatter.sh
48+
49+
- name: Check formatting
4350
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
51+
if ! git diff --color --exit-code; then
5952
echo "Please do an Auto Format on the sketches:"
6053
echo "Arduino IDE: Tools > Auto Format"
6154
echo "Arduino Web Editor: Ctrl + B"
55+
exit 1
6256
fi
63-
exit "$EXIT_STATUS"

0 commit comments

Comments
 (0)