dotnet_format.py 1.0 KB

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