name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            binary: irona
            archive: irona-linux-x86_64.tar.gz
            use_cross: false
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            binary: irona
            archive: irona-linux-aarch64.tar.gz
            use_cross: true
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            binary: irona
            archive: irona-linux-x86_64-musl.tar.gz
            use_cross: true
          - target: aarch64-apple-darwin
            os: macos-latest
            binary: irona
            archive: irona-macos-arm64.tar.gz
            use_cross: false
          - target: x86_64-apple-darwin
            os: macos-latest
            binary: irona
            archive: irona-macos-x86_64.tar.gz
            use_cross: false
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            binary: irona.exe
            archive: irona-windows-x86_64.zip
            use_cross: false

    steps:
      - uses: actions/checkout@v4

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

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Install cross
        if: matrix.use_cross
        uses: taiki-e/install-action@cross

      - name: Build (native)
        if: ${{ !matrix.use_cross }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build (cross)
        if: matrix.use_cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Package (Unix)
        if: matrix.os != 'windows-latest'
        run: |
          cp target/${{ matrix.target }}/release/${{ matrix.binary }} .
          tar czf ${{ matrix.archive }} ${{ matrix.binary }}

      - name: Package (Windows)
        if: matrix.os == 'windows-latest'
        run: |
          cp target/${{ matrix.target }}/release/${{ matrix.binary }} .
          Compress-Archive -Path ${{ matrix.binary }} -DestinationPath ${{ matrix.archive }}

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: ${{ matrix.archive }}
          path: ${{ matrix.archive }}

  release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install git-cliff
        uses: taiki-e/install-action@git-cliff

      - name: Generate changelog
        run: git cliff --current --output CHANGELOG.md

      - name: Download all artifacts
        uses: actions/download-artifact@v5
        with:
          path: artifacts
          merge-multiple: true

      - name: Publish release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          body_path: CHANGELOG.md

  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: release
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Publish
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
