# This workflow compiles the example sketches for one of each distinct type of active official Arduino board.
#
# This requires the use of external components (e.g., libraries, boards platform, toolchain), so it is possible for
# compilations to be broken by bugs in the external components, even though the example itself is perfect.
#
# If this happens, the procedure is:
# - Report the bug to the appropriate issue tracker.
# - Adjust this workflow to remove the compilation for the specific board/sketch combination that is failing due to the
#   bug.
# - Add a comment to the workflow providing the URL of the issue report.
# - Once the bug is fixed and the fix released, revert the commit that removed the failing compilation.
# - If the issue is closed as "wontfix", add a comment to the affected examples documenting the incompatibility and open
#   an issue requesting that the associated tutorials be updated.

name: Compile Examples

on:
  pull_request:
    paths:
      - ".github/workflows/compile-examples.yml"
      - "examples/**"
  push:
    paths:
      - ".github/workflows/compile-examples.yml"
      - "examples/**"
  # Scheduled trigger checks for breakage caused by changes to external resources (libraries, platforms)
  schedule:
    # run every Tuesday at 3 AM UTC
    - cron: "0 3 * * 2"
  # workflow_dispatch event allows the workflow to be triggered manually
  # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch
  workflow_dispatch:
  # repository_dispatch event allows the workflow to be triggered via the GitHub API
  # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#repository_dispatch
  repository_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      # Libraries to install for all boards
      UNIVERSAL_LIBRARIES: |
        - name: Servo
        - name: LiquidCrystal
        - name: CapacitiveSensor

      # Sketch paths to compile (recursive) for all boards
      UNIVERSAL_SKETCH_PATHS: >-
        "examples/01.Basics"
        "examples/02.Digital/BlinkWithoutDelay"
        "examples/02.Digital/Button"
        "examples/02.Digital/Debounce"
        "examples/02.Digital/DigitalInputPullup"
        "examples/02.Digital/StateChangeDetection"
        "examples/03.Analog"
        "examples/04.Communication/ASCIITable"
        "examples/04.Communication/Dimmer"
        "examples/04.Communication/Graph"
        "examples/04.Communication/Midi"
        "examples/04.Communication/PhysicalPixel"
        "examples/04.Communication/ReadASCIIString"
        "examples/04.Communication/SerialCallResponse"
        "examples/04.Communication/SerialCallResponseASCII"
        "examples/04.Communication/SerialEvent"
        "examples/04.Communication/VirtualColorMixer"
        "examples/05.Control"
        "examples/06.Sensors/ADXL3xx"
        "examples/06.Sensors/Knock"
        "examples/07.Display"
        "examples/08.Strings"
        "examples/11.ArduinoISP"

    strategy:
      fail-fast: false

      matrix:
        board:
          # For testing the Starter Kit examples
          - fqbn: arduino:avr:uno
            usb: false
            serial1: false
            starter-kit: true
            tone: true
            pulsein: true
          # Adding this in addition to the Uno because it has 1.5 kB less available flash
          - fqbn: arduino:avr:nano
            usb: false
            serial1: false
            starter-kit: false
            tone: true
            pulsein: true
          - fqbn: arduino:avr:leonardo
            usb: true
            serial1: true
            starter-kit: false
            tone: true
            pulsein: true
          - fqbn: arduino:megaavr:uno2018:mode=off
            usb: false
            serial1: true
            starter-kit: false
            tone: true
            pulsein: true
          - fqbn: arduino:samd:mkrzero
            usb: true
            serial1: true
            starter-kit: false
            tone: true
            pulsein: true
          - fqbn: arduino:mbed:nano33ble
            usb: false
            serial1: true
            starter-kit: false
            tone: true
            pulsein: true
          - fqbn: arduino:mbed:envie_m7
            usb: false
            serial1: true
            starter-kit: false
            tone: true
            # Bug report: https://github.com/arduino/ArduinoCore-mbed/issues/48
            # Change the value to true once it is fixed.
            pulsein: false
          - fqbn: arduino:sam:arduino_due_x
            usb: true
            serial1: true
            starter-kit: false
            # Bug report: https://github.com/arduino/ArduinoCore-sam/issues/24
            # Change the value to true once it is fixed.
            tone: false
            pulsein: true

        # Make board type-specific customizations to the matrix jobs
        include:
          - board:
              # Boards with Keyboard+Mouse library compatibility
              usb: true
            # Install these libraries in addition to the ones defined by env.UNIVERSAL_LIBRARIES
            usb-libraries: |
              - name: Keyboard
              - name: Mouse
            # Compile these sketches in addition to the ones defined by env.UNIVERSAL_SKETCH_PATHS
            usb-sketch-paths: >-
              "examples/09.USB"
          - board:
              usb: false
            usb-libraries: ""
            usb-sketch-paths: ""
          - board:
              # Boards with a Serial1 port
              serial1: true
            serial1-sketch-paths: >-
              "examples/04.Communication/MultiSerial"
              "examples/04.Communication/SerialPassthrough"
          - board:
              serial1: false
            serial1-sketch-paths: ""
          - board:
              starter-kit: true
            starter-kit-sketch-paths: >-
              "examples/10.StarterKit_BasicKit"
          - board:
              starter-kit: false
            starter-kit-sketch-paths: ""
          - board:
              tone: true
            tone-sketch-paths: >-
              "examples/02.Digital/toneKeyboard"
              "examples/02.Digital/toneMelody"
              "examples/02.Digital/toneMultiple"
              "examples/02.Digital/tonePitchFollower"
          - board:
              tone: false
            tone-sketch-paths: ""
          - board:
              pulsein: true
            pulsein-sketch-paths: >-
              "examples/06.Sensors/Memsic2125"
              "examples/06.Sensors/Ping"
          - board:
              pulsein: false
            pulsein-sketch-paths: ""

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

      - name: Compile examples
        uses: arduino/actions/libraries/compile-examples@master
        with:
          fqbn: ${{ matrix.board.fqbn }}
          libraries: |
            ${{ env.UNIVERSAL_LIBRARIES }}
            ${{ matrix.usb-libraries }}
          sketch-paths: >-
            ${{ env.UNIVERSAL_SKETCH_PATHS }}
            ${{ matrix.usb-sketch-paths }}
            ${{ matrix.serial1-sketch-paths }}
            ${{ matrix.starter-kit-sketch-paths }}
            ${{ matrix.tone-sketch-paths }}
            ${{ matrix.pulsein-sketch-paths }}
