Bläddra i källkod

CI: Replace cmake-format with gersemi for CMake file format checks

PatTheMav 1 år sedan
förälder
incheckning
19d3e30a3a

+ 0 - 54
.cmake-format.json

@@ -1,54 +0,0 @@
-{
-  "format": {
-    "line_width": 120,
-    "tab_size": 2,
-    "enable_sort": true,
-    "autosort": true
-  },
-  "additional_commands": {
-    "find_qt": {
-      "flags": [],
-      "kwargs": {
-        "COMPONENTS": "+",
-        "COMPONENTS_WIN": "+",
-        "COMPONENTS_MACOS": "+",
-        "COMPONENTS_LINUX": "+"
-      }
-    },
-    "set_target_properties_obs": {
-      "pargs": 1,
-      "flags": [],
-      "kwargs": {
-        "PROPERTIES": {
-          "kwargs": {
-            "PREFIX": 1,
-            "OUTPUT_NAME": 1,
-            "FOLDER": 1,
-            "VERSION": 1,
-            "SOVERSION": 1,
-            "FRAMEWORK": 1,
-            "BUNDLE": 1,
-            "AUTOMOC": 1,
-            "AUTOUIC": 1,
-            "AUTORCC": 1,
-            "AUTOUIC_SEARCH_PATHS": 1,
-            "BUILD_RPATH": 1,
-            "INSTALL_RPATH": 1,
-            "XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC": 1,
-            "XCODE_ATTRIBUTE_CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION": 1,
-            "XCODE_ATTRIBUTE_GCC_WARN_SHADOW":1 ,
-            "LIBRARY_OUTPUT_DIRECTORY": 1
-          }
-        }
-      }
-    },
-    "add_obs_plugin": {
-      "pargs": 1,
-      "flags": ["WITH_MESSAGE"],
-      "kwargs": {
-        "PLATFORMS": "+",
-        "ARCHITECTURES": "+"
-      }
-    }
-  }
-}

+ 59 - 0
.github/actions/run-gersemi/action.yaml

