name: Create Release Builds

on:
  push:
    tags:
      - "v.*.*.*" # matches v.1.0.1 etc

jobs:
  once:
    name: Create GitHub release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Create a release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: true
          prerelease: true

  build:
    name: Cross-platform release build
    needs: once
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            file: target/release/boilr
            asset_name: linux_BoilR
            args: --best --lzma
            strip: true
          - os: windows-latest
            file: target/release/boilr.exe
            asset_name: windows_BoilR.exe
            args: -9
            strip: false      
          - os: macos-latest
            file: target/release/boilr
            asset_name: macos_x86_64_BoilR
            args: -9
            strip: false
          - os: macos-latest
            file: target/aarch64-apple-darwin/release/boilr
            asset_name: macos_darwin_BoilR
            args: -9
            strip: false
            target: aarch64-apple-darwin
    steps:
      - uses: actions/checkout@v3
      - name: Install dependencies
        if: ${{ matrix.os == 'ubuntu-latest'}}
        run: |
          sudo apt-get update 
          sudo apt-get install -y libclang-dev libgtk-3-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libspeechd-dev libxkbcommon-dev libssl-dev
      - name: Rust Cache
        id: rust_cache
        uses: Swatinem/rust-cache@v2
      - name: Build Release Version
        if: ${{ matrix.target != 'aarch64-apple-darwin' }}
        run: cargo build --release     
      - name: Build aarch64 Release Version 
        if: ${{matrix.target == 'aarch64-apple-darwin' }}
        run: rustup target add aarch64-apple-darwin && cargo build --release --target aarch64-apple-darwin
      - name: Compress binaries
        uses: svenstaro/upx-action@v2
        with:
          file: ${{ matrix.file }}
          args: ${{ matrix.args }}
          strip: ${{ matrix.strip }}
      - name: Upload Release Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.once.outputs.upload_url }}
          asset_path: ${{ github.workspace }}/${{ matrix.file }}
          asset_name: ${{ matrix.asset_name }}
          asset_content_type: application/octet-stream
