Browse Source

CI: Overhaul static checks to use `pre-commit`

Thaddeus Crews 1 year ago
parent
commit
df969ff742

+ 5 - 59
.github/workflows/static_checks.yml

@@ -19,28 +19,13 @@ jobs:
       - name: Install APT dependencies
       - name: Install APT dependencies
         uses: awalsh128/cache-apt-pkgs-action@latest
         uses: awalsh128/cache-apt-pkgs-action@latest
         with:
         with:
-          packages: dos2unix libxml2-utils moreutils
+          packages: libxml2-utils
 
 
       - name: Install Python dependencies and general setup
       - name: Install Python dependencies and general setup
         run: |
         run: |
-          pip3 install pytest==7.1.2 mypy==0.971
+          pip3 install pytest==7.1.2
           git config diff.wsErrorHighlight all
           git config diff.wsErrorHighlight all
 
 
-      - name: Get changed files
-        id: changed-files
-        env:
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        run: |
-          if [ "${{ github.event_name }}" == "pull_request" ]; then
-            files=$(git diff-tree --no-commit-id --name-only -r HEAD^1..HEAD 2> /dev/null || true)
-          elif [ "${{ github.event_name }}" == "push" -a "${{ github.event.forced }}" == "false" -a "${{ github.event.created }}" == "false" ]; then
-            files=$(git diff-tree --no-commit-id --name-only -r ${{ github.event.before }}..${{ github.event.after }} 2> /dev/null || true)
-          fi
-          echo "$files" >> changed.txt
-          cat changed.txt
-          files=$(echo "$files" | grep -v 'thirdparty' | xargs -I {} sh -c 'echo "./{}"' | tr '\n' ' ')
-          echo "CHANGED_FILES=$files" >> $GITHUB_ENV
-
       # This needs to happen before Python and npm execution; it must happen before any extra files are written.
       # This needs to happen before Python and npm execution; it must happen before any extra files are written.
       - name: .gitignore checks (gitignore_check.sh)
       - name: .gitignore checks (gitignore_check.sh)
         run: |
         run: |
@@ -49,51 +34,12 @@ jobs:
       - name: Style checks via pre-commit
       - name: Style checks via pre-commit
         uses: pre-commit/[email protected]
         uses: pre-commit/[email protected]
         with:
         with:
-          extra_args: --verbose --files ${{ env.CHANGED_FILES }}
-
-      - name: File formatting checks (file_format.sh)
-        run: |
-          bash ./misc/scripts/file_format.sh changed.txt
-
-      - name: Header guards formatting checks (header_guards.sh)
-        run: |
-          bash ./misc/scripts/header_guards.sh changed.txt
-
-      - name: Python scripts static analysis (mypy_check.sh)
-        run: |
-          if grep -qE '\.py$|SConstruct|SCsub' changed.txt || [ -z "$(cat changed.txt)" ]; then
-            bash ./misc/scripts/mypy_check.sh
-          else
-            echo "Skipping Python static analysis as no Python files were changed."
-          fi
-
-      - name: Python builders checks via pytest (pytest_builders.sh)
-        run: |
-          bash ./misc/scripts/pytest_builders.sh
+          extra_args: --verbose
 
 
-      - name: JavaScript style and documentation checks via ESLint and JSDoc
+      - name: Python builders checks via pytest
         run: |
         run: |