@@ -0,0 +1,59 @@
+name: Run gersemi
+description: Runs gersemi and checks for any changes introduced by it
+inputs:
+  failCondition:
+    description: Controls whether failed checks also fail the workflow run
+    required: false
+    default: never
+  workingDirectory:
+    description: Working directory for checks
+    required: false
+    default: ${{ github.workspace }}
+runs:
+  using: composite
+  steps:
+    - name: Check Runner Operating System 🏃‍♂️
+      if: runner.os == 'Windows'
+      shell: bash
+      run: |
+        : Check Runner Operating System 🏃‍♂️
+        echo "::notice::run-gersemi action requires a macOS-based or Linux-based runner."
+        exit 2
+
+    - name: Check for Changed Files ✅
+      uses: ./.github/actions/check-changes
+      id: checks
+      with:
+        checkGlob: "'*.cmake' '*CMakeLists.txt'"
+        diffFilter: 'ACM'
+
+    - name: Install Dependencies 🛍️
+      if: runner.os == 'Linux' && fromJSON(steps.checks.outputs.hasChangedFiles)
+      shell: bash
+      run: |
+        : Install Dependencies 🛍️
+        echo ::group::Install Dependencies
+        eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
+        echo "/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin" >> $GITHUB_PATH
+        brew install --quiet zsh
+        echo ::endgroup::
+
+    - name: Run gersemi 🎛️
+      if: fromJSON(steps.checks.outputs.hasChangedFiles)
+      id: result
+      shell: zsh --no-rcs --errexit --pipefail {0}
+      working-directory: ${{ github.workspace }}
+      env:
+        CHANGED_FILES: ${{ steps.checks.outputs.changedFiles }}
+      run: |
+        : Run gersemi 🎛️
+        if (( ${+RUNNER_DEBUG} )) setopt XTRACE
+
+        print ::group::Install gersemi
+        brew install --quiet obsproject/tools/gersemi
+        print ::endgroup::
+
+        print ::group::Run gersemi
+        local -a changes=(${(s:,:)CHANGED_FILES//[\[\]\'\"]/})
+        ./build-aux/run-gersemi --fail-${{ inputs.failCondition }} --check ${changes}
+        print ::endgroup::

+ 4 - 4
.github/workflows/check-format.yaml

@@ -26,15 +26,15 @@ jobs:
         with:
           failCondition: error
 
-  cmake-format:
+  gersemi:
     runs-on: ubuntu-22.04
     steps:
       - uses: actions/checkout@v4
         with:
           fetch-depth: 0
-      - name: cmake-format Check 🎛️
-        id: cmake-format
-        uses: ./.github/actions/run-cmake-format
+      - name: gersemi Check 🎛️
+        id: gersemi
+        uses: ./.github/actions/run-gersemi
         with:
           failCondition: error
 

+ 106 - 29
build-aux/.run-format.zsh

@@ -60,27 +60,81 @@ invoke_formatter() {
 
       local -a format_args=(-style=file -fallback-style=none)
       if (( _loglevel > 2 )) format_args+=(--verbose)
+
+      check_files() {
+        local -i num_failures=0
+        local -a source_files=($@)
+        local file
+        local -a format_args=(-style=file -fallback-style=none)
+        if (( _loglevel > 2 )) format_args+=(--verbose)
+
+        local -a command=(${formatter} ${format_args})
+
+        for file (${source_files}) {
+          if ! ${command} "${file}" | diff -q "${file}" - &> /dev/null; then
+            log_error "${file} requires formatting changes."
+            if (( fail_on_error == 2 )) return 2;
+            num_failures=$(( num_failures + 1 ))
+          fi
+        }
+        if (( num_failures && fail_on_error == 1 )) return 2
+      }
+
+      format_files() {
+        local -a source_files=($@)
+
+        if (( ${#source_files} )) {
+          local -a format_args=(-style=file -fallback-style=none -i)
+          if (( _loglevel > 2 )) format_args+=(--verbose)
+
+          "${formatter}" ${format_args} ${source_files}
+        }
+      }
       ;;
-    cmake)
-      local formatter=cmake-format
-      if (( ${+commands[cmake-format]} )) {
-        local cmake_format_version=$(cmake-format --version)
+    gersemi)
+      local formatter=gersemi
+      if (( ${+commands[gersemi]} )) {
+        local gersemi_version=($(gersemi --version))
 
-        if ! is-at-least 0.6.13 ${cmake_format_version}; then
-          log_error "cmake-format is not version 0.6.13 or above (found ${cmake_format_version})."
+        if ! is-at-least 0.12.0 ${gersemi_version[2]}; then
+          log_error "gersemi is not version 0.12.0 or above (found ${gersemi_version[2]}."
           exit 2
         fi
-      } else {
-        log_error "No viable cmake-format version found (required 0.6.13)"
-        exit 2
       }
 
-      if (( ! #source_files )) source_files=((libobs|libobs-*|UI|plugins|deps|shared|cmake)/**/(CMakeLists.txt|*.cmake)(.N))
+      if (( ! #source_files )) source_files=(CMakeLists.txt (libobs|libobs-*|UI|plugins|deps|shared|cmake|test)/**/(CMakeLists.txt|*.cmake)(.N))
 
       source_files=(${source_files:#*/(jansson|decklink/*/decklink-sdk|obs-websocket|obs-browser|win-dshow/libdshowcapture)/*})
+      source_files=(${source_files:#(cmake/Modules/*|*/legacy.cmake)})
+
+      check_files() {
+        local -i num_failures=0
+        local -a source_files=($@)
+        local file
+        local -a command=(${formatter} -c --no-cache ${source_files})
+
+        if (( ${#source_files} )) {
+          while read -r line; do
+            local -a line_tokens=(${(z)line})
+            file=${line_tokens[1]//*obs-studio\//}
 
-      local -a format_args=()
-      if (( _loglevel > 2 )) format_args+=(--log-level debug)
+            log_error "${file} requires formatting changes."
+
+            if (( fail_on_error == 2 )) return 2
+            num_failures=$(( num_failures + 1 ))
+          done < <(${command} 2>&1)
+
+          if (( num_failures && fail_on_error == 1 )) return 2
+        }
+      }
+
+      format_files() {
+        local -a source_files=($@)
+
+        if (( ${#source_files} )) {
+          "${formatter}" -i ${source_files}
+        }
+      }
       ;;
     swift)
       local formatter=swift-format
@@ -98,30 +152,53 @@ invoke_formatter() {
 
       if (( ! #source_files )) source_files=((libobs|libobs-*|UI|plugins)/**/*.swift(.N))
 
-      local -a format_args=()
+      check_files() {
+        local -i num_failures=0
+        local -a source_files=($@)
+        local file
+        local -a format_args=()
+
+        local -a command=(${formatter} ${format_args})
+
+        for file (${source_files}) {
+          if ! "${command}" "${file}" | diff -q "${file}" - &> /dev/null; then
+            log_error "${file} requires formatting changes."
+            if (( fail_on_error == 2 )) return 2;
+            num_failures=$(( num_failures + 1 ))
+          fi
+        }
+        if (( num_failures && fail_on_error == 1 )) return 2
+      }
+
+      format_files() {
+        local -a source_files=($@)
+
+        if (( ${#source_files} )) {
+          local -a format_args=(-i)
+
+          "${formatter}" ${format_args} ${source_files}
+        }
+      }
       ;;
-    *) log_error "Invalid formatter specified: ${1}. Valid options are clang-format, cmake-format, and swift-format."; exit 2 ;;
+    *) log_error "Invalid formatter specified: ${1}. Valid options are clang-format, gersemi, and swift-format."; exit 2 ;;
   }
 
   local file
   local -i num_failures=0
   if (( check_only )) {
-    for file (${source_files}) {
-      if (( _loglevel > 1 )) log_info "Checking format of ${file}..."
-
-      if ! "${formatter}" ${format_args} "${file}" | diff -q "${file}" - &> /dev/null; then
-        log_error "${file} requires formatting changes."
-
-        if (( fail_on_error == 2 )) return 2;
-        num_failures=$(( num_failures + 1 ))
-      else
-        if (( _loglevel > 1 )) log_status "${file} requires no formatting changes."
-      fi
+    if (( ${+functions[check_files]} )) {
+      check_files ${source_files}
+    } else {
+      log_error "No format check function defined for formatter '${formatter}'"
+      exit 2
+    }
+  } else {
+    if (( ${+functions[format_files]} )) {
+      format_files ${source_files}
+    } else {
+      log_error "No format function defined for formatter '${formatter}'"
+      exit 2
     }
-    if (( fail_on_error && num_failures )) return 2;
-  } elif (( ${#source_files} )) {
-    format_args+=(-i)
-    "${formatter}" ${format_args} ${source_files}
   }
 }
 

+ 3 - 3
build-aux/README.md

@@ -23,13 +23,13 @@ Example of use:
 ./build-aux/run-clang-format
 ```
 
-### `run-cmake-format`
+### `run-gersemi-format`
 
-This script allows to check the formatting and/or format of the CMake files and requires ZSH and `cmakelang` (`cmake-format`) Python package.
+This script allows to check the formatting and/or format of the CMake files and requires ZSH and `gersemi` Python package.
 
 Example of use:
 ```sh
-./build-aux/run-cmake-format
+./build-aux/run-gersemi
 ```
 
 ### `run-swift-format`

+ 0 - 0
build-aux/run-cmake-format → build-aux/run-gersemi