name: Windows Qt6
on:
  # push代码时触发workflow
  push:
    # 忽略README.md
    paths-ignore:
      - "README.md"
      - "LICENSE"
      - "BUILD.md"
  # pull_request时触发workflow
  pull_request:
    # 忽略README.md
    paths-ignore:
      - "README.md"
      - "LICENSE"
      - "BUILD.md"
  release:
    types: [published]
jobs:
  build:
    name: Build
    strategy:
      matrix:
        qt_version: [6.9.3]
        build_type: [Release, RelWithDebInfo]
        platform: [windows-latest]
        include:
          - platform: windows-latest
            arch: x64
            qtarch: win64_msvc2022_64
      fail-fast: false

    runs-on: ${{ matrix.platform }}
    env:
      targetName: lemon.exe
    # 步骤
    steps:
      - name: Get the version
        id: get_version
        shell: bash
        run: echo "VERSION=$(echo $GITHUB_REF | cut -d / -f 3)" >> $GITHUB_OUTPUT
      - name: Checking out sources
        uses: actions/checkout@v4
        with:
          submodules: "recursive"
      - name: Install Python 3.12 version
        uses: actions/setup-python@v4
        with:
          python-version: "3.12"
          architecture: x64
      - name: Restoring submodules
        run: git submodule update --init
      # =========================================================================================================
      - name: Install MSVC compiler
        uses: ilammy/msvc-dev-cmd@v1
        with:
          # 14.1 is for vs2017, 14.2 is vs2019, following the upstream vcpkg build from Qv2ray-deps repo
          arch: x64
      - name: Cache Qt
        id: cache-qt
        uses: actions/cache@v4
        with:
          path: ../Qt
          key: QtCache-${{ matrix.platform }}-x64-${{ matrix.qt_version }}
      - name: Installing Qt - x64
        uses: jurplel/install-qt-action@v4
        with:
          version: ${{ matrix.qt_version }}
          arch: ${{ matrix.qtarch }}
          mirror: "http://mirrors.ocf.berkeley.edu/qt/"
          cached: ${{ steps.cache-qt.outputs.cache-hit }}
          setup-python: "false"
          modules: "qtactiveqt"

      - name: Win-x64 - ${{ matrix.qt_version }} - Generate Dependencies and Build
        shell: bash
        env:
          CC: cl.exe
          CXX: cl.exe
        run: |
          cmake . \
            -GNinja \
            -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
            -DLEMON_QT6=ON \
            -DLEMON_BUILD_INFO="Build for Windows" \
            -DLEMON_BUILD_EXTRA_INFO="Build on Windows x64" \
            -DENABLE_XLS_EXPORT=ON
          cmake --build . --parallel $(nproc)
      - name: package
        env:
          archiveName: lemon.${{ steps.get_version.outputs.VERSION }}.Windows-${{ matrix.arch }}
        shell: pwsh
        run: |
          # 创建文件夹
          New-Item -ItemType Directory ${{ env.archiveName }}
          # 拷贝exe
          Copy-Item ${{ env.targetName }} ${{ env.archiveName}}\
          # 拷贝依赖
          windeployqt --qmldir . ${{ env.archiveName }}\${{ env.targetName }}
          # 打包zip
          Compress-Archive -Path ${{ env.archiveName }} ${{ env.archiveName }}'.zip'
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        env:
          archiveName: lemon.${{ steps.get_version.outputs.VERSION }}.Windows-${{ matrix.arch }}
        with:
          name: lemon-win-qt6-x64-${{ matrix.build_type }}.zip
          path: ${{ env.archiveName }}.zip
      - name: uploadRelease
        if: startsWith(github.event.ref, 'refs/tags/')
        uses: svenstaro/upload-release-action@v2
        env:
          archiveName: lemon.${{ steps.get_version.outputs.VERSION }}.Windows-${{ matrix.arch }}
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ./${{ env.archiveName }}.zip
          asset_name: lemon-win-qt6-x64-${{ matrix.build_type }}.zip
          tag: ${{ github.ref }}
          overwrite: true
