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 eeb4461
Show file tree
Hide file tree
Showing 5 changed files with 133 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/)
4 changes: 4 additions & 0 deletions pre-commit-dotnet-format-multiplayer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

export DEFINES="UNITY_EDITOR UNITY_EDITOR_WIN UNITY_ANDROID TILT_BRUSH PHOTON_UNITY_NETWORKING,PUN_2_0_OR_NEWER,PUN_2_OR_NEWER,PUN_2_19_OR_NEWER,FUSION_WEAVER,FUSION2,CROSS_PLATFORM_INPUT,MOBILE_INPUT,PHOTON_VOICE_DEFINED"
./pre_commit_dotnet_format.sh "$@"
4 changes: 4 additions & 0 deletions pre-commit-dotnet-format-oculus.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

export DEFINES="UNITY_EDITOR UNITY_ANDROID TILT_BRUSH OCULUS_SUPPORTED FORCE_HEADTRACKING FORCE_FOCUSAWARE FORCE_QUEST_SUPPORT_DEVICE USE_TILT_BRUSH_CPP 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 PASSTHROUGH_SUPPORTED"
./pre_commit_dotnet_format.sh "$@"
4 changes: 4 additions & 0 deletions pre-commit-dotnet-format-windows.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

export DEFINES="UNITY_EDITOR UNITY_EDITOR_WIN UNITY_WIN TILT_BRUSH SELECTION_ON AUDIO_REACTIVE USD_SUPPORTED FBX_SUPPORTED UNITY_STANDALONE_WIN USE_TILT_BRUSH_CPP 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 PASSTHROUGH_SUPPORTED"
./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 eeb4461

Please sign in to comment.