name: Windows CI

on: [push, pull_request, workflow_dispatch]

jobs:
  msvc-qmake-build:
  
    strategy:
      matrix:
        include:
          - qt_ver: '6.10.3'
            vs: '2022'
            aqt_arch: 'win64_msvc2022_64'
            msvc_arch: 'x64'

    runs-on: windows-2022

    steps:
    - uses: actions/checkout@v6
    - name: Install Qt
      uses: jurplel/install-qt-action@v4
      with:
        arch: ${{ matrix.aqt_arch }}
        version: ${{ matrix.qt_ver }}
        modules: 'qtimageformats'
        cache: true
    - name: Build
      shell: cmd
      run: |
        set VS=${{ matrix.vs }}
        set VCVARS="C:\Program Files (x86)\Microsoft Visual Studio\%VS%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
        if not exist %VCVARS% set VCVARS="C:\Program Files\Microsoft Visual Studio\%VS%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
        call %VCVARS% ${{ matrix.msvc_arch }}
        qmake pineapple-pictures.pro
        nmake
        nmake clean
        windeployqt --verbose=2 --no-quick-import --no-translations --no-opengl-sw --no-system-d3d-compiler --no-system-dxc-compiler --skip-plugin-types tls,networkinformation release\ppic.exe
    - uses: actions/upload-artifact@v6
      with:
        name: "windows-msvc${{ matrix.vs }}-qt${{ matrix.qt_ver }}-qmake-package"
        path: release/*

  msvc-cmake-build:
  
    strategy:
      matrix:
        include:
          - qt_ver: '6.10.3'
            vs: '2022'
            aqt_arch: 'win64_msvc2022_64'
            msvc_arch: 'x64'

    runs-on: windows-2022

    steps:
    - uses: actions/checkout@v6
    - name: Install Qt
      uses: jurplel/install-qt-action@v4
      with:
        arch: ${{ matrix.aqt_arch }}
        version: ${{ matrix.qt_ver }}
        modules: 'qtimageformats'
        cache: true
    - name: Build
      shell: cmd
      run: |
        :: ------ env ------
        set PWD=%cd%
        set VS=${{ matrix.vs }}
        set VCVARS="C:\Program Files (x86)\Microsoft Visual Studio\%VS%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
        if not exist %VCVARS% set VCVARS="C:\Program Files\Microsoft Visual Studio\%VS%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
        call %VCVARS% ${{ matrix.msvc_arch }}
        :: ------ dep ------
        set CMAKE_PREFIX_PATH=%PWD%/dependencies_bin
        mkdir dependencies_src
        echo ::group::===== exiv2 =====
        curl -fsSL -o exiv2_bin.zip https://github.com/Exiv2/exiv2/releases/download/v0.28.7/exiv2-0.28.7-2022msvc-AMD64.zip
        7z x exiv2_bin.zip -y
        ren .\exiv2-0.28.7-2022msvc-AMD64 dependencies_bin
        echo ::endgroup::
        echo ::group::===== zlib =====
        curl -fsSL -o zlib_src.zip https://github.com/madler/zlib/releases/download/v1.3.2/zlib132.zip
        7z x zlib_src.zip -y -o"dependencies_src"
        ren .\dependencies_src\zlib-1.3.2 zlib || goto :error
        cmake ./dependencies_src/zlib -Bbuild_dependencies/zlib -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error
        cmake --build build_dependencies/zlib --config Release --target=install || goto :error
        echo ::endgroup::
        echo ::group::===== AOM for libavif AVI decoding support =====
        git clone -q -b v3.13.1 --depth 1 https://aomedia.googlesource.com/aom dependencies_src/aom
        cmake ./dependencies_src/aom -Bbuild_dependencies/aom -DCMAKE_INSTALL_PREFIX="dependencies_bin" -DENABLE_DOCS=OFF -DBUILD_SHARED_LIBS=ON -DAOM_TARGET_CPU=generic -DENABLE_TESTS=OFF -DENABLE_TESTDATA=OFF -DENABLE_TOOLS=OFF -DENABLE_EXAMPLES=0 || goto :error
        cmake --build build_dependencies/aom --config Release --target=install || goto :error
        echo ::endgroup::
        echo ::group::===== libavif =====
        curl -fsSL -o libavif-v1_3_0.zip https://github.com/AOMediaCodec/libavif/archive/v1.3.0.zip
        7z x libavif-v1_3_0.zip -y -o"dependencies_src"
        ren .\dependencies_src\libavif-1.3.0 libavif || goto :error
        cmake ./dependencies_src/libavif -Bbuild_dependencies/libavif -DCMAKE_INSTALL_PREFIX="dependencies_bin" -DAVIF_CODEC_AOM=SYSTEM -DAVIF_CODEC_AOM_DECODE=ON -DAVIF_CODEC_AOM_ENCODE=ON -DAVIF_LIBYUV=LOCAL
        cmake --build build_dependencies/libavif --config Release --target=install || goto :error
        echo ::endgroup::
        echo ::group::===== expat =====
        curl -fsSL -o expat_src.zip https://github.com/libexpat/libexpat/archive/R_2_7_3.zip
        7z x expat_src.zip -y -o"dependencies_src"
        ren .\dependencies_src\libexpat-R_2_7_3 expat || goto :error
        cmake ./dependencies_src/expat/expat -Bbuild_dependencies/expat -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error
        cmake --build build_dependencies/expat --config Release --target=install || goto :error
        echo ::endgroup::
        echo ::group::===== ECM =====
        git clone -q https://invent.kde.org/frameworks/extra-cmake-modules.git dependencies_src/extra-cmake-modules
        cmake .\dependencies_src\extra-cmake-modules -Bbuild_dependencies/extra-cmake-modules -DCMAKE_INSTALL_PREFIX="dependencies_bin" -DBUILD_TESTING=OFF || goto :error
        cmake --build build_dependencies/extra-cmake-modules --config Release --target=install || goto :error
        echo ::endgroup::
        echo ::group::===== KArchive =====
        git clone -q https://invent.kde.org/frameworks/karchive.git dependencies_src/karchive
        cmake .\dependencies_src\karchive -Bbuild_dependencies/karchive -DBUILD_TESTING=OFF -DWITH_LIBZSTD=OFF -DWITH_BZIP2=OFF -DWITH_LIBLZMA=OFF -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error
        cmake --build build_dependencies/karchive --config Release --target=install || goto :error
        echo ::endgroup::
        echo ::group::===== KImageFormats =====
        git clone -q https://invent.kde.org/frameworks/kimageformats.git dependencies_src/kimageformats
        cmake .\dependencies_src\kimageformats -Bbuild_dependencies/kimageformats -DBUILD_TESTING=OFF -DKDE_INSTALL_QTPLUGINDIR=%QT_ROOT_DIR%\plugins || goto :error
        cmake --build build_dependencies/kimageformats --config Release --target=install || goto :error
        echo ::endgroup::
        :: ------ app ------
        echo ::group::===== App (Build'n'Install) =====
        cmake -Bbuild .
        cmake --build build --config Release
        cmake --install build --config Release --prefix "%PWD%\build"
        echo ::endgroup::
        echo ::group::===== passoc stuff =====
        curl -fsSL -o passoc-standalone.zip https://github.com/BLumia/pineapple-assoc-manager/releases/latest/download/pineapple-assoc-manager-qt${{ matrix.qt_ver }}-standalone.zip
        7z x passoc-standalone.zip passoc.exe -y -o"build/bin"
        echo ::endgroup::
        :: ------ pkg ------
        windeployqt --verbose=2 --no-compiler-runtime --no-quick-import --no-translations --no-network --no-opengl-sw --no-system-d3d-compiler --no-system-dxc-compiler --skip-plugin-types tls,networkinformation build\bin\ppic.exe
        robocopy ./dependencies_bin/bin build/bin *.dll
        if ErrorLevel 8 (exit /B 1)
        copy LICENSE build\bin
        exit /B 0
    - uses: actions/upload-artifact@v6
      with:
        name: "windows-msvc${{ matrix.vs }}-qt${{ matrix.qt_ver }}-cmake-package"
        path: build/bin/*