-          if grep -q "\.js" changed.txt || [ -z "$(cat changed.txt)" ]; then
-            cd platform/web
-            npm ci
-            npm run lint
-            npm run docs -- -d dry-run
-          else
-            echo "Skipping JavaScript formatting as no Web/JS files were changed."
-          fi
+          pytest ./tests/python_build
 
 
       - name: Class reference schema checks
       - name: Class reference schema checks
         run: |
         run: |
           xmllint --noout --schema doc/class.xsd doc/classes/*.xml modules/*/doc_classes/*.xml platform/*/doc_classes/*.xml
           xmllint --noout --schema doc/class.xsd doc/classes/*.xml modules/*/doc_classes/*.xml platform/*/doc_classes/*.xml
-
-      - name: Documentation checks
-        run: |
-          doc/tools/doc_status.py doc/classes modules/*/doc_classes platform/*/doc_classes
-
-      - name: Spell checks via codespell
-        if: github.event_name == 'pull_request' && env.CHANGED_FILES != ''
-        uses: codespell-project/actions-codespell@v2
-        with:
-          skip: "./bin,./thirdparty,*.desktop,*.gen.*,*.po,*.pot,*.rc,./AUTHORS.md,./COPYRIGHT.txt,./DONORS.md,./core/input/gamecontrollerdb.txt,./core/string/locales.h,./editor/project_converter_3_to_4.cpp,./misc/scripts/codespell.sh,./platform/android/java/lib/src/com,./platform/web/node_modules,./platform/web/package-lock.json"
-          ignore_words_list: "breaked,colour,curvelinear,doubleclick,expct,findn,gird,hel,inout,lod,mis,nd,numer,ot,requestor,te,thirdparty,vai"
-          path: ${{ env.CHANGED_FILES }}

+ 139 - 13
.pre-commit-config.yaml

@@ -1,3 +1,9 @@
+exclude: |
+  (?x)^(
+    .*thirdparty/.*|
+    .*-so_wrap\.(h|c)$
+  )
+
 repos:
 repos:
   - repo: https://github.com/pre-commit/mirrors-clang-format
   - repo: https://github.com/pre-commit/mirrors-clang-format
     rev: v17.0.6
     rev: v17.0.6
@@ -7,10 +13,8 @@ repos:
         types_or: [text]
         types_or: [text]
         exclude: |
         exclude: |
           (?x)^(
           (?x)^(
-            tests/python_build.*|
-            .*thirdparty.*|
-            .*platform/android/java/lib/src/com.*|
-            .*-so_wrap.*
+            tests/python_build/.*|
+            platform/android/java/lib/src/com/.*
           )
           )
 
 
   - repo: https://github.com/psf/black-pre-commit-mirror
   - repo: https://github.com/psf/black-pre-commit-mirror
@@ -19,33 +23,155 @@ repos:
       - id: black
       - id: black
         files: (\.py$|SConstruct|SCsub)
         files: (\.py$|SConstruct|SCsub)
         types_or: [text]
         types_or: [text]
-        exclude: .*thirdparty.*
         args:
         args:
           - --line-length=120
           - --line-length=120
 
 
+  - repo: https://github.com/pre-commit/mirrors-mypy
+    rev: v0.971
+    hooks:
+      - id: mypy
+        files: \.py$
+        types_or: [text]
+        args: [--config-file=./misc/scripts/mypy.ini]
+
+  - repo: https://github.com/codespell-project/codespell
+    rev: v2.2.6
+    hooks:
+      - id: codespell
+        types_or: [text]
+        exclude: |
+          (?x)^(
+            .*\.desktop$|
+            .*\.gitignore$|
+            .*\.po$|
+            .*\.pot$|
+            .*\.rc$|
+            \.mailmap$|
+            AUTHORS.md$|
+            COPYRIGHT.txt$|
+            DONORS.md$|
+            core/input/gamecontrollerdb.txt$|
+            core/string/locales.h$|
+            editor/project_converter_3_to_4.cpp$|
+            platform/android/java/lib/src/com/.*|
+            platform/web/package-lock.json$
+          )
+        args:
+          - --enable-colors
+          - --write-changes
+          - --check-hidden
+          - --quiet-level
+          - '3'
+          - --ignore-words-list
+          - aesthetic,aesthetics,breaked,cancelled,colour,curvelinear,doubleclick,expct,findn,gird,hel,inout,lod,mis,nd,numer,ot,requestor,te,thirdparty,vai
+          - --builtin
+          - clear,rare,en-GB_to_en-US
+
+  ### Requires Docker; look into alternative implementation.
+  # - repo: https://github.com/comkieffer/pre-commit-xmllint.git
+  #   rev: 1.0.0
+  #   hooks:
+  #     - id: xmllint
+  #       language: docker
+  #       types_or: [text]
+  #       files: ^(doc/classes|.*/doc_classes)/.*\.xml$
+  #       args: [--schema, doc/class.xsd]
+
+  - repo: https://github.com/pre-commit/mirrors-eslint
+    rev: v8.46.0
+    hooks:
+      - id: eslint
+        name: eslint-engine
+        files: ^(platform/web/js/engine/|js/jsdoc2rst/).*\.js$
+        args: [--fix, --no-eslintrc, --config, platform/web/.eslintrc.engine.js]
+        additional_dependencies: &eslint-deps
+          - [email protected]
+          - [email protected]
+          - [email protected]
+          - [email protected]
+          - '@html-eslint/[email protected]'
+          - '@html-eslint/[email protected]'
+      - id: eslint
+        name: eslint-libs
+        files: ^(platform/web/js/libs/|modules/).*\.js$
+        args: [--fix, --no-eslintrc, --config, platform/web/.eslintrc.libs.js]
+        additional_dependencies: *eslint-deps
+      - id: eslint
+        name: eslint-sw
+        files: ^misc/dist/html/service-worker\.js$
+        args: [--fix, --no-eslintrc, --config, platform/web/.eslintrc.sw.js]
+        additional_dependencies: *eslint-deps
+      - id: eslint
+        name: eslint-html
+        files: ^misc/dist/html/.*\.html$
+        types: [html]
+        args: [--fix, --no-eslintrc, --config, platform/web/.eslintrc.html.js]
+        additional_dependencies: *eslint-deps
+
   - repo: local
   - repo: local
     hooks:
     hooks:
       - id: make-rst
       - id: make-rst
         name: make-rst
         name: make-rst
