Skip to content

Commit

Permalink
Update formatting script to use pre-commit and GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Dec 5, 2024
1 parent df079ec commit e4f0721
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 60 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions .github/workflows/file_format.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions .github/workflows/static_checks.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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]
13 changes: 0 additions & 13 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
47 changes: 0 additions & 47 deletions format.sh

This file was deleted.

0 comments on commit e4f0721

Please sign in to comment.