version: 0.0.0.{build}
image: Visual Studio 2017


environment:
    matrix:
        - MBITS: 32
          PYTHON_VERSION: 37
          MSVC_VERSION: 15
          DEPLOY_CORE: 1
          DEPLOY_PYTHON_BINDINGS: 1
          DEPLOY_JAVA_BINDINGS: 1
          DEPLOY_PERL_BINDINGS: 1
          DEPLOY_CSHARP_BINDINGS: 1
        - MBITS: 64
          PYTHON_VERSION: 37
          MSVC_VERSION: 15
          DEPLOY_CORE: 1
          DEPLOY_PYTHON_BINDINGS: 1
          DEPLOY_JAVA_BINDINGS: 1
          DEPLOY_PERL_BINDINGS: 1
          DEPLOY_CSHARP_BINDINGS: 1
        
        - MBITS: 32
          PYTHON_VERSION: 37
          MSVC_VERSION: 14
          DEPLOY_CORE: 1
        - MBITS: 64
          PYTHON_VERSION: 37
          MSVC_VERSION: 14
          DEPLOY_CORE: 1


install:
    - ps: |
        if ($env:MBITS -eq "32") {
            echo "Architecture set to 32 bit."
            $env:CMAKE_GENERATOR="Visual Studio $env:MSVC_VERSION"
            $env:PYTHON_PATH="C:/Python$env:PYTHON_VERSION"
        } elseif ($env:MBITS -eq "64") {
            echo "Architecture set to 64 bit."
            $env:CMAKE_GENERATOR="Visual Studio $env:MSVC_VERSION Win64"
            $env:PYTHON_PATH="C:/Python$env:PYTHON_VERSION-x64"
        } else {
            echo "No architecture set. Build will be canceled."
        }
        # Install SWIG by Choco
        choco install -y --no-progress swig --version 4.0.1
        choco install -y --no-progress openjdk
        # Install Strawberry Perl and put before any other Perls in %PATH%
        if ($env:MBITS -eq "32") {
            choco install -y --no-progress --forcex86 strawberryperl
        } elseif ($env:MBITS -eq "64") {
            choco install -y --no-progress strawberryperl
        } else {
            echo "No architecture set. Build will be canceled."
        }
        $Env:PATH="C:\strawberry\perl\bin;C:\strawberry\perl\site\bin;C:\strawberry\c\bin;$Env:PATH"
        $Env:PERL_VERSION=$(perl -e 'print $^V')

before_build:
    - mkdir build
    - cd build
    - cmake -G "%CMAKE_GENERATOR%" ../ -DBUILD_BINDINGS_PYTHON=%DEPLOY_PYTHON_BINDINGS% -DPYTHON_LIBRARY="%PYTHON_PATH%/libs/python%PYTHON_VERSION%.lib" -DBUILD_BINDINGS_JAVA=%DEPLOY_JAVA_BINDINGS% -DBUILD_BINDINGS_CSHARP=%DEPLOY_CSHARP_BINDINGS%
    - cd ../
    - ps: |
        if ($Env:DEPLOY_PERL_BINDINGS -eq "1") {
            mkdir build-perl
            cd build-perl
            # Need to use Unix Makefiles because Strawberry Perl is a MinGW environment
            cmake -G "Unix Makefiles" ../ -DBUILD_BINDINGS_PERL=$Env:DEPLOY_PERL_BINDINGS
            if ($LastExitCode -ne 0) { throw "Perl bindings: cmake failed with exit code $LastExitCode" }
            cd ../
        }
    - ps: $env:CORE_VERSION = Get-Content -Path ./version.txt
    - ps: $env:UNDERSCORED_CORE_VERSION = $env:CORE_VERSION.Replace(".", "_")
    - ps: echo "Core version is $env:CORE_VERSION"
# TODO: use the core version to update appveyor version here
#    - ps: $env:BUILD_VERSION = "0.0.1." + $env:APPVEYOR_BUILD_NUMBER 
#    - ps: echo update build version to $env:BUILD_VERSION
#    - ps: Update-AppveyorBuild -Version $env:BUILD_VERSION


