github: Add code formatting workflow and tools.

This commit is contained in:
iabdalkader 2023-07-01 12:38:34 +02:00
parent 187840a9f0
commit 12e0805a56
5 changed files with 833 additions and 453 deletions

58
.github/workflows/codeformat.yml vendored Normal file
View File

@ -0,0 +1,58 @@
name: '🔎 Check Code Formatting'
on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
branches:
- 'master'
jobs:
formatting-check:
runs-on: ubuntu-20.04
steps:
- name: '⏳ Checkout repository'
uses: actions/checkout@v3
with:
submodules: false
persist-credentials: false
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: '♻ Caching dependencies'
uses: actions/cache@v3.3.1
id: cache
with:
path: ~/cache/deps/bin
key: 'uncrustify'
- name: '🛠 Install dependencies'
if: steps.cache.outputs.cache-hit != 'true'
run: source tools/ci.sh && ci_install_code_format_deps
- name: '📜 Get list of changed files'
id: changed-files
uses: tj-actions/changed-files@v37
with:
files: |
src/**/*.c
src/**/*.h
!src/hal/**
!src/uvc/**
!src/lib/**
!src/drivers/**
!src/micropython/**
!src/stm32cubeai/**
- name: '📜 Show list of changed files'
run: |
echo "${{ toJSON(steps.changed-files.outputs) }}"
shell:
bash
- name: '🔎 Check code formatting'
if: steps.changed-files.outputs.any_changed == 'true'
run: |
source tools/ci.sh && ci_run_code_format_check ${{ steps.changed-files.outputs.all_changed_files }}

View File

@ -51,3 +51,43 @@ ci_package_firmware_release() {
ci_package_firmware_development() {
(cd firmware && for i in *; do zip -r -j "firmware_${i%/}.zip" "$i"; done)
}
########################################################################################
# Install code formatter deps
CODEFORMAT_PATH=${HOME}/cache/deps/
UNCRUSTIFY_PATH=${CODEFORMAT_PATH}/uncrustify
UNCRUSTIFY_URL="https://github.com/uncrustify/uncrustify/archive/uncrustify-0.75.0.tar.gz"
ci_install_code_format_deps() {
sudo apt-get install wget cmake build-essential colordiff
mkdir -p ${UNCRUSTIFY_PATH}
wget --no-check-certificate -O - ${UNCRUSTIFY_URL} | tar xvz --strip-components=1 -C ${UNCRUSTIFY_PATH}
(cd ${UNCRUSTIFY_PATH} && mkdir build && cd build && cmake .. && cmake --build .)
# Copy binaries to cache
mkdir -p ${CODEFORMAT_PATH}/bin
cp ${UNCRUSTIFY_PATH}/build/uncrustify ${CODEFORMAT_PATH}/bin/
cp `which colordiff` ${CODEFORMAT_PATH}/bin/
chmod +x ${CODEFORMAT_PATH}/bin/uncrustify
}
########################################################################################
# Run code formatter
ci_run_code_format_check() {
export PATH=${CODEFORMAT_PATH}/bin:${PATH}
UNCRUSTIFY_CONFIG=tools/uncrustify.cfg
exit_code=0
for file in "$@"; do
file_fmt="${file}.tmp"
uncrustify -q -c ${UNCRUSTIFY_CONFIG} -f ${file} -o ${file_fmt} || true
diff -q -u ${file} ${file_fmt} >> /dev/null 2>&1 || {
colordiff -u ${file} ${file_fmt} || true
exit_code=1
}
done
exit $exit_code
}

View File

@ -1,9 +1,12 @@
#!/bin/sh
CFG_PATH=`dirname $0`/uncrustify.cfg
BASE_DIR=`dirname $0`
if [ $# -ne 1 ]; then
if [ $# -lt 1 ]; then
echo "usage `basename $0` file.(h/c)"
exit 1
fi
uncrustify -c ${CFG_PATH} --no-backup -lC $1
for file in "$@"; do
${BASE_DIR}/uncrustify -c ${CFG_PATH} --no-backup $file
done

BIN
tools/uncrustify Executable file

Binary file not shown.

File diff suppressed because it is too large Load Diff