+        language: python
         entry: python3 doc/tools/make_rst.py doc/classes modules platform --dry-run --color
         entry: python3 doc/tools/make_rst.py doc/classes modules platform --dry-run --color
         pass_filenames: false
         pass_filenames: false
+        files: ^(doc/classes|.*/doc_classes)/.*\.xml$
+
+      - id: doc-status
+        name: doc-status
         language: python
         language: python
-        files: ^(doc|modules|platform).*xml$
+        entry: python3 doc/tools/doc_status.py
+        files: ^(doc/classes|.*/doc_classes)/.*\.xml$
+
+      - id: jsdoc
+        name: jsdoc
+        language: node
+        entry: jsdoc
+        files: ^platform/web/js/engine/(engine|config|features)\.js$
+        args: [--template, platform/web/js/jsdoc2rst/, --destination, '', -d, dry-run]
+        additional_dependencies: ["[email protected]"]
 
 
       - id: copyright-headers
       - id: copyright-headers
         name: copyright-headers
         name: copyright-headers
         language: python
         language: python
-        files: \.(c|h|cpp|hpp|cc|cxx|m|mm|inc|java)$
         entry: python3 misc/scripts/copyright_headers.py
         entry: python3 misc/scripts/copyright_headers.py
+        files: \.(c|h|cpp|hpp|cc|cxx|m|mm|inc|java)$
         exclude: |
         exclude: |
           (?x)^(
           (?x)^(
-            .*thirdparty.*|
-            .*-so_wrap.*|
             core/math/bvh_.*\.inc$|
             core/math/bvh_.*\.inc$|
-            platform/android/java/lib/src/com.*|
-            platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView.*|
-            platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper.*|
-            platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.*
+            platform/android/java/lib/src/com/.*|
+            platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView\.java$|
+            platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper\.java$|
+            platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix\.java$
+          )
+
+      - id: header-guards
+        name: header-guards
+        language: python
+        entry: python3 misc/scripts/header_guards.py
+        files: \.(h|hpp)$
+        exclude: |
+          (?x)^(
+            .*/thread\.h$|
+            .*/platform_config\.h$|
+            .*/platform_gl\.$h
+          )
+
+      - id: file-format
+        name: file-format
+        language: python
+        entry: python3 misc/scripts/file_format.py
+        types_or: [text]
+        exclude: |
+          (?x)^(
+            .*\.test\.txt$|
+            .*\.svg$|
+            .*\.patch$|
+            .*\.out$|
+            modules/gdscript/tests/scripts/parser/features/mixed_indentation_on_blank_lines\.gd$|
+            modules/gdscript/tests/scripts/parser/warnings/empty_file_newline_comment\.notest\.gd$|
+            modules/gdscript/tests/scripts/parser/warnings/empty_file_newline\.notest\.gd$|
+            platform/android/java/lib/src/com/google/.*
           )
           )
 
 
       - id: dotnet-format
       - id: dotnet-format

+ 0 - 8
misc/scripts/codespell.sh

