name: release

on:
  push:
    tags:
      - 'v*.*.*'

permissions:
  contents: write   # for creating releases

jobs:
  build:
    name: build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact: wire
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            artifact: wire
            cross: true
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            artifact: wire
            cross: true
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            artifact: wire
            cross: true
          # x86_64-apple-darwin (Intel Mac) deliberately dropped in v0.1.0.
          # macos-13 GitHub-hosted runners had multi-hour queue starvation;
          # Apple Silicon (aarch64-apple-darwin) covers Mac users post-2020.
          # Intel Mac users on this version fall back to: cargo install --git
          # https://github.com/SlanchaAi/wire wire. Re-add if v0.2 demand surfaces.
          - target: aarch64-apple-darwin
            os: macos-14
            artifact: wire
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            artifact: wire.exe

    steps:
      - uses: actions/checkout@v4

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

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Install cross (for non-host Linux builds)
        if: matrix.cross == true
        run: cargo install cross --git https://github.com/cross-rs/cross --locked

      - name: Build (cross)
        if: matrix.cross == true
        run: cross build --release --target ${{ matrix.target }} --bin wire

      - name: Build (cargo)
        if: matrix.cross != true
        run: cargo build --release --target ${{ matrix.target }} --bin wire

      - name: Stage artifact
        shell: bash
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/${{ matrix.artifact }} dist/wire-${{ matrix.target }}${{ runner.os == 'Windows' && '.exe' || '' }}
          cd dist
          if [ "${{ runner.os }}" = "Windows" ]; then
            powershell -Command "(Get-FileHash wire-${{ matrix.target }}.exe -Algorithm SHA256).Hash.ToLower() + '  wire-${{ matrix.target }}.exe' | Out-File -Encoding ascii wire-${{ matrix.target }}.exe.sha256"
          else
            shasum -a 256 wire-${{ matrix.target }} > wire-${{ matrix.target }}.sha256
          fi

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

  release:
    name: github release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          draft: false
          prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-beta') || contains(github.ref, '-alpha') }}
          generate_release_notes: true

  publish-crates-io:
    name: cargo publish (crates.io)
    needs: build
    runs-on: ubuntu-latest
    # Skip pre-release tags; crates.io is for stable consumers only.
    if: ${{ !contains(github.ref, '-rc') && !contains(github.ref, '-beta') && !contains(github.ref, '-alpha') }}
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      # v0.6.8: this is the load-bearing fix. Pre-v0.6.8 release.yml
      # had no `cargo publish` step, so every tagged release built
      # GitHub binaries but `cargo install slancha-wire` stayed
      # frozen at the last manually-published version (v0.6.1).
      # Operators running the install.sh fallback path silently got
      # stale binaries for 6 patch releases.
      #
      # Skips gracefully if the version on the tag is already on
      # crates.io (`cargo publish` exits non-zero — we `|| true` so a
      # re-run of an already-published tag doesn't fail the workflow).
      - name: cargo publish slancha-wire
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          if [ -z "${CARGO_REGISTRY_TOKEN:-}" ]; then
            echo "::warning::CARGO_REGISTRY_TOKEN secret not set — skipping crates.io publish"
            echo "::warning::add it under repo Settings → Secrets and variables → Actions"
            exit 0
          fi
          cargo publish --locked || {
            echo "::warning::cargo publish failed — likely the version is already on crates.io"
            exit 0
          }
