From e4f072171f5db3c6bdabfeeabad0ccbf9bf7b59d Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Thu, 18 Feb 2021 21:43:56 -0500 Subject: [PATCH] Update formatting script to use pre-commit and GitHub Actions --- .gitattributes | 6 ++++ .github/workflows/file_format.py | 52 +++++++++++++++++++++++++++++ .github/workflows/static_checks.yml | 24 +++++++++++++ .pre-commit-config.yaml | 16 +++++++++ appveyor.yml | 13 -------- format.sh | 47 -------------------------- 6 files changed, 98 insertions(+), 60 deletions(-) create mode 100644 .gitattributes create mode 100755 .github/workflows/file_format.py create mode 100644 .github/workflows/static_checks.yml create mode 100644 .pre-commit-config.yaml delete mode 100755 format.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..20a1577d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf +# Except for Windows-only / Visual Studio files +*.bat eol=crlf +*.sln eol=crlf +*.csproj eol=crlf diff --git a/.github/workflows/file_format.py b/.github/workflows/file_format.py new file mode 100755 index 00000000..8f511529 --- /dev/null +++ b/.github/workflows/file_format.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys + +if len(sys.argv) < 2: + print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.") + sys.exit(1) + +BOM = b"\xef\xbb\xbf" + +changed = [] +invalid = [] + +for file in sys.argv[1:]: + try: + with open(file, "rt", encoding="utf-8") as f: + original = f.read() + except UnicodeDecodeError: + invalid.append(file) + continue + + if original == "": + continue + + EOL = "\r\n" if file.endswith((".csproj", ".sln", ".bat")) else "\n" + WANTS_BOM = file.endswith((".csproj", ".sln")) + + revamp = EOL.join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip(EOL) + EOL + + new_raw = revamp.encode(encoding="utf-8") + if not WANTS_BOM and new_raw.startswith(BOM): + new_raw = new_raw[len(BOM) :] + elif WANTS_BOM and not new_raw.startswith(BOM): + new_raw = BOM + new_raw + + with open(file, "rb") as f: + old_raw = f.read() + + if old_raw != new_raw: + changed.append(file) + with open(file, "wb") as f: + f.write(new_raw) + +if changed: + for file in changed: + print(f"FIXED: {file}") + +if invalid: + for file in invalid: + print(f"REQUIRES MANUAL CHANGES: {file}") + sys.exit(1) diff --git a/.github/workflows/static_checks.yml b/.github/workflows/static_checks.yml new file mode 100644 index 00000000..1ed054a8 --- /dev/null +++ b/.github/workflows/static_checks.yml @@ -0,0 +1,24 @@ +name: 📊 Static Checks +on: [push, pull_request] + +concurrency: + group: ci-${{ github.actor }}-${{ github.head_ref || github.run_number }}-${{ github.ref }}-static + +jobs: + static-checks: + name: Code style and file formatting + runs-on: ubuntu-24.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - name: Install Python dependencies and general setup + run: | + git config diff.wsErrorHighlight all + + - name: Style checks via pre-commit + uses: pre-commit/action@v3.0.1 + with: + extra_args: --all-files diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..f2796719 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,16 @@ +default_language_version: + python: python3 + +exclude: | + (?x)^( + CODE_OF_CONDUCT.md + ) + +repos: + - repo: local + hooks: + - id: file-format + name: file-format + language: python + entry: python .github/workflows/file_format.py + types_or: [text] diff --git a/appveyor.yml b/appveyor.yml index 2e08df14..7dd4dcb0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,13 +10,6 @@ configuration: Release environment: APPVEYOR_YML_DISABLE_PS_LINUX: true -install: -- sh: | - if [ "$(uname)" != "Darwin" ]; then - sudo apt-get update -y - sudo apt-get install -y dos2unix recode - fi - build_script: - ps: | New-Item -Path . -Name "build" -ItemType "directory" @@ -33,12 +26,6 @@ build_script: cmake --build . --config ${CONFIGURATION} cd ../ -test_script: - - sh: | - if [ "$(uname)" != "Darwin" ]; then - bash ./format.sh - fi - artifacts: # Linux - path: bin/basisu diff --git a/format.sh b/format.sh deleted file mode 100755 index 5760aec5..00000000 --- a/format.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash - -# Loops through all text files tracked by Git. -git grep -zIl '' | -while IFS= read -rd '' f; do - # Exclude some types of files. - if [[ $f == *"proj" ]]; then - continue - elif [[ $f == *"filters" ]]; then - continue - elif [[ $f == *"sln" ]]; then - continue - elif [[ $f == *"json" ]]; then - continue - elif [[ $f == *"min.js" ]]; then - continue - elif [[ $f == *"coder.js" ]]; then - continue - fi - # Ensures that files are UTF-8 formatted. - recode UTF-8 $f 2> /dev/null - # Ensures that files have LF line endings. - dos2unix $f 2> /dev/null - # Ensures that files do not contain a BOM. - sed -i '1s/^\xEF\xBB\xBF//' "$f" - # Ensures that files end with newline characters. - tail -c1 < "$f" | read -r _ || echo >> "$f"; -done - -git diff > patch.patch -FILESIZE=$(stat -c%s patch.patch) -MAXSIZE=5 - -# If no patch has been generated all is OK, clean up, and exit. -if (( FILESIZE < MAXSIZE )); then - printf "Files in this commit comply with the formatting rules.\n" - rm -f patch.patch - exit 0 -fi - -# A patch has been created, notify the user, clean up, and exit. -printf "\n*** The following differences were found between the code " -printf "and the formatting rules:\n\n" -cat patch.patch -printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i '\n" -rm -f patch.patch -exit 1