name: Release

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

permissions:
  contents: write
  id-token: write

env:
  PACKAGE_NAME: linthis

jobs:
  # Build wheels for Linux
  linux:
    name: Build Linux ${{ matrix.target }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: ubuntu-latest
            target: x86_64
          - runner: ubuntu-24.04-arm
            target: aarch64
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --locked --out dist
          manylinux: '2_28'
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-${{ matrix.target }}
          path: dist
      - name: Archive binary
        run: |
          ARCH=${{ matrix.target }}
          if [ "$ARCH" = "x86_64" ]; then
            TARGET=x86_64-unknown-linux-gnu
          else
            TARGET=aarch64-unknown-linux-gnu
          fi
          ARCHIVE_NAME=${{ env.PACKAGE_NAME }}-$TARGET
          ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz

          mkdir -p $ARCHIVE_NAME
          cp target/$TARGET/release/${{ env.PACKAGE_NAME }} $ARCHIVE_NAME/
          tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
          shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: binaries-linux-${{ matrix.target }}
          path: |
            *.tar.gz
            *.sha256

  # Build wheels for Windows
  windows:
    name: Build Windows x86_64
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: x86_64
          args: --release --locked --out dist
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-windows-x86_64
          path: dist
      - name: Archive binary
        shell: bash
        run: |
          TARGET=x86_64-pc-windows-msvc
          ARCHIVE_NAME=${{ env.PACKAGE_NAME }}-$TARGET
          ARCHIVE_FILE=$ARCHIVE_NAME.zip

          mkdir -p $ARCHIVE_NAME
          cp target/$TARGET/release/${{ env.PACKAGE_NAME }}.exe $ARCHIVE_NAME/
          7z a $ARCHIVE_FILE $ARCHIVE_NAME
          sha256sum $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: binaries-windows-x86_64
          path: |
            *.zip
            *.sha256

  # Build wheels for macOS
  macos:
    name: Build macOS ${{ matrix.target }}
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: macos-15-intel
            target: x86_64
          - runner: macos-14
            target: aarch64
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --locked --out dist
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-${{ matrix.target }}
          path: dist
      - name: Archive binary
        run: |
          ARCH=${{ matrix.target }}
          if [ "$ARCH" = "x86_64" ]; then
            TARGET=x86_64-apple-darwin
          else
            TARGET=aarch64-apple-darwin
          fi
          ARCHIVE_NAME=${{ env.PACKAGE_NAME }}-$TARGET
          ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz

          mkdir -p $ARCHIVE_NAME
          cp target/$TARGET/release/${{ env.PACKAGE_NAME }} $ARCHIVE_NAME/
          tar czvf $ARCHIVE_FILE $ARCHIVE_NAME
          shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256
      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: binaries-macos-${{ matrix.target }}
          path: |
            *.tar.gz
            *.sha256

  # Build source distribution
  sdist:
    name: Build sdist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: wheels-sdist
          path: dist

  # Create GitHub Release
  github-release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    needs: [linux, windows, macos, sdist]
    steps:
      - uses: actions/checkout@v4
      - name: Download all binaries
        uses: actions/download-artifact@v4
        with:
          pattern: binaries-*
          path: binaries
          merge-multiple: true
      - name: Prepare release body
        id: release_body
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          cat << EOF > release_body.md
          ## Installation

          \`\`\`bash
          pip install linthis==$VERSION
          # or
          pip install --upgrade linthis
          \`\`\`

          ## Quick Start

          \`\`\`bash
          # Run linthis
          linthis [-i <path>]
          \`\`\`

          See [CHANGELOG.md](CHANGELOG.md) for details.
          EOF
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: binaries/*
          generate_release_notes: true
          body_path: release_body.md
          draft: false
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # Publish to PyPI
  publish-pypi:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    needs: [linux, windows, macos, sdist]
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  # Publish to crates.io
  publish-crates:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    needs: [linux, windows, macos, sdist]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Run tests
        run: cargo test --locked
      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --locked