@@ -1,8 +0,0 @@
-#!/bin/sh
-SKIP_LIST="./.*,./**/.*,./bin,./thirdparty,*.desktop,*.gen.*,*.po,*.pot,*.rc,./AUTHORS.md,./COPYRIGHT.txt,./DONORS.md,"
-SKIP_LIST+="./core/input/gamecontrollerdb.txt,./core/string/locales.h,./editor/renames_map_3_to_4.cpp,./misc/scripts/codespell.sh,"
-SKIP_LIST+="./platform/android/java/lib/src/com,./platform/web/node_modules,./platform/web/package-lock.json,"
-
-IGNORE_LIST="breaked,cancelled,colour,curvelinear,doubleclick,expct,findn,gird,hel,inout,lod,mis,nd,numer,ot,requestor,te,thirdparty,vai"
-
-codespell -w -q 3 -S "${SKIP_LIST}" -L "${IGNORE_LIST}" --builtin "clear,rare,en-GB_to_en-US"

+ 10 - 6
misc/scripts/dotnet_format.py

@@ -5,10 +5,16 @@ import glob
 import os
 import os
 import sys
 import sys
 
 
-# Create dummy generated files.
+if len(sys.argv) < 2:
+    print("Invalid usage of dotnet_format.py, it should be called with a path to one or multiple files.")
+    sys.exit(1)
+
+# Create dummy generated files, if needed.
 for path in [
 for path in [
     "modules/mono/SdkPackageVersions.props",
     "modules/mono/SdkPackageVersions.props",
 ]:
 ]:
+    if os.path.exists(path):
+        continue
     os.makedirs(os.path.dirname(path), exist_ok=True)
     os.makedirs(os.path.dirname(path), exist_ok=True)
     with open(path, "w", encoding="utf-8", newline="\n") as f:
     with open(path, "w", encoding="utf-8", newline="\n") as f:
         f.write("<Project />")
         f.write("<Project />")
