name: "windows"

on:
  push:
    branches: [ '**']
  pull_request:
    # The branches below must be a subset of the branches above
    branches: [ master ]
  schedule:
    - cron: '27 4 * * 2'
  workflow_dispatch: ~

jobs:

  windows:
    name: windows Build
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - QT_VERSION: '5.12.12'
            ARCH: 'amd64'
            HOST_ARCH: 'amd64'
            COMPILER: 'msvc2017_64'
            METHOD: 'aqt'
            GENERATOR: 'Visual Studio 16 2019'
            RELEASE: false
            os: windows-2019
          - QT_VERSION: '5.12.12'
            ARCH: 'amd64'
            HOST_ARCH: 'amd64'
            COMPILER: 'msvc2017_64'
            TOOLSET: 'v141,version=14.16.27023'
            METHOD: 'aqt'
            GENERATOR: 'Visual Studio 16 2019'
            RELEASE: false
            os: windows-2019
          - QT_VERSION: '5.12.12'
            ARCH: 'x86'
            HOST_ARCH: 'amd64'
            COMPILER: 'msvc2017'
            METHOD: 'aqt'
            GENERATOR: 'Visual Studio 16 2019'
            RELEASE: false
            os: windows-2019
          - QT_VERSION: '6.2.4'
            ARCH: 'amd64'
            HOST_ARCH: 'amd64'
            COMPILER: 'msvc2019_64'
            METHOD: 'aqt'
            GENERATOR: 'Visual Studio 16 2019'
            RELEASE: false
            os: windows-2019
          - QT_VERSION: '6.2.4'
            ARCH: 'amd64'
            HOST_ARCH: 'amd64'
            COMPILER: 'msvc2019_64'
            METHOD: 'aqt'
            GENERATOR: 'Visual Studio 17 2022'
            RELEASE: false
            os: windows-latest
          - QT_VERSION: '6.5.3'
            ARCH: 'amd64'
            HOST_ARCH: 'amd64'
            COMPILER: 'msvc2019_64'
            METHOD: 'aqt'
            RELEASE: true
            GENERATOR: 'Ninja'
            os: windows-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Cache Qt
      uses: actions/cache@v3
      id: cache
      with:
        path: ~/Cache
        key: ${{ runner.os }}-${{ matrix.QT_VERSION }}-${{ matrix.COMPILER }}-${{ secrets.CACHE_VERSION }}

    - name: Install Qt setup(aqt)
      if: steps.cache.outputs.cache-hit != 'true'
      uses: actions/setup-python@v4
      with:
        python-version: '3.9'

    - name: Install Qt
      if: steps.cache.outputs.cache-hit != 'true'
      env:
        CI_BUILD_DIR: ${{ github.workspace }}
      shell: bash
      run: |
        ./tools/ci_install_windows.sh "${{ matrix.QT_VERSION }}" "${{ matrix.COMPILER }}" "${{ matrix.METHOD }}"

    - name: Build
      shell: powershell
      run: |
        .\tools\ci_setup_windows.ps1 -qtdir "$Home\Cache\Qt\${{ matrix.QT_VERSION }}\${{ matrix.COMPILER }}" -arch "${{ matrix.ARCH }}" -host_arch "${{ matrix.HOST_ARCH }}" -vcversion "${{ matrix.VCVERSION }}"
        .\tools\ci_script_windows.ps1 -generator "${{ matrix.GENERATOR }}" -arch "${{ matrix.ARCH }}" -toolset "${{ matrix.TOOLSET }}"

    - name: Rename
      shell: bash
      run: |
        VERSION_ID=$(date -u +%Y%m%dT%H%MZ --date=$(git show -s --format=%aI HEAD))-$(git rev-parse --short=7 HEAD)
        mv bld/gui/GPSBabel-*-Setup.exe bld/gui/GPSBabel-${VERSION_ID}-Setup.exe
        mv bld/gui/GPSBabel-*-Manifest.txt bld/gui/GPSBabel-${VERSION_ID}-Manifest.txt

    - name: Test
      shell: bash
      run: |
        # PATH="${HOME}/Cache/Qt/${{ matrix.QT_VERSION }}/${{ matrix.COMPILER }}/bin:${PATH}"
        PNAME=./bld/gui/package/gpsbabel.exe GBTEMP=./gbtemp ./testo 2>&1
        PNAME=./bld/gui/package/gpsbabel.exe GBTEMP=./gbtemp ./test_encoding_utf8 2>&1

    - name: Deploy
      # This only handles continous releases now, for other events artifacts may be saved in
      # the 'Upload Artifacts' step.
      if: ( github.event_name == 'push' ) && ( github.ref == 'refs/heads/master' ) && matrix.RELEASE
      shell: bash
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        RELEASE_NAME: Continuous-${{ runner.os }}
      run: |
        ./tools/uploadtool/upload_github.sh bld/gui/GPSBabel-*-Setup.exe

    - name: 'Upload Artifacts'
      uses: actions/upload-artifact@v3
      with:
        name: Windows_Installer ${{ join(matrix.*) }}
        path: |
          ./bld/gui/GPSBabel-*-Setup.exe
          ./bld/gui/GPSBabel-*-Manifest.txt
        retention-days: 7
