name: CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

jobs:
  build:
    name: Build Rolens

    strategy:
      fail-fast: false
      matrix:
        platform:
          - windows-2019
          - windows-2022
          - macos-11
          - macos-12
          - macos-13
          - ubuntu-20.04
          - ubuntu-22.04
        go-version: [1.18]
        node-version: [16]

    runs-on: ${{ matrix.platform }}

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: ${{ matrix.go-version }}
        cache-dependency-path: go.sum

    - name: Install Wails
      run: go install github.com/wailsapp/wails/v2/cmd/wails@latest

    - name: Install build dependencies for macOS
      if: contains(matrix.platform, 'macos')
      run: brew install create-dmg

    - name: Install build dependencies for Linux
      if: contains(matrix.platform, 'ubuntu')
      run: sudo apt-get install gtk+-3.0 webkit2gtk-4.0

    - name: Set up Node.js
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: npm
        cache-dependency-path: frontend/package-lock.json

    - name: Cross-compile Rolens for Windows
      if: contains(matrix.platform, 'windows')
      run: ./build/windows/ci_generate.ps1 -platform "${{ matrix.platform }}"

    - name: Cross-compile Rolens for Darwin
      if: contains(matrix.platform, 'macos')
      run: ./build/darwin/ci_generate.sh "${{ matrix.platform }}"

    - name: Cross-compile Rolens for Linux
      if: contains(matrix.platform, 'ubuntu')
      run: ./build/linux/ci_generate.sh "${{ matrix.platform }}"

    - name: Upload generated binaries
      uses: actions/upload-artifact@v2
      with:
        name: rolens-${{ matrix.platform }}
        path: releases/*

  bundle:
    name: Bundle artifacts
    needs: build
    runs-on: ubuntu-22.04

    steps:
    - name: Download artifacts
      uses: actions/download-artifact@v3
      with:
        path: artifacts

    - name: Place all tarballs in the same directory
      run: |
        mkdir bundle
        mv artifacts/*/* bundle

    - name: Upload the bundle as an artifact
      uses: actions/upload-artifact@v3
      with:
        name: rolens-bundle
        path: bundle
