summaryrefslogtreecommitdiffstats
path: root/misc/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'misc/scripts')
-rw-r--r--misc/scripts/dotnet_format.py30
-rwxr-xr-xmisc/scripts/dotnet_format.sh37
2 files changed, 30 insertions, 37 deletions
diff --git a/misc/scripts/dotnet_format.py b/misc/scripts/dotnet_format.py
new file mode 100644
index 0000000000..de414bbe30
--- /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("<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)
diff --git a/misc/scripts/dotnet_format.sh b/misc/scripts/dotnet_format.sh
deleted file mode 100755
index e2b4ba5e43..0000000000
--- 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 "<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