name: Build branch

on:
  push:
    paths-ignore:
      - '**.md'
  workflow_dispatch:

jobs:
  build_esp32:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        environment:
          - out: az-touch
            env: "az-touch-mod-esp32_ili9341_4MB -e az-touch-mod-esp32_ili9341_8MB"
          - env: d1-mini-esp32_ili9341
            out: d1-mini-esp32
          - env: d1-r32-waveshare_ili9486
            out: d1-r32-espduino32
          - env: d1-r32-unoshield_ili9341_adc
            out: d1-r32-espduino32
          - env: d1-r32-unoshield_ili9486_adc
            out: d1-r32-espduino32
          - env: esp32-one_st7796
            out: waveshare
          - env: esp32-one_ili9486
            out: waveshare
          - env: esp32-touchdown
            out: esp32-touchdown
          - out: freetouchdeck
            env: "freetouchdeck_4MB -e freetouchdeck_8MB"
          - out: featherwing
            env: "huzzah32-featherwing-24 -e huzzah32-featherwing-35"
          - env: lanbon_l8
            out: lanbon_l8
          - env: lolin-d32-pro_ili9341
            out: lolin-d32-pro
          - env: m5stack-core2
            out: m5stack
          - env: makerfabs-tft35-cap
            out: makerfabs-tft
          - env: ttgo-t7-v1.5_ili9341
            out: ttgo-t7
          - out: wireless-tag
            env: "wt32-sc01_4MB -e wt32-sc01_16MB -e wt-86-32-3zw1"
          - env: yeacreate-nscreen32
            out: yeacreate

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'true'
      - name: Cache pip
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Cache PlatformIO
        uses: actions/cache@v2
        with:
          path: ~/.platformio
          key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
      - name: Set up Python
        uses: actions/setup-python@v2
      - name: Install PlatformIO
        run: |
          python -m pip install --upgrade pip
          pip install --upgrade platformio
      - name: Enable ESP32 platforms from platformio_override-template.ini
        run: |
          sed 's/; user_setups\/esp32/user_setups\/esp32/g' platformio_override-template.ini > platformio_override.ini
      - name: List all files in current folder
        run: |
          ls -la
      - name: Cat platformio_override.ini
        run: |
          cat platformio_override.ini
      - name: Run PlatformIO
        run: pio run -e ${{ matrix.environment.env }}
      - name: Upload output file
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.environment.out }}
          path: build_output/firmware/*.bin
    #- name: Create release and upload firmware
    #  run: |
    #    set -x
    #    assets=()
    #    for asset in build_output/firmware/*.bin; do
    #      assets+=("-a" "$asset")
    #    done
    #    tag_name="${GITHUB_REF##*/}"
    #    hub release create "${assets[@]}" -m "$tag_name" "$tag_name"
    #  env:
    #    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  linux_build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'true'
      - name: Cache pip
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Cache PlatformIO
        uses: actions/cache@v2
        with:
          path: ~/.platformio
          key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
      - name: Set up Python
        uses: actions/setup-python@v2
      - name: Install PlatformIO
        run: |
          python -m pip install --upgrade pip
          pip install --upgrade platformio
      - name: Enable Linux platform from platformio_override.ini
        run: |
          sed 's/; user_setups\/linux/user_setups\/linux/g' platformio_override-template.ini > platformio_override.ini
          mkdir -p .pio/libdeps/linux_sdl_64bits/paho/src
      - name: Install SDL2 library
        run: |
          sudo apt-get update
          sudo apt-get install libsdl2-dev
      - name: List all files in current folder
        run: |
          ls -la
      - name: Cat platformio_override.ini
        run: |
          cat platformio_override.ini
      - name: Enable Linux platform from platformio_override.ini
        run: |
          sed -i 's/; user_setups\/linux/user_setups\/linux/g' platformio_override.ini
          mkdir -p .pio/libdeps/linux_sdl_64bits/paho/src
      - name: Install SDL2 library
        run: |
          sudo apt-get update
          sudo apt-get install libsdl2-dev
      - name: Run PlatformIO
        run: pio run -e linux_sdl_64bits