# SPDX-FileCopyrightText: 2024 Foundation Devices Inc.
#
# SPDX-License-Identifier: MIT

on:
  pull_request:
  push:
    branches:
      - main

name: Test Example Plugin

jobs:
  Build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macOS-latest
          - windows-latest
        build_mode:
          - debug
          - profile
          - release
    env:
      EXAMPLE_DIR: "a b/hello_rust_ffi_plugin/example"
      CARGOKIT_VERBOSE: 1
    steps:
      - name: Extract branch name
        shell: bash
        run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
        id: extract_branch
      - name: Setup Repository
        shell: bash
        run: |
            mkdir "a b" # Space is intentional
            cd "a b"
            git config --global user.email "you@example.com"
            git config --global user.name "Your Name"
            # "advanced" branch has extra iOS flavor and uses rust nightly for release builds
            git clone -b advanced https://github.com/irondash/hello_rust_ffi_plugin
            cd hello_rust_ffi_plugin
            git subtree pull --prefix cargokit https://github.com/${{ github.event.pull_request.head.repo.full_name || github.repository }} ${{ steps.extract_branch.outputs.branch }} --squash
      - uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
        with:
          channel: "stable"
      - name: Install GTK
        if: (matrix.os == 'ubuntu-latest')
        run: sudo apt-get update && sudo apt-get install libgtk-3-dev
      - name: Install ninja-build
        if: (matrix.os == 'ubuntu-latest')
        run: sudo apt-get update && sudo apt-get install ninja-build
      - name: Build Linux (${{ matrix.build_mode }})
        if: matrix.os == 'ubuntu-latest'
        shell: bash
        working-directory: ${{ env.EXAMPLE_DIR }}
        run: flutter build linux --${{ matrix.build_mode }} -v
      - name: Build macOS (${{ matrix.build_mode }})
        if: matrix.os == 'macos-latest'
        shell: bash
        working-directory: ${{ env.EXAMPLE_DIR }}
        run: flutter build macos --${{ matrix.build_mode }} -v
      - name: Build iOS (${{ matrix.build_mode }})
        if: matrix.os == 'macos-latest'
        shell: bash
        working-directory: ${{ env.EXAMPLE_DIR }}
        run: flutter build ios --${{ matrix.build_mode }} --no-codesign -v
      - name: Build iOS (${{ matrix.build_mode }}) - flavor1
        if: matrix.os == 'macos-latest'
        shell: bash
        working-directory: ${{ env.EXAMPLE_DIR }}
        run: flutter build ios --flavor flavor1 --${{ matrix.build_mode }} --no-codesign -v
      - name: Build Windows (${{ matrix.build_mode }})
        if: matrix.os == 'windows-latest'
        shell: bash
        working-directory: ${{ env.EXAMPLE_DIR }}
        run: flutter build windows --${{ matrix.build_mode }} -v
      - name: Build Android (${{ matrix.build_mode }})
        shell: bash
        working-directory: ${{ env.EXAMPLE_DIR }}
        run: |
          if [[ $(sysctl hw.optional.arm64) == *"hw.optional.arm64: 1"* ]]; then
            export JAVA_HOME=$JAVA_HOME_17_arm64
          else
            export JAVA_HOME=$JAVA_HOME_17_X64
          fi
          flutter build apk --${{ matrix.build_mode }} -v

