Merge pull request #88933 from raulsntos/dotnet/pre-commit

Move dotnet-format script to pre-commit
This commit is contained in:
Rémi Verschelde 2024-03-02 10:48:25 +01:00 committed by GitHub
commit f2045ba822
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 38 additions and 47 deletions

View File

@ -88,14 +88,6 @@ jobs:
run: |
doc/tools/doc_status.py doc/classes modules/*/doc_classes platform/*/doc_classes
- name: Style checks via dotnet format (dotnet_format.sh)
run: |
if grep -q "modules/mono" changed.txt || [ -z "$(cat changed.txt)" ]; then
bash ./misc/scripts/dotnet_format.sh
else
echo "Skipping dotnet format as no C# files were changed."
fi
- name: Spell checks via codespell
if: github.event_name == 'pull_request' && env.CHANGED_FILES != ''
uses: codespell-project/actions-codespell@v2

View File

@ -47,3 +47,9 @@ repos:
platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper.*|
platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.*
)
- id: dotnet-format
name: dotnet-format
language: python
entry: python3 misc/scripts/dotnet_format.py
types_or: [c#]

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import os
import sys
# Create dummy generated files.
for path in [
"modules/mono/SdkPackageVersions.props",
]:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write("<Project />")
# Avoid importing GeneratedIncludes.props.
os.environ["GodotSkipGenerated"] = "true"
# Match all the input files to their respective C# project.
input_files = [os.path.normpath(x) for x in sys.argv]
projects = {
path: [f for f in sys.argv if os.path.commonpath([f, path]) == path]
for path in [os.path.dirname(f) for f in glob.glob("**/*.csproj", recursive=True)]
}
# Run dotnet format on all projects with more than 0 modified files.
for path, files in projects.items():
if len(files) > 0:
command = f"dotnet format {path} --include {' '.join(files)}"
os.system(command)

View File

@ -1,37 +0,0 @@
#!/usr/bin/env bash
# This script runs dotnet format on all relevant files in the repo.
# This is the primary script responsible for fixing style violations in C# files.
set -uo pipefail
# Create dummy generated files.
echo "<Project />" > modules/mono/SdkPackageVersions.props
mkdir -p modules/mono/glue/GodotSharp/GodotSharp/Generated
echo "<Project />" > modules/mono/glue/GodotSharp/GodotSharp/Generated/GeneratedIncludes.props
mkdir -p modules/mono/glue/GodotSharp/GodotSharpEditor/Generated
echo "<Project />" > modules/mono/glue/GodotSharp/GodotSharpEditor/Generated/GeneratedIncludes.props
# Loops through all C# projects tracked by Git.
git ls-files -- '*.csproj' \
':!:.git/*' ':!:thirdparty/*' ':!:platform/android/java/lib/src/com/google/*' ':!:*-so_wrap.*' |
while read -r f; do
# Run dotnet format.
dotnet format "$f"
done
diff=$(git diff --color)
# If no diff has been generated all is OK, clean up, and exit.
if [ -z "$diff" ] ; then
printf "\e[1;32m*** Files in this commit comply with the dotnet format style rules.\e[0m\n"
exit 0
fi
# A diff has been created, notify the user, clean up, and exit.
printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
# Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
printf "%s\n" "$diff" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
exit 1

View File

@ -142,5 +142,5 @@
We can't use wildcards as there may be undesired old files still hanging around.
Fortunately code completion, go to definition and such still work.
-->
<Import Project="Generated\GeneratedIncludes.props" />
<Import Condition=" '$(GodotSkipGenerated)' == '' " Project="Generated\GeneratedIncludes.props" />
</Project>

View File

@ -44,5 +44,5 @@
We can't use wildcards as there may be undesired old files still hanging around.
Fortunately code completion, go to definition and such still work.
-->
<Import Project="Generated\GeneratedIncludes.props" />
<Import Condition=" '$(GodotSkipGenerated)' == '' " Project="Generated\GeneratedIncludes.props" />
</Project>