name: CI

on:
  push:
    branches: [master, trunk]
  pull_request:

jobs:
  fmt:
    name: Check formatting
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: nightly
          components: rustfmt
          override: true
    - name: Check formatting
      uses: actions-rs/cargo@v1
      with:
        command: fmt
        args: -- --check
  test:
    name: Check that examples build
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: stable
          override: true
    - uses: actions-rs/cargo@v1
      with:
        command: test
  build:
    name: Check that the code builds
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: stable
          override: true
    - uses: actions-rs/cargo@v1
      with:
        command: build
  examples:
    name: Check that examples build
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: stable
          override: true
    - uses: actions-rs/cargo@v1
      with:
        command: build
        args: --features webview --example webview_custom_protocol
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: stable
          override: true
          target: x86_64-apple-ios
    # Since it's all Objective-C message passing under the hood, we're
    # really just looking for whether we've broken the iOS build. It is likely
    # that more robust tests/checking infrastructure should exist for this side
    # of things as the iOS portion gets iterated on.
    #
    # (e.g, this at the moment will not catch invalid selector calls, like if an appkit-specific
    # selector is used for something on iOS)
    - uses: actions-rs/cargo@v1
      with:
        command: build
        args: --target x86_64-apple-ios --example ios-beta --no-default-features --features uikit,autolayout

  ios:
    name: Check that iOS tests pass via dinghy.
    runs-on: macos-latest
    steps:

    - name: Install cargo-dinghy
      uses: baptiste0928/cargo-install@v1
      with:
        crate: cargo-dinghy

    - uses: actions/checkout@v3
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: stable
          override: true
          target: x86_64-apple-ios

    - name: Launch XCode Simulator and prepare Dinghy
      run: |
        # Get system info
        xcrun simctl list runtimes
        # Launch the simulator
        RUNTIME_ID=$(xcrun simctl list runtimes | grep iOS | cut -d ' ' -f 7 | tail -1)
        SIM_ID=$(xcrun simctl create My-iphone-se com.apple.CoreSimulator.SimDeviceType.iPhone-14 $RUNTIME_ID)
        xcrun simctl boot $SIM_ID

    - name: Dinghy test
      run: |
        cargo dinghy --platform auto-ios-x86_64 test --no-default-features --features uikit,autolayout