variables:
  PYTHON_VERSION: 3.7
  CONDA_ENV: test-env
phases:
###########################################
- phase: Linux
###########################################
  variables:
    COMPILER: gcc
  queue:
    name: 'Hosted Linux Preview'
    parallel: 6
    matrix:
      regular:
        TASK: regular
      sdist:
        TASK: sdist
        PYTHON_VERSION: 3.5
      bdist:
        TASK: bdist
        PYTHON_VERSION: 3.6
      inference:
        TASK: if-else
      mpi_source:
        TASK: mpi
        METHOD: source
        PYTHON_VERSION: 2.7
      gpu_source:
        TASK: gpu
        METHOD: source
        PYTHON_VERSION: 3.6
  steps:
  - task: CondaEnvironment@0
    inputs:
      updateConda: true
      environmentName: $(CONDA_ENV)
      packageSpecs: 'python=$(PYTHON_VERSION)'
      createOptions: '-q'
  - script: |
      echo "##vso[task.setvariable variable=HOME_DIRECTORY]$AGENT_HOMEDIRECTORY"
      echo "##vso[task.setvariable variable=BUILD_DIRECTORY]$BUILD_SOURCESDIRECTORY"
      echo "##vso[task.setvariable variable=OS_NAME]linux"
      echo "##vso[task.setvariable variable=AZURE]true"
      echo "##vso[task.setvariable variable=LGB_VER]$(head -n 1 VERSION.txt)"
      AMDAPPSDK_PATH=$AGENT_HOMEDIRECTORY/AMDAPPSDK
      echo "##vso[task.setvariable variable=AMDAPPSDK_PATH]$AMDAPPSDK_PATH"
      LD_LIBRARY_PATH=$AMDAPPSDK_PATH/lib/x86_64:$LD_LIBRARY_PATH
      echo "##vso[task.setvariable variable=LD_LIBRARY_PATH]$LD_LIBRARY_PATH"
      echo "##vso[task.setvariable variable=OPENCL_VENDOR_PATH]$AMDAPPSDK_PATH/etc/OpenCL/vendors"
    displayName: 'Set variables'
  - bash: $(Build.SourcesDirectory)/.ci/setup.sh
    displayName: Setup
  - bash: $(Build.SourcesDirectory)/.ci/test.sh
    displayName: Test
  - task: PublishBuildArtifacts@1
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: PackageAssets
      artifactType: container
###########################################
- phase: MacOS
###########################################
  variables:
    COMPILER: gcc
  queue:
    name: 'Hosted macOS Preview'
    parallel: 3
    matrix:
      regular:
        TASK: regular
        PYTHON_VERSION: 3.6
      sdist:
        TASK: sdist
        PYTHON_VERSION: 3.5
      bdist:
        TASK: bdist
  steps:
  - script: |
      sudo chmod -R 777 /usr/local/miniconda/envs
    displayName: 'Fix for conda error on macOS'
  - task: CondaEnvironment@0
    inputs:
      updateConda: true
      environmentName: $(CONDA_ENV)
      packageSpecs: 'python=$(PYTHON_VERSION)'
      createOptions: '-q'
  - script: |
      echo "##vso[task.setvariable variable=HOME_DIRECTORY]$AGENT_HOMEDIRECTORY"
      echo "##vso[task.setvariable variable=BUILD_DIRECTORY]$BUILD_SOURCESDIRECTORY"
      echo "##vso[task.setvariable variable=OS_NAME]macos"
      echo "##vso[task.setvariable variable=AZURE]true"
      echo "##vso[task.setvariable variable=LGB_VER]$(head -n 1 VERSION.txt)"
    displayName: 'Set variables'
  - bash: $(Build.SourcesDirectory)/.ci/setup.sh
    displayName: Setup
  - bash: $(Build.SourcesDirectory)/.ci/test.sh
    displayName: Test
  - task: PublishBuildArtifacts@1
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
      artifactName: PackageAssets
      artifactType: container
###########################################
- phase: Windows
###########################################
  queue:
    name: 'Hosted VS2017'
    parallel: 3
    matrix:
      regular:
        TASK: regular
        PYTHON_VERSION: 3.5
      sdist:
        TASK: sdist
        PYTHON_VERSION: 2.7
      bdist:
        TASK: bdist
        PYTHON_VERSION: 3.6
  steps:
  - task: CondaEnvironment@0
    inputs:
      updateConda: true
      environmentName: $(CONDA_ENV)
      packageSpecs: 'python=$(PYTHON_VERSION) numpy nose scipy scikit-learn pandas matplotlib python-graphviz pytest'
      createOptions: '-q'
  - powershell: $(Build.SourcesDirectory)/.ci/test_windows.ps1
    displayName: Test
  - task: PublishBuildArtifacts@1
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: PackageAssets
      artifactType: container

###########################################
- phase: Package
###########################################
  dependsOn:
  - Linux
  - MacOS
  - Windows
  condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
  queue:
    name: 'Hosted VS2017'
  steps:
  # Download all agent packages from all previous phases
  - task: DownloadBuildArtifacts@0
    displayName: Download package assets
    inputs:
      artifactName: PackageAssets
      downloadPath: $(Build.SourcesDirectory)/binaries
  - powershell: |
      $client = new-object System.Net.WebClient
      $client.DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", "$(Build.SourcesDirectory)/.nuget/nuget.exe")
    displayName: 'Download NuGet application'
  - script: |
      cd %BUILD_SOURCESDIRECTORY%/.nuget
      python create_nuget.py %BUILD_SOURCESDIRECTORY%/binaries/PackageAssets
      nuget.exe pack LightGBM.nuspec
      xcopy *.nupkg %BUILD_ARTIFACTSTAGINGDIRECTORY%
    displayName: 'Build NuGet package'
  - task: PublishBuildArtifacts@1
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: Nuget
      artifactType: container
