clone_folder: C:\repo

matrix:
  fast_finish: true

environment:
  global:
    DOWNLOAD_DIR: _download_cache
    APPVEYOR_SAVE_CACHE_ON_ERROR: true
    PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\Git\cmd;C:\Tools\curl\bin;C:\msys64\usr\bin;C:\Program Files\7-zip;C:\Program Files (x86)\Inno Setup 6
  matrix:
    - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
      VS_VER: 14
      PLATFORM: x64
      PYTHON: C:\Python39-x64\python.exe

build_script:
  # Show environment info.
  - set APPVEYOR_BUILD_WORKER_IMAGE
  - set PATH
  - "%PYTHON% --version"
  - '%PYTHON% -c "import platform; print(platform.architecture())"'

  # Set RedNotebook VERSION variable.
  - for /f %%i in ('%PYTHON% -c "from rednotebook import info; print(info.version)"') do set VERSION=%%i
  - set VERSION

  # Set up GTK stack compiled with gvsbuild.
  - curl -Lo gtk-bundle.zip https://www.dropbox.com/s/lo0p8c96cljcc1c/gtk-bundle.zip?dl=1
  - 7z x gtk-bundle.zip -oC:\gtk
  - ls C:\gtk
  - set PATH=%PATH%;C:\gtk\bin

  # Somehow the msgfmt binary (used for building translations) is already available.
  # If it ever disappears, use the following steps to install it (untested).
  # - curl -Lo gettext.zip https://github.com/mlocati/gettext-iconv-windows/releases/download/v0.21-v1.16/gettext0.21-iconv1.16-shared-64.zip
  # - 7z x gettext.zip -oC:\gettext
  # - set PATH=%PATH%;C:\gettext\bin

  # Install other dependencies.
  - "%PYTHON% -m pip install --no-warn-script-location -U pip setuptools tox==3.27.1 wheel"
  # Install separately because --no-binary option disables using wheels (leading to problems for Cython).
  - "%PYTHON% -m pip install --no-warn-script-location -U --no-binary :all: pyenchant==3.2.2"
  - "%PYTHON% -m pip install --no-warn-script-location -r win/requirements.txt"
  - "%PYTHON% -m pip freeze"
  - "%PYTHON% -m pip list --outdated"

  # Patch GtkSourceView hook by replacing "3.0" with "4".
  - sed -i 's/3\.0/4/g' C:/Python39-x64/Lib/site-packages/pyinstaller/hooks/hook-gi.repository.GtkSource.py

  # Pyenchant wheels bundle DLLs. The bundled DLLs are incompatible to the DLLs
  # we use and lead to the error "ImportError: DLL load failed: The specified
  # procedure could not be found." when calling "import gi". Therefore, we use
  # the --no-binary option when installing the pyenchant package and use our own
  # libenchant.dll file (see also
  # https://pyenchant.github.io/pyenchant/install.html).
  # Enable debug logging for Pyenchant.
  - set PYENCHANT_VERBOSE_FIND=foobar

  # Install dictionaries.
  - curl -Lo dicts.tar.gz https://www.dropbox.com/s/f12k20aur6znpmo/myspell-dicts.tar.gz?dl=1
  - tar -xzf dicts.tar.gz
  - move enchant C:\gtk\share

  # Run tests.
  - "%PYTHON% -m tox -v -e py"

  - cd win

  # Sanity check with dummy app. PyInstaller misses Enchant DLLs, so we manually add them.
  - "%PYTHON% sampleapp.py"
  - "%PYTHON% -m PyInstaller --additional-hooks-dir=. --add-data C:/gtk/bin/libenchant.dll;. --add-data C:/gtk/lib/enchant/libenchant_myspell.dll;lib/enchant/ sampleapp.py"
  - cp -r C:\gtk\share\enchant dist\sampleapp\share\enchant
  - ls dist\sampleapp
  - ls dist\sampleapp\lib\enchant
  - ls dist\sampleapp\share
  - ls dist\sampleapp\share\enchant\
  - ls dist\sampleapp\share\enchant\myspell
  # Use clean PATH to check that everything has been copied correctly. The last semicolon ensures that the path doesn't end with a space.
  - cmd /C "set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\repo\win\dist\sampleapp; && dist\sampleapp\sampleapp.exe"

  # Create executable.
  - "%PYTHON% -m PyInstaller --workpath=C:/build --distpath=C:/ rednotebook.spec"
  - "%PYTHON% ../dev/build_translations.py C:/dist/share/locale"
  - cp -r C:\gtk\share\enchant C:\dist\share\enchant
  - tar -czf rednotebook.tar.gz -C C:\ dist
  - 7z a rednotebook.zip C:\dist

  # Build installer.
  - iscc /Qp /dREDNOTEBOOK_VERSION="%VERSION%" rednotebook.iss

  # Artifacts have to live under "clone_folder".
  - move rednotebook.tar.gz C:\repo\rednotebook-"%VERSION%".tar.gz
  - move rednotebook.zip C:\repo\rednotebook-"%VERSION%".zip
  - move C:\rednotebook.exe C:\repo\rednotebook-"%VERSION%".exe

artifacts:
  - path: rednotebook-*.tar.gz
    name: Standalone Windows executable (tarball)
  - path: rednotebook-*.zip
    name: Standalone Windows executable (zipfile)
  - path: rednotebook-*.exe
    name: Windows installer

deploy:
  description: ''
  provider: GitHub
  auth_token:
    secure: "p5SL/whtBmrrK+4Aqxng6+5+GNdq7uzwJDBnC7mz7U3ailXjz6LglDXf1+2sR6lN"
  artifact: /rednotebook-.*\.exe/
  draft: false
  prerelease: false
  on:
    appveyor_repo_tag: true

cache:
  - '%DOWNLOAD_DIR%'

test: off
