dotnet_format.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # This script runs dotnet format on all relevant files in the repo.
  3. # This is the primary script responsible for fixing style violations in C# files.
  4. set -uo pipefail
  5. # Create dummy generated files.
  6. echo "<Project />" > modules/mono/SdkPackageVersions.props
  7. mkdir -p modules/mono/glue/GodotSharp/GodotSharp/Generated
  8. echo "<Project />" > modules/mono/glue/GodotSharp/GodotSharp/Generated/GeneratedIncludes.props
  9. mkdir -p modules/mono/glue/GodotSharp/GodotSharpEditor/Generated
  10. echo "<Project />" > modules/mono/glue/GodotSharp/GodotSharpEditor/Generated/GeneratedIncludes.props
  11. # Loops through all C# projects tracked by Git.
  12. git ls-files -- '*.csproj' \
  13. ':!:.git/*' ':!:thirdparty/*' ':!:platform/android/java/lib/src/com/google/*' ':!:*-so_wrap.*' |
  14. while read -r f; do
  15. # Run dotnet format.
  16. dotnet format "$f"
  17. done
  18. diff=$(git diff --color)
  19. # If no diff has been generated all is OK, clean up, and exit.
  20. if [ -z "$diff" ] ; then
  21. printf "\e[1;32m*** Files in this commit comply with the dotnet format style rules.\e[0m\n"
  22. exit 0
  23. fi
  24. # A diff has been created, notify the user, clean up, and exit.
  25. printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
  26. # Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
  27. 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'
  28. printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
  29. exit 1