build_script:
    - cd build
    - msbuild /p:configuration=Release /v:m ALL_BUILD.vcxproj
    - cd ../
    - ps: |
        if ($Env:DEPLOY_PERL_BINDINGS -eq "1") {
            cd build-perl
            gmake
            if ($LastExitCode -ne 0) { throw "Perl bindings: gmake failed with exit code $LastExitCode" }
            cd ../
        }


after_build:
    - ps: $env:CORE_PACKAGE_NAME = 'sourcetraildb_core_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit-msvc' + $env:MSVC_VERSION
    - ps: $env:PYTHON_PACKAGE_NAME = 'sourcetraildb_python' + $env:PYTHON_VERSION + '_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit'
    - ps: $env:JAVA_PACKAGE_NAME = 'sourcetraildb_java_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit'
    - ps: $env:PERL_PACKAGE_NAME = 'sourcetraildb_strawberry-perl-' + $env:PERL_VERSION + '_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit'
    - ps: $env:CSHARP_PACKAGE_NAME = 'sourcetraildb_csharp_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit'
    - ps: echo $env:CORE_PACKAGE_NAME
    - ps: echo $env:PYTHON_PACKAGE_NAME
    - ps: echo $env:JAVA_PACKAGE_NAME
    - ps: echo $env:PERL_PACKAGE_NAME
    - ps: echo $env:CSHARP_PACKAGE_NAME
    
    - ps: $env:ARTIFACTS_TO_DEPLOY = ""
    
    - ps: |
        mkdir artifacts_core
        if ($env:DEPLOY_CORE -eq "1") {
            cd artifacts_core
            mkdir $env:CORE_PACKAGE_NAME
            cd $env:CORE_PACKAGE_NAME
            mkdir lib
            mkdir include
            mkdir license
            cd ../..
            
            copy build/core/release/sourcetraildb.lib artifacts_core/$env:CORE_PACKAGE_NAME/lib/
            copy build/core/include/version.h artifacts_core/$env:CORE_PACKAGE_NAME/include/
            copy core/include/* artifacts_core/$env:CORE_PACKAGE_NAME/include/
            copy LICENSE.txt artifacts_core/$env:CORE_PACKAGE_NAME/license/license_sourcetraildb.txt
            copy external/catch/license_catch.txt artifacts_core/$env:CORE_PACKAGE_NAME/license/
            copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_core/$env:CORE_PACKAGE_NAME/license/
            copy external/json/license_json.txt artifacts_core/$env:CORE_PACKAGE_NAME/license/
            
            $env:ARTIFACTS_TO_DEPLOY = $env:ARTIFACTS_TO_DEPLOY + "," + $env:CORE_PACKAGE_NAME
        }
    - ps: |
        mkdir artifacts_bindings_python
        if ($env:DEPLOY_PYTHON_BINDINGS -eq "1") {
            cd artifacts_bindings_python
            mkdir $env:PYTHON_PACKAGE_NAME
            cd $env:PYTHON_PACKAGE_NAME
            mkdir license
            cd ../..
        
            copy build/bindings_python/release/_sourcetraildb.pyd artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/
            copy build/bindings_python/sourcetraildb.py artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/
            copy LICENSE.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/license_sourcetraildb.txt
            copy external/catch/license_catch.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/
            copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/
            copy external/json/license_json.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/
            
            $env:ARTIFACTS_TO_DEPLOY = $env:ARTIFACTS_TO_DEPLOY + "," + $env:PYTHON_PACKAGE_NAME
        }
    - ps: |
        mkdir artifacts_bindings_java
        if ($env:DEPLOY_JAVA_BINDINGS -eq "1") {
            cd artifacts_bindings_java
            mkdir $env:JAVA_PACKAGE_NAME
            cd $env:JAVA_PACKAGE_NAME
            mkdir license
            cd ../..
        
            copy build/bindings_java/release/_sourcetraildb.dll artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/
            copy build/bindings_java/SourcetrailDB.jar artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/
            copy LICENSE.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/license_sourcetraildb.txt
            copy external/catch/license_catch.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/
            copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/
            copy external/json/license_json.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/
            
            $env:ARTIFACTS_TO_DEPLOY = $env:ARTIFACTS_TO_DEPLOY + "," + $env:JAVA_PACKAGE_NAME
        }
    - ps: |
        mkdir artifacts_bindings_perl
        if ($env:DEPLOY_PERL_BINDINGS -eq "1") {
            cd artifacts_bindings_perl
            mkdir $env:PERL_PACKAGE_NAME
            cd $env:PERL_PACKAGE_NAME
            mkdir license
            cd ../..

            copy build-perl/bindings_perl/sourcetraildb.dll artifacts_bindings_perl/$env:PERL_PACKAGE_NAME/
            copy build-perl/bindings_perl/sourcetraildb.pm artifacts_bindings_perl/$env:PERL_PACKAGE_NAME/
            copy LICENSE.txt artifacts_bindings_perl/$env:PERL_PACKAGE_NAME/license/license_sourcetraildb.txt
            copy external/catch/license_catch.txt artifacts_bindings_perl/$env:PERL_PACKAGE_NAME/license/
            copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_perl/$env:PERL_PACKAGE_NAME/license/
            copy external/json/license_json.txt artifacts_bindings_perl/$env:PERL_PACKAGE_NAME/license/

            $env:ARTIFACTS_TO_DEPLOY = $env:ARTIFACTS_TO_DEPLOY + "," + $env:PERL_PACKAGE_NAME
        }
    - ps: |
        mkdir artifacts_bindings_csharp
        if ($env:DEPLOY_CSHARP_BINDINGS -eq "1") {
            cd artifacts_bindings_csharp
            mkdir $env:CSHARP_PACKAGE_NAME
            cd $env:CSHARP_PACKAGE_NAME
            mkdir license
            cd ../..
        
            copy build/bindings_csharp/release/SourcetrailDB.dll artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/
            copy build/bindings_csharp/DefinitionKind.cs artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/
            copy build/bindings_csharp/ReferenceKind.cs artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/
            copy build/bindings_csharp/sourcetraildb.cs artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/
            copy build/bindings_csharp/sourcetraildbPINVOKE.cs artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/
            copy build/bindings_csharp/SymbolKind.cs artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/
            copy LICENSE.txt artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/license/license_sourcetraildb.txt
            copy external/catch/license_catch.txt artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/license/
            copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/license/
            copy external/json/license_json.txt artifacts_bindings_csharp/$env:CSHARP_PACKAGE_NAME/license/
            
            $env:ARTIFACTS_TO_DEPLOY = $env:ARTIFACTS_TO_DEPLOY + "," + $env:CSHARP_PACKAGE_NAME
        }
    - ps: |
        $env:ARTIFACTS_TO_DEPLOY = $env:ARTIFACTS_TO_DEPLOY.substring(1)
        echo $env:ARTIFACTS_TO_DEPLOY


test_script:
    - "build\\core\\release\\test_core.exe"


artifacts:
    - path: artifacts_core
      name: $(CORE_PACKAGE_NAME)
      type: Zip
    - path: artifacts_bindings_python
      name: $(PYTHON_PACKAGE_NAME)
      type: Zip
    - path: artifacts_bindings_java
      name: $(JAVA_PACKAGE_NAME)
      type: Zip
    - path: artifacts_bindings_perl
      name: $(PERL_PACKAGE_NAME)
      type: Zip
    - path: artifacts_bindings_csharp
      name: $(CSHARP_PACKAGE_NAME)
      type: Zip


deploy:
    provider: GitHub
    artifact: $(ARTIFACTS_TO_DEPLOY)
    auth_token:
        secure: 8UwbjFrtR19GxLgL3O3efUT0Su2mp3+X23YioThJITupEvexmQJPAf3Whmmb3eeL
    on:
        appveyor_repo_tag: true
