# This file is part of ssh-python.
# Copyright (C) 2017-2022 Panos Kittenis and contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
max_jobs: 5
build:
  parallel: true
  verbosity: minimal
skip_branch_with_pr: true
image: Visual Studio 2019

environment:
  global:
    PYTHONUNBUFFERED: 1
    SYSTEM_LIBSSH: 1
    PYPI_USER:
      secure: WKF8ok0UPIgkA+isDUuHww==
    PYPI_PASS:
      secure: uXkrTaPGSNQdXTJIt3aiy2E1GBnbn6gH3opi7DgMIcmpLgZt7RTeqsaszbjPToGkoqfVXN+jdkQca4LfGAKZ8a2LgrrVYoITdxbjNkZB3yDBKk6ULNMsqdHmt4I1evRhSPSSZnRKU2rSzL+gOTViEVnhcahsUJPgNAIFvpZWsZfdRAafFtF1D33B03aAPwuhYuZTIwRyhUrwVGsYzOzIoDLtnpGj/0+GHazwjhHXfg/yTd600SPiWVsgknxjoeBu+cmi2iMDeT3ULu3P8UGjuQ==
    # Default python version to run single commands with
    PYTHON_DEF: "C:\\Python38-x64"
    PYTHON_VERSION: "3.8"
    # Python versions to build wheels for
    PYTHONVERS: C:\Python38-x64 C:\Python39-x64 C:\Python310-x64 C:\Python311-x64 C:\Python312-x64
    PYTHON_ARCH: "64"

install:
  # If there is a newer build queued for the same PR, cancel this one.
  # The AppVeyor 'rollout builds' option is supposed to serve the same
  # purpose but it is problematic because it tends to cancel builds pushed
  # directly to master instead of just PR builds (or the converse).
  # credits: JuliaLang developers.
  - ps: if ($env:APPVEYOR_PULL_REQUEST_NUMBER -and $env:APPVEYOR_BUILD_NUMBER -ne ((Invoke-RestMethod `
        https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/history?recordsNumber=50).builds | `
        Where-Object pullRequestId -eq $env:APPVEYOR_PULL_REQUEST_NUMBER)[0].buildNumber) { `
          throw "There are newer queued builds for this pull request, failing early." }
  - set OPENSSL_DIR="C:\OpenSSL-v34-Win64"
  - set VCLIBDIR=%WINDIR%\System32
  - cp %VCLIBDIR%/vcruntime140.dll ssh/
  - cp %VCLIBDIR%/msvcr120.dll ssh/
  - cp %OPENSSL_DIR%/bin/*.dll ssh/

  - for %%I in (%PYTHONVERS%) do %%I\python.exe -V
  - for %%I in (%PYTHONVERS%) do %%I\Scripts\pip install -U wheel setuptools twine cython

  # .c files need to be generated on Windows to handle platform
  # specific code.
  # Fix version used by versioneer to current git tag so the generated .c files
  # do not cause a version change.
  - "%PYTHON_DEF%\\python.exe ci/appveyor/fix_version.py ."
  - mv -f .git .git.bak
  - 7z x ci\appveyor\zlib1211.zip
  # - appveyor DownloadFile http://web.mit.edu/kerberos/dist/kfw/4.1/kfw-4.1-amd64.msi
  # - msiexec /i kfw-4.1-amd64.msi /passive /qn
  # - ps: ls "C:\Program Files\MIT\Kerberos\"
  # - cp "C:\Program Files\MIT\Kerberos\*.dll" ssh/
  - ps: ls ssh/*.dll
  - ps: ls ssh
  - ps: ls

build_script:
  - for %%I in (%PYTHONVERS%) do cp %OPENSSL_DIR%/lib/VC/x64/MD/libcrypto.lib %%I/libs/libcrypto64MD.lib
  - for %%I in (%PYTHONVERS%) do cp %OPENSSL_DIR%/lib/VC/x64/MD/libssl.lib %%I/libs/libssl64MD.lib
  - ci\\appveyor\\build_zlib.bat
  - for %%I in (%PYTHONVERS%) do cp C:/zlib/lib/zlibstatic.lib %%I/libs/
  - for %%I in (%PYTHONVERS%) do ls %%I/libs/
  - ci\\appveyor\\build_ssh.bat
  - ps: ls src/src/Release
  - cp src/src/Release/ssh.dll ssh/
  - for %%I in (%PYTHONVERS%) do cp src/src/Release/ssh.lib %%I/libs/
  # Clean C files before build
  - rm -f ssh/*.c
  - for %%I in (%PYTHONVERS%) do %%I\python.exe -V
  - for %%I in (%PYTHONVERS%) do %%I\python.exe setup.py build_ext
  - for %%I in (%PYTHONVERS%) do %%I\python.exe setup.py build
  - for %%I in (%PYTHONVERS%) do %%I\python.exe setup.py install
  - ps: ls ssh

test_script:
  - cd dist
  - for %%I in (%PYTHONVERS%) do %%I\python.exe ..\\ci\\appveyor\\import_test.py
  - cd ..

after_test:
  - for %%I in (%PYTHONVERS%) do %%I\python.exe setup.py bdist_wheel
  - mv dist/* .

artifacts:
  - path: "*.whl"

deploy_script:
  # Calling twine requires we set path
  - "SET PATH=%PYTHON_DEF%;%PYTHON_DEF%\\Scripts;%PATH%"
  - python ci/appveyor/pypi_upload.py *.whl
