name: Build DAPLink (Windows)
on:
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-2019

    steps:
    - name: Cache Embedded Arm Toolchain
      id:   cache-arm-gcc
      uses: actions/cache@v2
      env:
        cache-name: arm-gcc-10.3-2021-07
      with:
        path: ${{ runner.temp }}/arm-gcc
        key:  ${{ runner.os }}-${{ env.cache-name }}
        restore-keys: ${{ runner.os }}-${{ env.cache-name }}

    - name: Cache Python modules
      uses: actions/cache@v2
      with:
        path: ~\AppData\Local\pip\Cache
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-

    - name: Checkout source files
      uses: actions/checkout@v2
      with:
        fetch-depth: 0

    - name: Install Embedded Arm Toolchain
      if:   steps.cache-arm-gcc.outputs.cache-hit != 'true'
      run:  |
        (New-Object System.Net.WebClient).DownloadFile("https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.07/gcc-arm-none-eabi-10.3-2021.07-win32.zip","gcc-arm-none-eabi-10.3-2021.07-win32.zip");
        echo "Installing gcc-arm-none-eabi-9-2020-q2-update-win32 in ${{ runner.temp }}\arm-gcc";
        Expand-Archive -Path .\gcc-arm-none-eabi-10.3-2021.07-win32.zip -DestinationPath ${{ runner.temp }} -PassThru;
        Rename-Item -Path ${{ runner.temp }}\gcc-arm-none-eabi-10.3-2021.07 -NewName ${{ runner.temp }}\arm-gcc

    - name: Check Embedded Arm Toolchain
      run:  |
        echo ${{ runner.temp }}\arm-gcc\bin | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        ${{ runner.temp }}\arm-gcc\bin\arm-none-eabi-gcc -v

    - name: Install Python module
      run:  |
        pip install -r requirements.txt

    - name: Compile
      run:  |
        python tools/progen_compile.py -t cmake_gcc_arm -g mingw-make --release --parallel -v
      shell: cmd

    - name: Upload test artifacts
      uses: actions/upload-artifact@v2
      with:
        name: firmware-dev-${{github.run_number}}
        path: |
          firmware*/*
          !firmware*/*.zip
