name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build Release Binaries
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: naru-linux-x64
          - target: x86_64-apple-darwin
            os: macos-latest
            name: naru-macos-x64
          - target: aarch64-apple-darwin
            os: macos-latest
            name: naru-macos-arm64
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            name: naru-windows-x64.exe

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      
      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Rename binary
        shell: bash
        run: |
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            mv target/${{ matrix.target }}/release/naru.exe ${{ matrix.name }}
          else
            mv target/${{ matrix.target }}/release/naru ${{ matrix.name }}
          fi

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

  publish:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - name: Download all artifacts
        uses: actions/download-artifact@v4
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            naru-linux-x64/naru-linux-x64
            naru-macos-x64/naru-macos-x64
            naru-macos-arm64/naru-macos-arm64
            naru-windows-x64.exe/naru-windows-x64.exe
          body_path: CHANGELOG.md
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
