Skip to content

Commit

Permalink
Add a pre-commit config to run with the specific defines of major pro…
Browse files Browse the repository at this point in the history
…jects

Run with pre-commit run -a -c .pre-commit-config-defines.yaml

This is not enabled by default, because the code, which is rather
convoluted due to the inability to pass --define to dotnet format,
coupled with our files' containing BOMs, doesn't run on Windows. It
barely runs on Linux, tbh ;-)
  • Loading branch information
mikeage committed Dec 6, 2024
1 parent 26fea93 commit b31c5fb
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .pre-commit-config-defines.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
repos:
- repo: local
hooks:
- id: dotnet-format-windows
name: dotnet-format for Windows
language: system
entry: ./pre-commit-dotnet-format-windows.sh
types_or: [c#, vb]
exclude: ^(Assets/ThirdParty)|(Packages/)|(Assets/Photon/)
- repo: local
hooks:
- id: dotnet-format-multiplayer
name: dotnet-format for multiplayer
language: system
entry: ./pre-commit-dotnet-format-multiplayer.sh
types_or: [c#, vb]
exclude: ^(Assets/ThirdParty)|(Packages/)|(Assets/Photon/)
- repo: local
hooks:
- id: dotnet-format-oculus
name: dotnet-format for oculus
language: system
entry: ./pre-commit-dotnet-format-oculus.sh
types_or: [c#, vb]
exclude: ^(Assets/ThirdParty)|(Packages/)|(Assets/Photon/)
26 changes: 26 additions & 0 deletions pre-commit-dotnet-format-multiplayer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

export DEFINES="
CROSS_PLATFORM_INPUT
FUSION2
FUSION_WEAVER
MOBILE_INPUT
PHOTON_UNITY_NETWORKING
PHOTON_VOICE_DEFINED
PUN_2_0_OR_NEWER
PUN_2_19_OR_NEWER
PUN_2_OR_NEWER
TILT_BRUSH
UNITY_2017_2_OR_NEWER
UNITY_2018_4_OR_NEWER
UNITY_2019_3_OR_NEWER
UNITY_2020_3_OR_NEWER
UNITY_5_4_OR_NEWER
UNITY_5_6_OR_NEWER
UNITY_EDITOR
UNITY_EDITOR_WIN
UNITY_STANDALONE_WIN
UNITY_WIN
USE_TILT_BRUSH_CPP
"
./pre_commit_dotnet_format.sh "$@"
20 changes: 20 additions & 0 deletions pre-commit-dotnet-format-oculus.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

export DEFINES="
FORCE_FOCUSAWARE
FORCE_HEADTRACKING
FORCE_QUEST_SUPPORT_DEVICE
OCULUS_SUPPORTED
PASSTHROUGH_SUPPORTED
TILT_BRUSH
UNITY_2017_2_OR_NEWER
UNITY_2018_4_OR_NEWER
UNITY_2019_3_OR_NEWER
UNITY_2020_3_OR_NEWER
UNITY_5_4_OR_NEWER
UNITY_5_6_OR_NEWER
UNITY_ANDROID
UNITY_EDITOR
USE_TILT_BRUSH_CPP
"
./pre_commit_dotnet_format.sh "$@"
22 changes: 22 additions & 0 deletions pre-commit-dotnet-format-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

export DEFINES="
AUDIO_REACTIVE
FBX_SUPPORTED
PASSTHROUGH_SUPPORTED
SELECTION_ON
TILT_BRUSH
UNITY_2017_2_OR_NEWER
UNITY_2018_4_OR_NEWER
UNITY_2019_3_OR_NEWER
UNITY_2020_3_OR_NEWER
UNITY_5_4_OR_NEWER
UNITY_5_6_OR_NEWER
UNITY_EDITOR
UNITY_EDITOR_WIN
UNITY_STANDALONE_WIN
UNITY_WIN
USD_SUPPORTED
USE_TILT_BRUSH_CPP
"
./pre_commit_dotnet_format.sh "$@"
95 changes: 95 additions & 0 deletions pre_commit_dotnet_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash
set -e

# Check if the DEFINES environment variable is set
if [[ -z "$DEFINES" ]]; then
echo "Error: The DEFINES environment variable is not set."
exit 1
fi

# Convert the DEFINES environment variable into an array
IFS=', ' read -r -a defines <<< "$DEFINES"

# Temporary marker to identify added lines
marker="### TEMP DEFINES ###"

# Read the list of staged files from pre-commit
staged_files=("$@")

# Check if we are running on macOS or Linux
if [[ "$(uname)" == "Darwin" ]]; then
SED_CMD="gsed" # Use gsed on macOS
else
SED_CMD="sed" # Use sed on Linux
fi

# Process only staged files
echo "Processing staged files..."
for file in "${staged_files[@]}"; do
# Skip non-C# files
if [[ "$file" != *.cs ]]; then
continue
fi

# Check if the file has a BOM (EF BB BF at the start of the file) using xxd
bom_present=false
bom_hex=$(xxd -p -l 3 "$file")
if [[ "$bom_hex" == "efbbbf" ]]; then
bom_present=true
fi

# If BOM is present, remove it temporarily
if $bom_present; then
# Remove BOM bytes (first 3 bytes) using tail and save content to temporary file
tail -c +4 "$file" > "$file.tmp" && mv "$file.tmp" "$file"
fi

# Add defines only if marker is not already present
if ! grep -q "$marker" "$file"; then
# Add each define line separately using sed to insert at the top
for define in "${defines[@]}"; do
$SED_CMD -i "1i\\#define $define" "$file"
done
# Insert marker to identify where defines were added
$SED_CMD -i "1i\\$marker" "$file"
fi

# If BOM was present, reinsert it at the beginning of the file
if $bom_present; then
# Reinsert BOM at the start of the file
{ echo -n -e '\xEF\xBB\xBF'; cat "$file"; } > "$file.tmp" && mv "$file.tmp" "$file"
fi
done

# Run dotnet format
echo "Running dotnet format..."
dotnet format whitespace --folder --include "$@"

# Remove only the added defines
echo "Removing temporary defines..."
for file in "${staged_files[@]}"; do
# Skip non-C# files
if [[ "$file" != *.cs ]]; then
continue
fi

# Check if the file has a BOM (EF BB BF at the start of the file) using xxd
bom_present=false
bom_hex=$(xxd -p -l 3 "$file")
if [[ "$bom_hex" == "efbbbf" ]]; then
bom_present=true
fi

# Use a temporary file to preserve BOM while modifying
tmp_file="$file.tmp"
# Remove the marker and added defines, ensuring the BOM stays intact
$SED_CMD "/$marker/,+${#defines[@]}d" "$file" > "$tmp_file" && mv "$tmp_file" "$file"

# If BOM was present, reinsert it at the beginning of the file
if $bom_present; then
# Reinsert BOM at the start of the file
{ echo -n -e '\xEF\xBB\xBF'; cat "$file"; } > "$file.tmp" && mv "$file.tmp" "$file"
fi
done

echo "Pre-commit hook completed!"

0 comments on commit b31c5fb

Please sign in to comment.