variables:
  Prerelease: 'ci'
  buildId: "1$(Build.BuildId)"
  buildProjects: '**/src/**/*.csproj'

jobs:
- job: Linux_Build_Test_SonarCloud

  pool:
    vmImage: 'Ubuntu-latest'

  steps:
  - script: |
      echo "BuildId = $(buildId)"
    displayName: 'Print buildId'

  - task: UseDotNet@2
    displayName: Use .NET 7.0
    inputs:
      packageType: 'sdk'
      version: '7.0.x'    

  - task: PowerShell@2
    displayName: "Use JDK11 by default"
    inputs:
      targetType: 'inline'
      script: |
        $jdkPath = $env:JAVA_HOME_11_X64
        Write-Host "##vso[task.setvariable variable=JAVA_HOME]$jdkPath"

  - script: |
      dotnet dev-certs https --trust || true
    displayName: 'dotnet dev-certs https'
  
  - task: SonarCloudPrepare@1
    displayName: 'Prepare analysis on SonarCloud'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests
    inputs:
      SonarCloud: SonarCloud
      organization: wiremock-net
      projectKey: 'WireMock-Net_WireMock.Net'
      projectName: 'WireMock.Net'
      extraProperties: |
        sonar.cs.opencover.reportsPaths=**/coverage.net7.0.opencover.xml
  
  - task: DotNetCoreCLI@2
    displayName: 'Build Unit tests'
    inputs:
      command: 'build'
      projects: './test/WireMock.Net.Tests/WireMock.Net.Tests.csproj'
      arguments: '--configuration Debug --framework net7.0'

  - task: CmdLine@2
    inputs:
      script: 'dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --no-build --configuration Debug --framework net7.0'
    displayName: 'Execute Unit Tests with Coverage'
   
  - task: SonarCloudAnalyze@1
    displayName: 'SonarCloud: Run Code Analysis'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests
  
  - task: SonarCloudPublish@1
    displayName: 'SonarCloud: Publish Quality Gate Result'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests
  
  - task: whitesource.ws-bolt.bolt.wss.WhiteSource Bolt@19
    displayName: 'WhiteSource Bolt'
    condition: and(succeeded(), eq(variables['RUN_WHITESOURCE'], 'yes'))
 
  - script: |
      bash <(curl https://codecov.io/bash) -t $(CODECOV_TOKEN) -f ./test/WireMock.Net.Tests/coverage.7.0.opencover.xml
    displayName: 'codecov'
  
  - task: PublishTestResults@2
    condition: and(succeeded(), eq(variables['PUBLISH_TESTRESULTS'], 'yes'))
    inputs:
      testRunner: VSTest
      testResultsFiles: '**/*.trx'
  
  - task: PublishBuildArtifacts@1
    displayName: Publish coverage file
    inputs:
      PathtoPublish: './test/WireMock.Net.Tests/coverage.net7.0.opencover.xml'

- job: Windows_Build_Test

  pool:
    vmImage: 'windows-2022'

  steps:
  - task: UseDotNet@2
    displayName: Use .NET 7.0
    inputs:
      packageType: 'sdk'
      version: '7.0.x'

  - task: DotNetCoreCLI@2
    displayName: 'Build Unit tests'
    inputs:
      command: 'build'
      projects: './test/WireMock.Net.Tests/WireMock.Net.Tests.csproj'
      arguments: '--configuration Debug --framework net7.0'
  
  - task: DotNetCoreCLI@2
    displayName: 'Execute Unit Tests with Coverage'
    inputs:
      command: 'test'
      projects: './test/WireMock.Net.Tests/WireMock.Net.Tests.csproj'
      arguments: '--no-build --configuration Debug --framework net7.0 --collect:"XPlat Code Coverage" --logger trx'
  
- job: Windows_Release_to_MyGet
  dependsOn: Windows_Build_Test

  pool:
    vmImage: 'windows-2022'

  steps:
  - task: UseDotNet@2
    displayName: Use .NET 7.0
    inputs:
      packageType: 'sdk'
      version: '7.0.x'

  - task: DotNetCoreCLI@2
    displayName: Build Release
    inputs:
      command: 'build'
      arguments: /p:Configuration=Release
      projects: $(buildProjects)

  - task: DotNetCoreCLI@2
    displayName: Pack
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests
    inputs:
      command: pack
      configuration: 'Release'
      packagesToPack: $(buildProjects)
      nobuild: true
      packDirectory: '$(Build.ArtifactStagingDirectory)/packages'
      verbosityPack: 'normal'
  
  - task: PublishBuildArtifacts@1
    displayName: Publish Artifacts
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
  
  - task: DotNetCoreCLI@2
    displayName: Push to MyGet
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests
    inputs:
      command: custom
      custom: nuget
      arguments: push $(Build.ArtifactStagingDirectory)\packages\*.nupkg -n -s https://www.myget.org/F/wiremock-net/api/v3/index.json -k $(MyGetKey)