# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
#
# vim:set expandtab shiftwidth=2 tabstop=2:
#
# Variables:
#   SpeechSdkVersion - Speech SDK version to use
#   SpeechSdkNuGetSource - where to pick up the Speech SDK from ("nuget.org" or "VstsFeed")
#   SpeechSdkVstsFeed - VSTS feed to use if SpeechSdkNuGetSource == "VstsFeed"
#

trigger:
- master

pr:
- master

resources:
- repo: self
  clean: true

variables:
  SPEECHSDK_NUGET_VERSION: 6.2.1
  CG_RAN: true # we do Component Governance explicitly only in the Pre job

jobs:

- job: Pre
  variables:
    CG_RAN: false
    DECODE_PERCENTS: true
  pool:
    vmImage: 'ubuntu-22.04'
  timeoutInMinutes: 10
  steps:
  - bash: ./ci/check-git-head.sh
    displayName: Repository checks
  - template: ./lint-python.yml
  - task: ms.vss-governance-buildtask.governance-build-task-component-detection.ComponentGovernanceComponentDetection@0
    displayName: Component Detection
    condition: >
      and(eq(variables['System.CollectionId'], '26f8e8b1-373f-4f65-96fc-d17a59b38306'),
        eq(variables['System.DefinitionId'], '5400'),
        eq(variables['Build.SourceBranch'], 'refs/heads/master'),
        or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.Reason'], 'Manual')))
  - bash: |
      . ci/functions.sh || exit 1
      set -u -e -o pipefail
      case "$(SpeechSdkNuGetSource)" in
        nuget.org)
          V_VSTS_FEED=
          V_INCLUDE_NUGET_ORG=true
          ;;
        VstsFeed)
          V_VSTS_FEED="$(SpeechSdkVstsFeed)"
          V_INCLUDE_NUGET_ORG=true
          ;;
        *)
          echo Unexpected value SpeechSdkNuGetSource=$(SpeechSdkNuGetSource)
          exit 1
          ;;
      esac
      vsts_setoutvar V_VSTS_FEED "$V_VSTS_FEED"
      vsts_setoutvar V_INCLUDE_NUGET_ORG "$V_INCLUDE_NUGET_ORG"
    name: var
    displayName: Set variables

- job: Windows2022SDK
  variables:
    V_VSTS_FEED: $[dependencies.Pre.outputs['var.V_VSTS_FEED']]
    V_INCLUDE_NUGET_ORG: $[dependencies.Pre.outputs['var.V_INCLUDE_NUGET_ORG']]
    DECODE_PERCENTS: true
  dependsOn: Pre
  timeoutInMinutes: 60
  pool:
    name: CarbonWS2022Compliant
  steps:
  - bash: 'perl ci/patch-samples.pl "$(SpeechSdkVersion)" . && git diff'
    displayName: Patch samples
  # - bash: 'dotnet workload install maui-android'
  #   displayName: install maui-android workload.
  - task: NuGetToolInstaller@0
    displayName: Use NuGet $(SPEECHSDK_NUGET_VERSION)
    inputs:
      versionSpec: $(SPEECHSDK_NUGET_VERSION)
  - task: NuGetCommand@2
    inputs:
      command: restore
      vstsFeed: $(V_VSTS_FEED)
      includeNuGetOrg: $(V_INCLUDE_NUGET_ORG)
      restoreSolution: '**\*.sln;!**\embedded-speech\*.sln' # Restoring everything but embedded samples
    displayName: 'Restore NuGet packages'
  - task: JavaToolInstaller@0
    inputs:
      versionSpec: '17'
      jdkArchitectureOption: 'x64'
      jdkSourceOption: 'PreInstalled'
    displayName: Use Java 17
  - bash: |
      set -u -e -o pipefail
      curl -v -L https://aka.ms/intent-recognition-samples-data  --output samples/cpp/intent-recognition/samples/test_input/audio.zip
      unzip -o samples/cpp/intent-recognition/samples/test_input/audio.zip -d samples/cpp/intent-recognition/samples/test_input
    displayName: Install intent recognition samples data
  # Note: for now building any platform. In the future, should build all available.
  - task: VSBuild@1
    inputs:
      maximumCpuCount: true
      solution: '**\*.sln;-:**\embedded-speech\*.sln' # Build everything but embedded samples
    displayName: 'Build samples (*.sln)'
    # Must set this to false, otherwise a build break will be treated as warning and the task will pass.
    # This however also means that we will stop building when we hit a build break. We will not continue building other VS projects.
    continueOnError: false
  - bash: ./ci/run-gradle.sh
    displayName: 'Build samples (gradle)'
