dotnet_format.py 1020 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python3
  2. import glob
  3. import os
  4. import sys
  5. if len(sys.argv) < 2:
  6. print("Invalid usage of dotnet_format.py, it should be called with a path to one or multiple files.")
  7. sys.exit(1)
  8. # Create dummy generated files, if needed.
  9. for path in [
  10. "modules/mono/SdkPackageVersions.props",
  11. ]:
  12. if os.path.exists(path):
  13. continue
  14. os.makedirs(os.path.dirname(path), exist_ok=True)
  15. with open(path, "w", encoding="utf-8", newline="\n") as f:
  16. f.write("<Project />")
  17. # Avoid importing GeneratedIncludes.props.
  18. os.environ["GodotSkipGenerated"] = "true"
  19. # Match all the input files to their respective C# project.
  20. projects = {
  21. path: " ".join([f for f in sys.argv[1:] if os.path.commonpath([f, path]) == path])
  22. for path in [os.path.dirname(f) for f in glob.glob("**/*.csproj", recursive=True)]
  23. }
  24. # Run dotnet format on all projects with more than 0 modified files.
  25. for path, files in projects.items():
  26. if files:
  27. os.system(f"dotnet format {path} --include {files}")