version: OpenLP-win-ci-b{build}

cache:
  - '%LOCALAPPDATA%\pip\Cache'
  -  /Users/appveyor/Libraries/Caches/pip

stack: python 3.11

environment:
  matrix:
    - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
      PY_DIR: C:\\Python311-x64
      CHOCO_VLC_ARG:
      FORCE_PACKAGING: 0
      PYICU_PACK: https://github.com/cgohlke/pyicu-build/releases/download/v2.11/PyICU-2.11-cp311-cp311-win_amd64.whl
    - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
      PY_DIR: C:\\Python311
      CHOCO_VLC_ARG: --forcex86
      FORCE_PACKAGING: 0
      PYICU_PACK: https://github.com/cgohlke/pyicu-build/releases/download/v2.11/PyICU-2.11-cp311-cp311-win32.whl
    - APPVEYOR_BUILD_WORKER_IMAGE: macos-catalina
      QT_QPA_PLATFORM: offscreen
      FORCE_PACKAGING: 0
      HOMEBREW_FORCE_BREWED_CURL: 1
      HOMEBREW_NO_AUTO_UPDATE: 1

init:
- cmd: set PATH=%PY_DIR%;%PY_DIR%\Scripts;%PATH%

install:
  # Update pip
  - python -m pip install --upgrade pip
  # Install generic dependencies from pypi.
  - python -m pip install sqlalchemy alembic platformdirs chardet beautifulsoup4 lxml Mako mysql-connector-python pytest mock psycopg2-binary websockets waitress six requests QtAwesome PyQt5 PyQtWebEngine pymediainfo PyMuPDF QDarkStyle python-vlc flask-cors pytest-qt pyenchant pysword qrcode flask packaging
  # Install Windows only dependencies
  - cmd: python -m pip install pyodbc pypiwin32
  - cmd: choco install vlc %CHOCO_VLC_ARG% --no-progress --limit-output
  # Download and install pyicu for windows from https://github.com/cgohlke/pyicu-build
  - cmd: python -m pip install %PYICU_PACK%
  # Mac only dependencies
  - sh: brew install --cask vlc
  - sh: brew install pkg-config icu4c
  - sh: PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:$PATH" PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/opt/icu4c/lib/pkgconfig" python -m pip install pyicu
  - sh: python -m pip install Pyro5 pyobjc-core pyobjc-framework-Cocoa py-applescript

build: off

test_script:
  - ps: >-
      If (($env:APPVEYOR_REPO_TAG -eq $False)) {
        cd $env:APPVEYOR_BUILD_FOLDER
        # Run the tests
        python -m pytest tests
        if ($LastExitCode -ne 0) {
          throw "The tests failed!"
        }
        # Go back to the user root folder
        cd ..
      }

after_test:
  # Only package on the master repo
  - ps: >-
      If (($env:APPVEYOR_REPO_TAG -eq $True) -Or ($env:APPVEYOR_SCHEDULED_BUILD -eq $True) -Or ($env:FORCE_PACKAGING -eq 1)) {
          # Continue on error
          $ErrorActionPreference = "Continue"
          # This is where we create a package using PyInstaller
          # Install PyInstaller
          python -m pip install --no-warn-script-location pyinstaller
          # Some windows only stuff...
          If ($isWindows) {
            # Disabled portable installers - can't figure out how to make them silent
            # - curl -L -O http://downloads.sourceforge.net/project/portableapps/PortableApps.com%20Installer/PortableApps.comInstaller_3.4.4.paf.exe
            # - PortableApps.comInstaller_3.4.4.paf.exe /S
            # - curl -L -O http://downloads.sourceforge.net/project/portableapps/PortableApps.com%20Launcher/PortableApps.comLauncher_2.2.1.paf.exe
            # - PortableApps.comLauncher_2.2.1.paf.exe /S
            # - curl -L -O http://downloads.sourceforge.net/project/portableapps/NSIS%20Portable/NSISPortable_3.0_English.paf.exe
            # - NSISPortable_3.0_English.paf.exe /S
            # Download and unpack portable-bundle
            appveyor DownloadFile https://get.openlp.org/win-sdk/portable-setup.7z
            7z x portable-setup.7z
            # Install VLC - Windows only
            choco install vlc $env:CHOCO_VLC_ARG --no-progress --limit-output
          }
          Else {
            # Install Mac only stuff
            # install dmgbuild tool
            python -m pip install --no-warn-script-location dmgbuild
            # use brew to build enchant, needed for pyenchant
            brew install enchant
          }
          # Get the packaging code
          Invoke-WebRequest -Uri "https://gitlab.com/openlp/packaging/-/archive/master/packaging-master.zip" -OutFile packaging-master.zip
          Expand-Archive -Path packaging-master.zip -DestinationPath .
          cd packaging-master
          If ($env:APPVEYOR_REPO_TAG -eq $True) {
            # If this is a release build, set release argument
            $releaseArg = "--release ""$env:APPVEYOR_REPO_TAG_NAME"""
            If ($isWindows) {
                $cmd = "python builders/windows-builder.py $releaseArg --skip-update -c windows/config-appveyor.ini -b ""$env:APPVEYOR_BUILD_FOLDER"" --portable"
                iex $cmd
            } else {
                $cmd = "python builders/macosx-builder.py $releaseArg --skip-update -c osx/config-appveyor.ini -b ""$env:APPVEYOR_BUILD_FOLDER"""
                iex $cmd
            }
          }
          Else {
            If ($isWindows) {
                python builders/windows-builder.py --skip-update --skip-translations -c windows/config-appveyor.ini -b "$env:APPVEYOR_BUILD_FOLDER" --portable
            }
            Else {
                python builders/macosx-builder.py --skip-update --skip-translations -c osx/config-appveyor.ini -b "$env:APPVEYOR_BUILD_FOLDER"
            }
          }
      }

artifacts:
  - path: dist\*.exe
    name: Windows Portable-installer
  - path: dist\*.msi
    name: Windows Installer
  - path: dist\*.dmg
    name: MacOSX Installer
