#---------------------------------#
#      general configuration      #
#---------------------------------#

version: 3.3.1-{build}-{branch}

branches:
  only:
    - develop
    - master

image: Visual Studio 2019

environment:
  CONDA: c:\Miniconda37-x64
  ECMWF: c:\ecmwf
  GIT_CLONE_DIR: $(ECMWF)\git
  INSTALL_DIR: $(ECMWF)\install
  MAGICS_SRC: $(GIT_CLONE_DIR)\magics
  MAGICS_TESTS_SRC: ${MAGICS_SRC}/../magics-test
  MAGICS_PYTHON_TESTS: $(MAGICS_TESTS_SRC)\test\gallery
  MAGPLUS_HOME: $(INSTALL_DIR)
  MAGICS_PYTHON_SRC: $(GIT_CLONE_DIR)\magics_python
  ECBUILD_SRC: $(GIT_CLONE_DIR)\ecbuild

# scripts that are called at very beginning, before repo cloning
init:
  # activate MSVC environment - https://www.appveyor.com/docs/lang/cpp/
  - cmd: call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
  # activate conda environment so we use the correct version of python during the build
  - cmd: call %CONDA%\Scripts\activate.bat
  # make sure git clones symlinks as symlinks
  - cmd: git config --global core.symlinks true
  # add conda bins to path so tests can find linux utils
  - cmd: set PATH=%CONDA%\Library\usr\bin;%CONDA%\Library\bin;%CONDA%\Scripts;%CONDA%\bin;%PATH%
  # add magics\build\bin to path so tests can find magics.dll
  - cmd: set PATH=%PATH%;%MAGICS_SRC%\build\bin
  # add install_dir to path so tests can find eccodes.dll
  - cmd: set PATH=%PATH%;%INSTALL_DIR%
  # auto-yes for conda
  - cmd: conda config --set always_yes yes
  # update conda
  - cmd: conda update -n base -c conda-forge conda

clone_folder: $(MAGICS_SRC)

clone_depth: 1

# scripts that run after cloning repository
install:
  # install ecbuild
  - cmd: git clone --depth 1 https://github.com/ecmwf/ecbuild.git %ECBUILD_SRC% -b %APPVEYOR_REPO_BRANCH%

  # install magics-python src
  - cmd: git clone -b master --depth 1 https://github.com/ecmwf/magics-python.git %MAGICS_PYTHON_SRC%
  - cmd: git clone --depth 1 https://github.com/ecmwf/magics-test.git %MAGICS_TESTS_SRC%

  # install linux utils
  - cmd: conda install -c msys2 m2-bash ^
                                m2-findutils ^
                                m2-coreutils ^
                                m2-grep ^
                                m2-sed ^
                                m2-gawk ^
                                m2-diffutils ^
                                m2-perl ^
                                m2-unzip

  # install conda deps
  - cmd: conda install -c conda-forge libnetcdf expat xarray scipy cftime
  - cmd: conda install -c conda-forge cmake jom proj4=6 pthreads-win32 libiconv eccodes pytest
  - cmd: pip install findlibs

  # gtk+ - needed for pango
  # includes cairo, pango, glib, and other deps
  - cmd: cd %ECMWF%
  - cmd: curl -O -L http://ftp.gnome.org/pub/gnome/binaries/win64/gtk+/2.22/gtk+-bundle_2.22.1-20101229_win64.zip
  - cmd: unzip gtk+-bundle_2.22.1-20101229_win64.zip -d gtk
  - cmd: rm gtk+-bundle_2.22.1-20101229_win64.zip
  - cmd: set GTK=%ECMWF%\gtk
  - cmd: set PATH=%PATH%;%GTK%\bin;%GTK%\lib

#---------------------------------#
#       build configuration       #
#---------------------------------#

platform:
  - x64

before_build:
  # add include paths
  - cmd: set INCLUDE=%INCLUDE%;%GTK%\include
  - cmd: set INCLUDE=%INCLUDE%;%GTK%\include\cairo
  - cmd: set INCLUDE=%INCLUDE%;%GTK%\include\glib-2.0;%GTK%\lib\glib-2.0\include
  # check we got all the dlls
  # gtk
  - cmd: where libpango-1.0-0.dll
  - cmd: where libglib-2.0-0.dll
  - cmd: where libcairo-2.dll
  - cmd: where libfontconfig-1.dll
  - cmd: where libfreetype-6.dll
  - cmd: where libpng14-14.dll
  - cmd: where libintl-8.dll
  # eccodes
  - cmd: where eccodes.dll
  # conda
  - cmd: where pthreadvse2.dll
  - cmd: where netcdf.dll
  - cmd: where vcruntime140.dll

build_script:
  - cmd: cd %MAGICS_SRC%
  - cmd: mkdir build && cd build
  - cmd: cmake -G "NMake Makefiles JOM" ^
               -D CMAKE_INSTALL_PREFIX=%INSTALL_DIR% ^
               -D CMAKE_BUILD_TYPE=Release ^
               -D GTK_PATH=%GTK%
               ..
  - cmd: jom -j2
  - cmd: ctest --output-on-failure
  - cmd: jom install

#---------------------------------#
#       tests configuration       #
#---------------------------------#

test_script:
  - cmd: cd %MAGICS_PYTHON_SRC%
  - cmd: set PYTHONPATH=%PYTHONPATH%;%MAGICS_PYTHON_SRC%
  - cmd: pytest
