name: Build

on:
  pull_request:
  push:
  repository_dispatch:
  release:
    types:
      - created

jobs:
  ESP32-S2:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        board:
        # Alphabetical order
        - 'adafruit_feather_esp32s2'
        - 'adafruit_feather_esp32s2_tft'
        - 'adafruit_magtag_29gray'
        - 'adafruit_metro_esp32s2'
        - 'espressif_kaluga_1'
        - 'espressif_saola_1_wroom'
        - 'espressif_saola_1_wrover'
        - 'gravitech_cucumberRIS_v1.1'
        - 'lilygo_ttgo_t8_s2_st7789'
        - 'microdev_micro_s2'
        - 'olimex_esp32s2_devkit_lipo_vB1'
        - 'targett_mcb_wroom'
        - 'targett_mcb_wrover'
        - 'unexpectedmaker_feathers2'
    steps:
    - name: Setup Python
      uses: actions/setup-python@v2

    - name: Checkout
      uses: actions/checkout@v2
      with:
        submodules: 'true'

    - uses: actions/cache@v2
      name: Fetch IDF tool cache
      id: idf-cache
      with:
        path: ${{ github.workspace }}/.idf_tools
        key: ${{ runner.os }}-idf-tools-${{ hashFiles('.git/modules/lib/esp-idf/HEAD') }}-20201222

    - name: Clone IDF submodules
      run: |
        (cd $IDF_PATH && git submodule update --init)
      env:
        IDF_PATH: ${{ github.workspace }}/lib/esp-idf

    - name: Install IDF tools
      if: steps.idf-cache.outputs.cache-hit != 'true'
      run: |
        $IDF_PATH/tools/idf_tools.py --non-interactive install required
        $IDF_PATH/tools/idf_tools.py --non-interactive install cmake
        $IDF_PATH/tools/idf_tools.py --non-interactive install-python-env
        rm -rf $IDF_TOOLS_PATH/dist
      env:
        IDF_PATH: ${{ github.workspace }}/lib/esp-idf
        IDF_TOOLS_PATH: ${{ github.workspace }}/.idf_tools

    - name: Build
      run: |
        source $IDF_PATH/export.sh
        make -C ports/esp32s2/ BOARD=${{ matrix.board }} all self-update copy-artifact
      env:
        IDF_PATH: ${{ github.workspace }}/lib/esp-idf
        IDF_TOOLS_PATH: ${{ github.workspace }}/.idf_tools

    - uses: actions/upload-artifact@v2
      with:
        name: ${{ matrix.board }}
        path: ports/esp32s2/_bin/${{ matrix.board }}/

    - name: Create Release Asset
      if: ${{ github.event_name == 'release' }}
      run: |
        #for f in ports/esp32s2/_bin/${{ matrix.board }}/*; do mv $f ${f%.*}-${{ github.event.release.tag_name }}."${f#*.}"; done
        zip -jr tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.zip ports/esp32s2/_bin/${{ matrix.board }}

    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: ${{ github.event_name == 'release' }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.zip
        asset_name: tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.zip
        asset_content_type: application/zip

    - name: Upload Release Asset for Self-Update
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: ${{ github.event_name == 'release' }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: ports/esp32s2/_bin/${{ matrix.board }}/update-tinyuf2.uf2
        asset_name: update-tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.uf2
        asset_content_type: application/x-binary

  ARM:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        board:
        # Alphabetical order
        # lpc55
        - 'double_m33_express'
        - 'lpcxpresso55s28'
        - 'lpcxpresso55s69'
        # mimxrt10xx
        - 'imxrt1010_evk'
        - 'imxrt1020_evk'
        - 'imxrt1060_evk'
        - 'metro_m7_1011'
        #stm32f3
        - 'stm32f303disco'
        # stm32f4
        - 'feather_stm32f405_express'
        - 'stm32f411ve_discovery'
        - 'stm32f401_blackpill'

    steps:
    - name: Setup Python
      uses: actions/setup-python@v2

    - name: Setup Node.js
      uses: actions/setup-node@v1

    - name: Checkout code
      uses: actions/checkout@v2
      with:
        submodules: 'true'

    - name: Install Toolchains
      run: |
        npm install --global xpm
        xpm install --global @xpack-dev-tools/arm-none-eabi-gcc@latest
        echo `echo $HOME/.local/xPacks/@xpack-dev-tools/arm-none-eabi-gcc/*/.content/bin` >> $GITHUB_PATH

    - name: Find Port
      run: |
        PORT=`echo ports/*/boards/${{ matrix.board }}`
        PORT=`dirname $PORT`
        PORT=`dirname $PORT`
        echo PORT=$PORT >> $GITHUB_ENV

    - name: Build
      run: make -C $PORT BOARD=${{ matrix.board }} all copy-artifact

    - uses: actions/upload-artifact@v2
      with:
        name: ${{ matrix.board }}
        path: ${{ env.PORT }}/_bin/${{ matrix.board }}

    - name: Create Release Asset
      if: ${{ github.event_name == 'release' }}
      run: |
        for f in ${{ env.PORT }}/_bin/${{ matrix.board }}/*; do mv $f ${f%.*}-${{ github.event.release.tag_name }}."${f#*.}"; done
        zip -jr tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.zip ${{ env.PORT }}/_bin/${{ matrix.board }}

    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: ${{ github.event_name == 'release' }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.zip
        asset_name: tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.zip
        asset_content_type: application/zip

    - name: Upload Release Asset for Self-Update
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: ${{ github.event_name == 'release' }}
      with:
        upload_url: ${{ github.event.release.upload_url }}
        asset_path: ${{ env.PORT }}/_bin/${{ matrix.board }}/update-tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.uf2
        asset_name: update-tinyuf2-${{ matrix.board }}-${{ github.event.release.tag_name }}.uf2
        asset_content_type: application/x-binary
