gdstyle 0.4.0

A fast, opinionated linter and formatter for GDScript (Godot 4.x)
Documentation
name: Release

on:
  push:
    tags:
      - "v*"
  # Lets the crates.io token be checked on demand, without cutting a release.
  # On a manual run only `preflight` executes; every other job is push-gated.
  workflow_dispatch:

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  # `publish-crates` runs *after* `release`, so an expired CARGO_REGISTRY_TOKEN
  # would otherwise surface only once the GitHub release and all 9 assets were
  # already published, leaving a tag that never reaches crates.io. Checking the
  # token up front turns that into a clean pre-release failure.
  preflight:
    name: Preflight - crates.io token
    runs-on: ubuntu-latest
    steps:
      - name: Verify CARGO_REGISTRY_TOKEN
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          set -uo pipefail
          if [ -z "${CARGO_REGISTRY_TOKEN}" ]; then
            echo "::error::CARGO_REGISTRY_TOKEN is not set. Add it at Settings > Secrets and variables > Actions."
            exit 1
          fi

          body=$(mktemp)
          status=$(curl -sS -o "$body" -w '%{http_code}' \
            -H "Authorization: ${CARGO_REGISTRY_TOKEN}" \
            -H 'User-Agent: gdstyle-release (github.com/atelico/gdstyle)' \
            https://crates.io/api/v1/me) || status=000

          if [ "$status" = "200" ]; then
            echo "crates.io accepted the token."
            echo "Note: crates.io exposes token *expiry* only on the website;"
            echo "check https://crates.io/settings/tokens for the expiry date."
            exit 0
          fi

          # Only "authentication failed" unambiguously means the token is
          # expired/revoked. Anything else (network blip, scope rule, outage)
          # is inconclusive, and must not block a release on its own.
          if grep -q 'authentication failed' "$body"; then
            echo "::error::crates.io rejected CARGO_REGISTRY_TOKEN - it is expired or revoked. Generate a new one at https://crates.io/settings/tokens and update the repository secret."
            exit 1
          fi

          echo "::warning::Could not verify CARGO_REGISTRY_TOKEN (HTTP $status). Continuing; the publish step may still fail."
          head -c 400 "$body" || true

  build-cli:
    name: CLI - ${{ matrix.target }}
    if: github.event_name == 'push'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact: gdstyle
          - target: x86_64-apple-darwin
            os: macos-latest
            artifact: gdstyle
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact: gdstyle
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            artifact: gdstyle.exe

    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Build CLI
        run: cargo build --release --target ${{ matrix.target }} -p gdstyle

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../gdstyle-${{ matrix.target }}.tar.gz ${{ matrix.artifact }}

      - name: Package (Windows)
        if: runner.os == 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          7z a ../../../gdstyle-${{ matrix.target }}.zip ${{ matrix.artifact }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: cli-${{ matrix.target }}
          path: gdstyle-${{ matrix.target }}.*

  build-gdextension:
    name: GDExtension - ${{ matrix.target }}
    if: github.event_name == 'push'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            lib_name: libgdstyle_gdext.so
          - target: x86_64-apple-darwin
            os: macos-latest
            lib_name: libgdstyle_gdext.dylib
          - target: aarch64-apple-darwin
            os: macos-latest
            lib_name: libgdstyle_gdext.dylib
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            lib_name: gdstyle_gdext.dll

    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Build GDExtension
        run: cargo build --release --target ${{ matrix.target }} -p gdstyle-gdext

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../gdstyle-gdext-${{ matrix.target }}.tar.gz ${{ matrix.lib_name }}

      - name: Package (Windows)
        if: runner.os == 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          7z a ../../../gdstyle-gdext-${{ matrix.target }}.zip ${{ matrix.lib_name }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: gdext-${{ matrix.target }}
          path: gdstyle-gdext-${{ matrix.target }}.*

  package-godot-plugin:
    name: Package Godot plugin
    if: github.event_name == 'push'
    needs: build-gdextension
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Download all GDExtension artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Assemble plugin
        run: |
          mkdir -p plugin-package/addons/gdstyle/bin

          # Copy plugin source files.
          cp godot-plugin/addons/gdstyle/plugin.cfg plugin-package/addons/gdstyle/
          cp godot-plugin/addons/gdstyle/plugin.gd plugin-package/addons/gdstyle/
          cp godot-plugin/addons/gdstyle/gdstyle_panel.gd plugin-package/addons/gdstyle/
          cp godot-plugin/addons/gdstyle/gdstyle.gdextension plugin-package/addons/gdstyle/

          # Ship the license inside the addon folder, where users actually
          # end up with it. Copied from the repo root so it can't go stale.
          cp LICENSE plugin-package/addons/gdstyle/LICENSE

          # Sync plugin.cfg version to the git tag (e.g. v0.1.0 -> 0.1.0)
          # so the asset library listing always matches the release.
          TAG_VERSION="${GITHUB_REF_NAME#v}"
          sed -i "s/^version=.*/version=\"${TAG_VERSION}\"/" plugin-package/addons/gdstyle/plugin.cfg
          echo "plugin.cfg version set to ${TAG_VERSION}"

          # Bundle the icon if one exists at the addon root.
          if [ -f godot-plugin/addons/gdstyle/icon.png ]; then
            cp godot-plugin/addons/gdstyle/icon.png plugin-package/addons/gdstyle/
          fi
          if [ -f godot-plugin/addons/gdstyle/icon.svg ]; then
            cp godot-plugin/addons/gdstyle/icon.svg plugin-package/addons/gdstyle/
          fi

          # Extract and place platform libraries.
          cd artifacts

          tar xzf gdext-x86_64-unknown-linux-gnu/gdstyle-gdext-x86_64-unknown-linux-gnu.tar.gz
          mv libgdstyle_gdext.so ../plugin-package/addons/gdstyle/bin/

          tar xzf gdext-x86_64-apple-darwin/gdstyle-gdext-x86_64-apple-darwin.tar.gz
          mv libgdstyle_gdext.dylib ../plugin-package/addons/gdstyle/bin/libgdstyle_gdext.x86_64.dylib

          tar xzf gdext-aarch64-apple-darwin/gdstyle-gdext-aarch64-apple-darwin.tar.gz
          mv libgdstyle_gdext.dylib ../plugin-package/addons/gdstyle/bin/libgdstyle_gdext.arm64.dylib
          7z x gdext-x86_64-pc-windows-msvc/gdstyle-gdext-x86_64-pc-windows-msvc.zip -o.
          mv gdstyle_gdext.dll ../plugin-package/addons/gdstyle/bin/

      - name: Package plugin
        run: |
          cd plugin-package
          zip -r ../gdstyle-godot-plugin.zip addons/

      - name: Upload plugin package
        uses: actions/upload-artifact@v4
        with:
          name: godot-plugin
          path: gdstyle-godot-plugin.zip

  release:
    name: Create GitHub Release
    if: github.event_name == 'push'
    needs: [preflight, build-cli, build-gdextension, package-godot-plugin]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          body_path: RELEASE_NOTES.md
          generate_release_notes: true
          files: |
            artifacts/cli-*/gdstyle-*
            artifacts/gdext-*/gdstyle-gdext-*
            artifacts/godot-plugin/gdstyle-godot-plugin.zip

  publish-crates:
    name: Publish to crates.io
    if: github.event_name == 'push'
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      # Only the `gdstyle` crate is published; `gdstyle-gdext` has
      # publish = false in its Cargo.toml because it's a Godot-only artifact
      # shipped via the plugin zip, not via crates.io.
      - name: Publish gdstyle
        run: cargo publish -p gdstyle
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}