name: Release

on:
  push:
    tags:
      - "v*"

jobs:
  build_release:
    name: NIF ${{ matrix.nif }} - ${{ matrix.job.target }} (${{ matrix.job.os }})
    runs-on: ${{ matrix.job.os }}

    strategy:
      fail-fast: false
      matrix:
        nif: ["2.15"]
        job:
          - { target: aarch64-apple-darwin,      os: macos-14 }
          - { target: x86_64-apple-darwin,       os: macos-13 }
          - { target: x86_64-unknown-linux-gnu,  os: ubuntu-22.04 }
          - { target: x86_64-pc-windows-gnu,     os: windows-2022 }

    steps:
      - name: Checkout source code
        uses: actions/checkout@v4

      - name: Extract crate information
        shell: bash
        run: |
          echo "PROJECT_VERSION=$(sed -n 's/^  @version "\(.*\)"/\1/p' mix.exs | head -n1)" >> $GITHUB_ENV

      - name: Install system dependencies (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libssl-dev pkg-config


      - name: Install Java for macOS (required for extractous)
        if: runner.os == 'macOS'
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Add target
        shell: bash
        run: |
          rustup target add ${{ matrix.job.target }}


      - name: Build the project
        id: build-crate
        uses: philss/rustler-precompiled-action@v1.1.4
        with:
          project-name: extractousex_native
          project-version: ${{ env.PROJECT_VERSION }}
          target: ${{ matrix.job.target }}
          nif-version: ${{ matrix.nif }}
          use-cross: ${{ matrix.job.use-cross }}

      - name: Artifact upload
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.build-crate.outputs.file-name }}
          path: ${{ steps.build-crate.outputs.file-path }}

      - name: Publish archives and packages
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ${{ steps.build-crate.outputs.file-path }}
        if: startsWith(github.ref, 'refs/tags/')