From 97851f03401537b7fa0affc50a5800eda351c4b8 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Tue, 27 Feb 2024 21:09:04 +0100 Subject: [PATCH] Move dotnet-format script to pre-commit Co-authored-by: Paul Joannon <437025+paulloz@users.noreply.github.com> --- .github/workflows/static_checks.yml | 8 ---- .pre-commit-config.yaml | 6 +++ misc/scripts/dotnet_format.py | 30 +++++++++++++++ misc/scripts/dotnet_format.sh | 37 ------------------- .../GodotSharp/GodotSharp/GodotSharp.csproj | 2 +- .../GodotSharpEditor/GodotSharpEditor.csproj | 2 +- 6 files changed, 38 insertions(+), 47 deletions(-) create mode 100644 misc/scripts/dotnet_format.py delete mode 100755 misc/scripts/dotnet_format.sh diff --git a/.github/workflows/static_checks.yml b/.github/workflows/static_checks.yml index a9808fee95ca..0d5c1906b50d 100644 --- a/.github/workflows/static_checks.yml +++ b/.github/workflows/static_checks.yml @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3493219ea7ee..1962231d8a99 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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#] diff --git a/misc/scripts/dotnet_format.py b/misc/scripts/dotnet_format.py new file mode 100644 index 000000000000..de414bbe3028 --- /dev/null +++ b/misc/scripts/dotnet_format.py @@ -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("") + +# 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) diff --git a/misc/scripts/dotnet_format.sh b/misc/scripts/dotnet_format.sh deleted file mode 100755 index e2b4ba5e437c..000000000000 --- a/misc/scripts/dotnet_format.sh +++ /dev/null @@ -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 "" > modules/mono/SdkPackageVersions.props -mkdir -p modules/mono/glue/GodotSharp/GodotSharp/Generated -echo "" > modules/mono/glue/GodotSharp/GodotSharp/Generated/GeneratedIncludes.props -mkdir -p modules/mono/glue/GodotSharp/GodotSharpEditor/Generated -echo "" > 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 ``. -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="" 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 '\e[0m\n" -exit 1 diff --git a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj index db16b1fe1d5b..d54942e65486 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj @@ -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. --> - + diff --git a/modules/mono/glue/GodotSharp/GodotSharpEditor/GodotSharpEditor.csproj b/modules/mono/glue/GodotSharp/GodotSharpEditor/GodotSharpEditor.csproj index 31e20e4ecd01..c32cbcd3d16e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharpEditor/GodotSharpEditor.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharpEditor/GodotSharpEditor.csproj @@ -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. --> - +