@@ -17,14 +23,12 @@ for path in [
 os.environ["GodotSkipGenerated"] = "true"
 os.environ["GodotSkipGenerated"] = "true"
 
 
 # Match all the input files to their respective C# project.
 # Match all the input files to their respective C# project.
-input_files = [os.path.normpath(x) for x in sys.argv]
 projects = {
 projects = {
-    path: [f for f in sys.argv if os.path.commonpath([f, path]) == path]
+    path: " ".join([f for f in sys.argv[1:] if os.path.commonpath([f, path]) == path])
     for path in [os.path.dirname(f) for f in glob.glob("**/*.csproj", recursive=True)]
     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.
 # Run dotnet format on all projects with more than 0 modified files.
 for path, files in projects.items():
 for path, files in projects.items():
-    if len(files) > 0:
-        command = f"dotnet format {path} --include {' '.join(files)}"
-        os.system(command)
+    if files:
+        os.system(f"dotnet format {path} --include {files}")

+ 51 - 0
misc/scripts/file_format.py

@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys
+
+if len(sys.argv) < 2:
+    print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.")
+    sys.exit(1)
+
+BOM = b"\xef\xbb\xbf"
+
+changed = []
+invalid = []
+
+for file in sys.argv[1:]:
+    try:
+        with open(file, "rt", encoding="utf-8") as f:
+            original = f.read()
+    except UnicodeDecodeError:
+        invalid.append(file)
+        continue
+
+    if original == "":
+        continue
+
+    EOL = "\r\n" if file.endswith((".csproj", ".sln", ".bat")) or file.startswith("misc/msvs") else "\n"
+    WANTS_BOM = file.endswith((".csproj", ".sln"))
+
+    revamp = EOL.join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip(EOL) + EOL
+
+    new_raw = revamp.encode(encoding="utf-8")
+    if not WANTS_BOM and new_raw.startswith(BOM):
+        new_raw = new_raw[len(BOM) :]
+    elif WANTS_BOM and not new_raw.startswith(BOM):
+        new_raw = BOM + new_raw
+
+    with open(file, "rb") as f:
+        old_raw = f.read()
+
+    if old_raw != new_raw:
+        changed.append(file)
+        with open(file, "wb") as f:
+            f.write(new_raw)
+
+if changed:
+    for file in changed:
+        print(f"FIXED: {file}")
+if invalid:
+    for file in invalid:
+        print(f"REQUIRES MANUAL CHANGES: {file}")
+    sys.exit(1)

+ 0 - 89
misc/scripts/file_format.sh

@@ -1,89 +0,0 @@
-#!/usr/bin/env bash
-
-# This script ensures proper POSIX text file formatting and a few other things.
-# This is supplementary to clang_format.sh and black_format.sh, but should be
-# run before them.
-
-# We need dos2unix and isutf8.
-if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v isutf8)" ]; then
-    printf "Install 'dos2unix' and 'isutf8' (moreutils package) to use this script.\n"
-    exit 1
-fi
-
-set -uo pipefail
-
-if [ $# -eq 0 ]; then
-    # Loop through all code files tracked by Git.
-    mapfile -d '' files < <(git grep -zIl '')
-else
-    # $1 should be a file listing file paths to process. Used in CI.
-    mapfile -d ' ' < <(cat "$1")
-fi
-
-for f in "${files[@]}"; do
-    # Exclude some types of files.
-    if [[ "$f" == *"csproj" ]]; then
-        continue
-    elif [[ "$f" == *"sln" ]]; then
-        continue
-    elif [[ "$f" == *".bat" ]]; then
-        continue
-    elif [[ "$f" == *".out" ]]; then
-        # GDScript integration testing files.
-        continue
-    elif [[ "$f" == *"patch" ]]; then
-        continue
-    elif [[ "$f" == *"pot" ]]; then
-        continue
-    elif [[ "$f" == *"po" ]]; then
-        continue
-    elif [[ "$f" == "thirdparty/"* ]]; then
-        continue
-    elif [[ "$f" == *"/thirdparty/"* ]]; then
-        continue
-    elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
-        continue
-    elif [[ "$f" == *"-so_wrap."* ]]; then
-        continue
-    elif [[ "$f" == *".test.txt" ]]; then
-        continue
-    fi
-    # Ensure that files are UTF-8 formatted.
-    isutf8 "$f" >> utf8-validation.txt 2>&1
-    # Ensure that files have LF line endings and do not contain a BOM.
-    dos2unix "$f" 2> /dev/null
-    # Remove trailing space characters and ensures that files end
-    # with newline characters. -l option handles newlines conveniently.
-    perl -i -ple 's/\s*$//g' "$f"
-done
-
-diff=$(git diff --color)
-
-if [ ! -s utf8-validation.txt ] && [ -z "$diff" ] ; then
-    # If no UTF-8 violations were collected (the file is empty) and
-    # no diff has been generated all is OK, clean up, and exit.
-    printf "\e[1;32m*** Files in this commit comply with the file formatting rules.\e[0m\n"
-    rm -f utf8-validation.txt
-    exit 0
-fi
-
-if [ -s utf8-validation.txt ]
-then
-    # If the file has content and is not empty, violations
-    # detected, notify the user, clean up, and exit.
-    printf "\n\e[1;33m*** The following files contain invalid UTF-8 character sequences:\e[0m\n\n"
-    cat utf8-validation.txt
-fi
-
-rm -f utf8-validation.txt
-
-if [ ! -z "$diff" ]
-then
-    # 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'
-fi
-
-printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
-exit 1

+ 150 - 0
misc/scripts/header_guards.py

@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys
+from pathlib import Path
+
+if len(sys.argv) < 2:
+    print("Invalid usage of header_guards.py, it should be called with a path to one or multiple files.")
+    sys.exit(1)
+
+HEADER_CHECK_OFFSET = 30
+HEADER_BEGIN_OFFSET = 31
+HEADER_END_OFFSET = -1
+
+changed = []
+invalid = []
+
+for file in sys.argv[1:]:
+    with open(file, "rt", encoding="utf-8", newline="\n") as f:
+        lines = f.readlines()
+
+    if len(lines) <= HEADER_BEGIN_OFFSET:
+        continue  # Most likely a dummy file.
+
+    if lines[HEADER_CHECK_OFFSET].startswith("#import"):
+        continue  # Early catch obj-c file.
+
+    split = file.split("/")  # Already in posix-format.
+
+    prefix = ""
+    if split[0] == "modules" and split[-1] == "register_types.h":
+        prefix = f"{split[1]}_"  # Name of module.
+    elif split[0] == "platform" and (file.endswith("api/api.h") or "/export/" in file):
+        prefix = f"{split[1]}_"  # Name of platform.
+    elif file.startswith("modules/mono/utils") and "mono" not in split[-1]:
+        prefix = "MONO_"
+    elif file == "servers/rendering/storage/utilities.h":
+        prefix = "RENDERER_"
+
+    suffix = ""
+    if "dummy" in file and "dummy" not in split[-1]:
+        suffix = "_DUMMY"
+    elif "gles3" in file and "gles3" not in split[-1]:
+        suffix = "_GLES3"
+    elif "renderer_rd" in file and "rd" not in split[-1]:
+        suffix = "_RD"
+    elif split[-1] == "ustring.h":
+        suffix = "_GODOT"
+
+    name = (f"{prefix}{Path(file).stem}{suffix}{Path(file).suffix}".upper()
+            .replace(".", "_").replace("-", "_").replace(" ", "_"))  # fmt: skip
+
+    HEADER_CHECK = f"#ifndef {name}\n"
+    HEADER_BEGIN = f"#define {name}\n"
+    HEADER_END = f"#endif // {name}\n"
+
+    if (
+        lines[HEADER_CHECK_OFFSET] == HEADER_CHECK
+        and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN
+        and lines[HEADER_END_OFFSET] == HEADER_END
+    ):
+        continue
+
+    # Guards might exist but with the wrong names.
+    if (
+        lines[HEADER_CHECK_OFFSET].startswith("#ifndef")
+        and lines[HEADER_BEGIN_OFFSET].startswith("#define")
+        and lines[HEADER_END_OFFSET].startswith("#endif")
+    ):
+        lines[HEADER_CHECK_OFFSET] = HEADER_CHECK
+        lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN
+        lines[HEADER_END_OFFSET] = HEADER_END
+        with open(file, "wt", encoding="utf-8", newline="\n") as f:
+            f.writelines(lines)
+        changed.append(file)
+        continue
+
+    header_check = -1
+    header_begin = -1
+    header_end = -1
+    pragma_once = -1
+    objc = False
+
+    for idx, line in enumerate(lines):
+        if not line.startswith("#"):
+            continue
+        elif line.startswith("#ifndef") and header_check == -1:
+            header_check = idx
+        elif line.startswith("#define") and header_begin == -1:
+            header_begin = idx
+        elif line.startswith("#endif") and header_end == -1:
+            header_end = idx
+        elif line.startswith("#pragma once"):
+            pragma_once = idx
+            break
+        elif line.startswith("#import"):
+            objc = True
+            break
+
+    if objc:
+        continue
+
+    if pragma_once != -1:
+        lines.pop(pragma_once)
+        lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
+        lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
+        lines.append("\n")
+        lines.append(HEADER_END)
+        with open(file, "wt", encoding="utf-8", newline="\n") as f:
+            f.writelines(lines)
+        changed.append(file)
+        continue
+
+    if header_check == -1 and header_begin == -1 and header_end == -1:
+        # Guards simply didn't exist
+        lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
+        lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
+        lines.append("\n")
+        lines.append(HEADER_END)
+        with open(file, "wt", encoding="utf-8", newline="\n") as f:
+            f.writelines(lines)
+        changed.append(file)
+        continue
+
+    if header_check != -1 and header_begin != -1 and header_end != -1:
+        # All prepends "found", see if we can salvage this.
+        if header_check == header_begin - 1 and header_begin < header_end:
+            lines.pop(header_check)
+            lines.pop(header_begin - 1)
+            lines.pop(header_end - 2)
+            if lines[header_end - 3] == "\n":
+                lines.pop(header_end - 3)
+            lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
+            lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
+            lines.append("\n")
+            lines.append(HEADER_END)
+            with open(file, "wt", encoding="utf-8", newline="\n") as f:
+                f.writelines(lines)
+            changed.append(file)
+            continue
+
+    invalid.append(file)
+
+if changed:
+    for file in changed:
+        print(f"FIXED: {file}")
+if invalid:
+    for file in invalid:
+        print(f"REQUIRES MANUAL CHANGES: {file}")
+    sys.exit(1)

+ 0 - 87
misc/scripts/header_guards.sh

@@ -1,87 +0,0 @@
-#!/bin/bash
-
-if [ ! -f "version.py" ]; then
-  echo "Warning: This script is intended to be run from the root of the Godot repository."
-  echo "Some of the paths checks may not work as intended from a different folder."
-fi
-
-if [ $# -eq 0 ]; then
-    # Loop through all code files tracked by Git.
-    files=$(find -name "thirdparty" -prune -o -name "*.h" -print | sed "s@^\./@@g")
-else
-    # $1 should be a file listing file paths to process. Used in CI.
-    files=$(cat "$1" | grep -v "thirdparty/" | grep -E "\.h$" | sed "s@^\./@@g")
-fi
-
-files_invalid_guard=""
-
-for file in $files; do
-  # Skip *.gen.h and *-so_wrap.h, they're generated.
-  if [[ "$file" == *".gen.h" || "$file" == *"-so_wrap.h" ]]; then continue; fi
-  # Has important define before normal header guards.
-  if [[ "$file" == *"thread.h" || "$file" == *"platform_config.h" || "$file" == *"platform_gl.h" ]]; then continue; fi
-  # Obj-C files don't use header guards.
-  if grep -q "#import " "$file"; then continue; fi
-
-  bname=$(basename $file .h)
-
-  # Add custom prefix or suffix for generic filenames with a well-defined namespace.
-
-  prefix=
-  if [[ "$file" == "modules/"*"/register_types.h" ]]; then
-    module=$(echo $file | sed "s@.*modules/\([^/]*\).*@\1@")
-    prefix="${module^^}_"
-  fi
-  if [[ "$file" == "platform/"*"/api/api.h" || "$file" == "platform/"*"/export/"* ]]; then
-    platform=$(echo $file | sed "s@.*platform/\([^/]*\).*@\1@")
-    prefix="${platform^^}_"
-  fi
-  if [[ "$file" == "modules/mono/utils/"* && "$bname" != *"mono"* ]]; then prefix="MONO_"; fi
-  if [[ "$file" == "servers/rendering/storage/utilities.h" ]]; then prefix="RENDERER_"; fi
-
-  suffix=
-  if [[ "$file" == *"dummy"* && "$bname" != *"dummy"* ]]; then suffix="_DUMMY"; fi
-  if [[ "$file" == *"gles3"* && "$bname" != *"gles3"* ]]; then suffix="_GLES3"; fi
-  if [[ "$file" == *"renderer_rd"* && "$bname" != *"rd"* ]]; then suffix="_RD"; fi
-  if [[ "$file" == *"ustring.h" ]]; then suffix="_GODOT"; fi
-
-  # ^^ is bash builtin for UPPERCASE.
-  guard="${prefix}${bname^^}${suffix}_H"
-
-  # Replaces guards to use computed name.
-  # We also add some \n to make sure there's a proper separation.
-  sed -i $file -e "0,/ifndef/s/#ifndef.*/\n#ifndef $guard/"
-  sed -i $file -e "0,/define/s/#define.*/#define $guard\n/"
-  sed -i $file -e "$ s/#endif.*/\n#endif \/\/ $guard/"
-  # Removes redundant \n added before, if they weren't needed.
-  sed -i $file -e "/^$/N;/^\n$/D"
-
-  # Check that first ifndef (should be header guard) is at the expected position.
-  # If not it can mean we have some code before the guard that should be after.
-  # "31" is the expected line with the copyright header.
-  first_ifndef=$(grep -n -m 1 "ifndef" $file | sed 's/\([0-9]*\).*/\1/')
-  if [[ "$first_ifndef" != "31" ]]; then
-    files_invalid_guard+="$file\n"
-  fi
-done
-
-if [[ ! -z "$files_invalid_guard" ]]; then
-  echo -e "The following files were found to have potentially invalid header guard:\n"
-  echo -e "$files_invalid_guard"
-fi
-
-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 header guards formatting 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

+ 0 - 6
misc/scripts/mypy_check.sh

@@ -1,6 +0,0 @@
-#!/usr/bin/env bash
-
-set -uo pipefail
-
-echo -e "Python: mypy static analysis..."
-mypy --config-file=./misc/scripts/mypy.ini .

+ 0 - 5
misc/scripts/pytest_builders.sh

@@ -1,5 +0,0 @@
-#!/usr/bin/env bash
-set -uo pipefail
-
-echo "Running Python checks for builder system"
-pytest ./tests/python_build