# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: 'vNext$(rev:.r)' # Format for build number (will be overridden)

# DevOps Setup: Define the following pipeline level variables in Azure DevOps build pipeline

# ArtifactFeedID: (Optional - set to your Azure DevOps Artifact (NuGet) feed. If not provided, publish job will be skipped.)
# BuildConfiguration: (Optional. Defaults to 'Release')
# BuildPlatform: (Optional. Defaults to 'Any CPU')
# GenerateDocs: (Optional. Only builds documentation website if set to 'true'.)
# GenerateWebsite: (Optional. Only builds lucene.net website if set to 'true'.)
# IsRelease: (Optional. By default the Release job is disabled, setting this to 'true' will enable it)
# GeneratePackages: (Optional. Defaults to 'true'. Setting to 'false' will cut about 1 minute from the build time by skipping packing the NuGet files and subsequent artiact upload. If ArtifactFeedID is provided or IsRelease is 'true', this setting has no effect.)

# Versioning Variables

# BuildCounterSeed: (Optional - Set in conjunction with VersionSuffix, will cause the build counter to begin at this value. Note that it is set once, to reset is an API call.)
# PackageVersion: (Optional - This can be used to explicitly set the whole version number to a specific version, i.e. 4.8.0-beta00005. It overrides all other version settings.)
# PreReleaseCounterPattern: (Optional. Set to '0000000000' in ci pipeline or '00000' in release pipeline. The default is '0000000000'. This setting has no effect if VersionSuffix is ''.)
# VersionSuffix: (Optional. Defaults to 'ci'. Set to 'beta' or 'rc' or '' in production pipeline.)

# Testing variables

# RunTests: 'true' (Optional - set to 'false' to disable test jobs - useful for debugging. If not provided, tests will be run.)
# AssertsEnabled: 'true' (Optional - set to 'false' to run tests without asserts, which is less thorough. This can speed up testing and verify the application will run without asserts.)
# IsNightly: 'false' (Optional - set to 'true' to run additional tests for the nightly build)
# IsWeekly: 'false' (Optional - set to 'true' to run additional tests for the weekly build)
# RunSlowTests: 'true' (Optional - set to 'false' to skip slow tests to make testing time shorter)
# RunAwaitsFixTests: 'false' (Optional - set to 'true' to run flakey tests)
# Codec: 'random' (Optional - set to a specific codec to test the same codec throughout all tests)
# DocValuesFormat: 'random' (Optional - set to a specific doc values format to test the same codec throughout all tests)
# PostingsFormat: 'random' (Optional - set to a specific postings format to test the same codec throughout all tests)
# Directory: 'random' (Optional - set to a specific directory implementation to test the same codec throughout all tests)
# Verbose: 'false' (Optional - set to true for verbose logging output)
# Multiplier: '1' (Optional - the number of iterations to multiply applicable tests by)

# RunX86Tests: 'false' (Optional - set to 'true' to enable x86 tests)

variables:
- name: BuildCounter
  value: $[counter(variables['VersionSuffix'],coalesce(variables['BuildCounterSeed'], 1250))]
- name: DocumentationArtifactName
  value: 'docs'
- name: DocumentationArtifactZipFileName
  value: 'documentation.zip'
- name: WebsiteArtifactName
  value: 'website'
- name: WebsiteArtifactZipFileName
  value: 'website.zip'  
- name: BinaryArtifactName
  value: 'testbinaries'
- name: NuGetArtifactName
  value: 'nuget'
- name: DebugArtifactName # For .pdb symbols
  value: 'debug'
- name: ReleaseArtifactName
  value: 'release'
- name: TestResultsArtifactName
  value: 'testresults'
- name: VersionArtifactName
  value: 'version'
- name: BuildNumberFileName
  value: 'buildNumber.txt'
- name: PackageVersionFileName
  value: 'packageVersion.txt'
- name: FileVersionFileName
  value: 'fileVersion.txt'
- name: TestSettingsFileName
  value: 'lucene.testsettings.json'
- name: BuildDirectory # Where the build scripts and configs are
  value: '$(System.DefaultWorkingDirectory)/build'
- name: PublishDirectory # Test binaries directory
  value: '$(Build.ArtifactStagingDirectory)/$(BinaryArtifactName)'
- name: NuGetArtifactDirectory # NuGet binaries directory
  value: '$(Build.ArtifactStagingDirectory)/$(NuGetArtifactName)'


stages:
- stage: Build_Stage
  displayName: 'Build Stage:'
  jobs:

  - job: Build
    pool:
      vmImage: 'windows-2019'

    variables:
      PublishTempDirectory: '$(Build.ArtifactStagingDirectory)/publish'

    steps:
    
    - checkout: self # self represents the repo where the initial Pipelines YAML file was found
      fetchDepth: '1'  # the depth of commits to ask Git to fetch

    - pwsh: |
        $configuration = if ($env:BUILDCONFIGURATION) { $env:BUILDCONFIGURATION } else { "Release" }
        Write-Host "##vso[task.setvariable variable=BuildConfiguration;]$configuration"
        $isRelease = if ($env:ISRELEASE -eq 'true') { 'true' } else { 'false' }
        Write-Host "##vso[task.setvariable variable=IsRelease;]$isRelease"
        $isNightly = if ($env:ISNIGHTLY -eq 'true') { 'true' } else { 'false' }
        Write-Host "##vso[task.setvariable variable=IsNightly;]$isNightly"
        $isWeekly = if ($env:ISWEEKLY -eq 'true') { 'true' } else { 'false' }
        Write-Host "##vso[task.setvariable variable=IsWeekly;]$isWeekly"
        $runPack = if ($env:ISRELEASE -eq 'true' -or $env:ARTIFACTFEEDID -ne '' -or $env:GENERATEPACKAGES -ne 'false') { 'true' } else { 'false' }
        Write-Host "##vso[task.setvariable variable=RunPack;]$runPack"
      displayName: 'Setup Default Variable Values'

    - task: UseDotNet@2
      displayName: 'Use .NET Core sdk 5.0.100'
      inputs:
        version: 5.0.100

    - pwsh: |
        Import-Module "$(BuildDirectory)/psake.psm1"
        $generateBuildBat = $env:ISRELEASE
        $primaryCommand = if ($env:RUNPACK -ne 'false') { 'Pack' } else { 'Compile' }
        $parameters = @{}
        $properties = @{
            backup_files='false';
            publish_directory='$(PublishTempDirectory)';
            nuget_package_directory='$(NuGetArtifactDirectory)';
            # Lock the build.bat so it only builds this version in the release distribution
            generateBuildBat=$generateBuildBat;
            skipSdkInstallation='true'
        }
        [string[]]$tasks = @($primaryCommand)
        if ($env:RunTests -ne 'false') {
            [string[]]$tasks = $primaryCommand,'Publish'
        }
        Invoke-psake $(BuildDirectory)/build.ps1 -Task $tasks -properties $properties -parameters $parameters
        exit !($psake.build_success)
      displayName: 'PSake Build, Pack, and Publish'

    #- template: 'build/azure-templates/show-all-environment-variables.yml' # Uncomment for debugging
        
    - pwsh: |
        $dir = '$(Build.ArtifactStagingDirectory)/$(VersionArtifactName)'
        if (!(Test-Path $dir)) { New-Item -ItemType Directory -Path "$dir" -Force }
        '$(PackageVersion)' | Out-File -FilePath "$dir/$(PackageVersionFileName)" -Force
        '$(FileVersion)' | Out-File -FilePath "$dir/$(FileVersionFileName)" -Force
        '$(Build.BuildNumber)' | Out-File -FilePath "$dir/$(BuildNumberFileName)" -Force
      displayName: 'Persist Versions to Files'

    - pwsh: |
        # Generate a lucene.testsettings.json file for use with the test framework
        $assert = if ($env:AssertsEnabled -ne 'false') { 'true' } else { 'false' }
        $nightly = if ($env:IsNightly -eq 'true') { 'true' } else { 'false' }
        $weekly = if ($env:IsWeekly -eq 'true') { 'true' } else { 'false' }
        $slow = if ($env:RunSlowTests -ne 'false') { 'true' } else { 'false' }
        $awaitsFix = if ($env:RunAwaitsFixTests -ne 'false') { 'true' } else { 'false' }
        $codec = if ($env:Codec -eq $null) { 'random' } else { $env:Codec }
        $docValuesFormat = if ($env:DocValuesFormat -eq $null) { 'random' } else { $env:DocValuesFormat }
        $postingsFormat = if ($env:PostingsFormat -eq $null) { 'random' } else { $env:PostingsFormat }
        $directory = if ($env:Directory -eq $null) { 'random' } else { $env:Directory }
        $verbose = if ($env:Verbose -eq 'true') { 'true' } else { 'false' }
        $multiplier = if ($env:Multiplier -eq $null) { '1' } else { $env:Multiplier }
        $fileText = "{`n`t" +
            """assert"": ""$assert"",`n`t" +
            """tests"": {`n`t`t" +
                """nightly"": ""$nightly"",`n`t`t" +
                """weekly"": ""$weekly"",`n`t`t" +
                """slow"": ""$slow"",`n`t`t" +
                """awaitsfix"": ""$awaitsFix"",`n`t`t" +
                """codec"": ""$codec"",`n`t`t" +
                """docvaluesformat"": ""$docValuesFormat"",`n`t`t" +
                """postingsformat"": ""$postingsFormat"",`n`t`t" +
                """directory"": ""$directory"",`n`t`t" +
                """verbose"": ""$verbose"",`n`t`t" +
                """multiplier"": ""$multiplier""`n`t" +
            "}`n" +
        "}"
        Out-File -filePath "$(Build.ArtifactStagingDirectory)/$(TestSettingsFileName)" -encoding UTF8 -inputObject $fileText
      displayName: 'Persist Test Settings to lucene.testsettings.json'
      condition: and(succeeded(), ne(variables['RunTests'], 'false'))

    # If this is a release pipeline, copy the build.bat and Version.props files as version artifacts, which will 
    # overwrite the build.bat and Version.props files of the release.
    - task: CopyFiles@2
      displayName: 'Copy build.bat and Version.props Files to: /$(VersionArtifactName)'
      inputs:
        SourceFolder: '$(System.DefaultWorkingDirectory)'
        Contents: |
          build.bat
          Version.props
        TargetFolder: '$(Build.ArtifactStagingDirectory)/$(VersionArtifactName)'
      condition: and(succeeded(), eq(variables['IsRelease'], 'true'))
            
    - task: PublishPipelineArtifact@1
      displayName: 'Publish Artifact: $(VersionArtifactName)'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/$(VersionArtifactName)'
        artifact: '$(VersionArtifactName)'
        publishLocation: 'pipeline'
    
    # Copy the .pdb files as build artifacts, which will 
    # later be used to push to the Azure Artifacts symbol server.
    - task: CopyFiles@2
      displayName: 'Copy .pdb Files to: /$(DebugArtifactName)'
      inputs:
        SourceFolder: '$(System.DefaultWorkingDirectory)'
        Contents: '**/bin/$(BuildConfiguration)/**/*.pdb'
        TargetFolder: '$(Build.ArtifactStagingDirectory)/$(DebugArtifactName)'
      condition: and(succeeded(), ne(variables['ArtifactFeedID'], ''))

    - task: PublishPipelineArtifact@1
      displayName: 'Publish Artifact: $(NuGetArtifactName)'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/$(NuGetArtifactName)'
        artifact: '$(NuGetArtifactName)'
        publishLocation: 'pipeline'
      condition: and(succeeded(), ne(variables['RunPack'], 'false'))
    
    - task: PublishPipelineArtifact@1
      displayName: 'Publish Artifact: $(DebugArtifactName)'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/$(DebugArtifactName)'
        artifact: '$(DebugArtifactName)'
        publishLocation: 'pipeline'
      condition: and(succeeded(), ne(variables['ArtifactFeedID'], ''))

    - template: 'build/azure-templates/publish-test-binaries.yml'
      parameters:
        publishDirectory: $(PublishTempDirectory)
        framework: 'net5.0'
        binaryArtifactName: '$(BinaryArtifactName)'
        testSettingsFilePath: '$(Build.ArtifactStagingDirectory)/$(TestSettingsFileName)'

    - template: 'build/azure-templates/publish-test-binaries.yml'
      parameters:
        publishDirectory: $(PublishTempDirectory)
        framework: 'netcoreapp3.1'
        binaryArtifactName: '$(BinaryArtifactName)'
        testSettingsFilePath: '$(Build.ArtifactStagingDirectory)/$(TestSettingsFileName)'

    - template: 'build/azure-templates/publish-test-binaries.yml'
      parameters:
        publishDirectory: $(PublishTempDirectory)
        framework: 'netcoreapp2.1'
        binaryArtifactName: '$(BinaryArtifactName)'
        testSettingsFilePath: '$(Build.ArtifactStagingDirectory)/$(TestSettingsFileName)'

    - template: 'build/azure-templates/publish-test-binaries.yml'
      parameters:
        publishDirectory: $(PublishTempDirectory)
        framework: 'net48'
        binaryArtifactName: '$(BinaryArtifactName)'
        testSettingsFilePath: '$(Build.ArtifactStagingDirectory)/$(TestSettingsFileName)'
        
  - job: Docs
    condition: and(succeeded(), eq(variables['GenerateDocs'], 'true'))
    pool:
      vmImage: 'vs2017-win2016'

    steps:
    - pwsh: |
         $(Build.SourcesDirectory)/websites/apidocs/docs.ps1 -LuceneNetVersion $(PackageVersion) -Clean -BaseUrl https://lucenenet.apache.org/docs/
      errorActionPreference: 'continue'
      ignoreLASTEXITCODE: 'true'
      failOnStderr: 'false'
      displayName: 'Generate Documentation'

    - task: ArchiveFiles@2
      displayName: 'Zip Documenation Files'
      inputs:
        rootFolderOrFile: '$(Build.SourcesDirectory)/websites/apidocs/_site'
        includeRootFolder: false
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(DocumentationArtifactName)/$(DocumentationArtifactZipFileName)'

    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: $(DocumentationArtifactName)'
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(DocumentationArtifactName)'
        ArtifactName: '$(DocumentationArtifactName)'

  - job: Website
    condition: and(succeeded(), eq(variables['GenerateWebsite'], 'true'))
    pool:
      vmImage: 'vs2017-win2016'

    steps:
    - pwsh: |
         $(Build.SourcesDirectory)/websites/site/site.ps1 0 1
      errorActionPreference: 'continue'
      ignoreLASTEXITCODE: 'true'
      failOnStderr: 'false'
      displayName: 'Generate Website'

    - task: ArchiveFiles@2
      displayName: 'Zip Website Files'
      inputs:
        rootFolderOrFile: '$(Build.SourcesDirectory)/websites/site/_site'
        includeRootFolder: false
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(WebsiteArtifactName)/$(WebsiteArtifactZipFileName)'

    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: $(WebsiteArtifactName)'
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(WebsiteArtifactName)'
        ArtifactName: '$(WebsiteArtifactName)'


- stage: Test_Stage
  displayName: 'Test Stage:'
  jobs:

  - job: Test_net5_0_x64
    condition: and(succeeded(), ne(variables['RunTests'], 'false'))
    strategy:
      matrix:
        Windows:
          osName: 'Windows'
          imageName: 'windows-2019'
          maximumParallelJobs: 8
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        Linux:
          osName: 'Linux'
          imageName: 'ubuntu-18.04'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        macOS:
          osName: 'macOS'
          imageName: 'macOS-10.15'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
    displayName: 'Test net5.0,x64 on'
    pool:
      vmImage: $(imageName)
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: $(osName)
        framework: 'net5.0'
        vsTestPlatform: 'x64'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: $(maximumParallelJobs)
        maximumAllowedFailures: $(maximumAllowedFailures)

  - job: Test_net5_0_x86 # Only run Nightly or if explicitly enabled with RunX86Tests
    condition: and(succeeded(), ne(variables['RunTests'], 'false'), or(eq(variables['IsNightly'], 'true'), eq(variables['RunX86Tests'], 'true')))
    strategy:
      matrix:
        Windows:
          osName: 'Windows'
          imageName: 'windows-2019'
          maximumParallelJobs: 8
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        Linux:
          osName: 'Linux'
          imageName: 'ubuntu-18.04'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        macOS:
          osName: 'macOS'
          imageName: 'macOS-10.15'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
    displayName: 'Test net5.0,x86 on'
    pool:
      vmImage: $(imageName)
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: $(osName)
        framework: 'net5.0'
        vsTestPlatform: 'x86'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: $(maximumParallelJobs)
        maximumAllowedFailures: $(maximumAllowedFailures)

  - job: Test_netcoreapp3_1_x64
    condition: and(succeeded(), ne(variables['RunTests'], 'false'))
    strategy:
      matrix:
        Windows:
          osName: 'Windows'
          imageName: 'windows-2019'
          maximumParallelJobs: 8
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        Linux:
          osName: 'Linux'
          imageName: 'ubuntu-18.04'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        macOS:
          osName: 'macOS'
          imageName: 'macOS-10.15'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
    displayName: 'Test netcoreapp3.1,x64 on'
    pool:
      vmImage: $(imageName)
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: $(osName)
        framework: 'netcoreapp3.1'
        vsTestPlatform: 'x64'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: $(maximumParallelJobs)
        maximumAllowedFailures: $(maximumAllowedFailures)

  - job: Test_netcoreapp3_1_x86 # Only run Nightly or if explicitly enabled with RunX86Tests
    condition: and(succeeded(), ne(variables['RunTests'], 'false'), or(eq(variables['IsNightly'], 'true'), eq(variables['RunX86Tests'], 'true')))
    strategy:
      matrix:
        Windows:
          osName: 'Windows'
          imageName: 'windows-2019'
          maximumParallelJobs: 8
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        Linux:
          osName: 'Linux'
          imageName: 'ubuntu-18.04'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        macOS:
          osName: 'macOS'
          imageName: 'macOS-10.15'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
    displayName: 'Test netcoreapp3.1,x86 on'
    pool:
      vmImage: $(imageName)
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: $(osName)
        framework: 'netcoreapp3.1'
        vsTestPlatform: 'x86'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: $(maximumParallelJobs)
        maximumAllowedFailures: $(maximumAllowedFailures)

  - job: Test_netcoreapp2_2_x64
    condition: and(succeeded(), ne(variables['RunTests'], 'false'))
    strategy:
      matrix:
        Windows:
          osName: 'Windows'
          imageName: 'windows-2019'
          maximumParallelJobs: 8
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        Linux:
          osName: 'Linux'
          imageName: 'ubuntu-18.04'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        macOS:
          osName: 'macOS'
          imageName: 'macOS-10.15'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
    displayName: 'Test netcoreapp2.1,x64 on'
    pool:
      vmImage: $(imageName)
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: $(osName)
        framework: 'netcoreapp2.1'
        vsTestPlatform: 'x64'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: $(maximumParallelJobs)
        maximumAllowedFailures: $(maximumAllowedFailures)

  - job: Test_netcoreapp2_2_x86 # Only run Nightly or if explicitly enabled with RunX86Tests
    condition: and(succeeded(), ne(variables['RunTests'], 'false'), or(eq(variables['IsNightly'], 'true'), eq(variables['RunX86Tests'], 'true')))
    strategy:
      matrix:
        Windows:
          osName: 'Windows'
          imageName: 'windows-2019'
          maximumParallelJobs: 8
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        Linux:
          osName: 'Linux'
          imageName: 'ubuntu-18.04'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
        macOS:
          osName: 'macOS'
          imageName: 'macOS-10.15'
          maximumParallelJobs: 7
          maximumAllowedFailures: 4 # Maximum allowed failures for a successful build
    displayName: 'Test netcoreapp2.1,x86 on'
    pool:
      vmImage: $(imageName)
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: $(osName)
        framework: 'netcoreapp2.1'
        vsTestPlatform: 'x86'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: $(maximumParallelJobs)
        maximumAllowedFailures: $(maximumAllowedFailures)

  - job: Test_net48_Windows_x64
    condition: and(succeeded(), ne(variables['RunTests'], 'false'))
    displayName: 'Test net48,x64 on Windows'
    pool:
      vmImage: 'windows-2019'
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: 'Windows'
        framework: 'net48'
        vsTestPlatform: 'x64'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: 8
        maximumAllowedFailures: 2 # Maximum allowed failures for a successful build

  - job: Test_net48_Windows_x86 # Only run Nightly or if explicitly enabled with RunX86Tests
    condition: and(succeeded(), ne(variables['RunTests'], 'false'), or(eq(variables['IsNightly'], 'true'), eq(variables['RunX86Tests'], 'true')))
    displayName: 'Test net48,x86 on Windows'
    pool:
      vmImage: 'windows-2019'
    steps:
    - template: 'build/azure-templates/run-tests-on-os.yml'
      parameters:
        osName: 'Windows'
        framework: 'net48'
        vsTestPlatform: 'x86'
        testResultsArtifactName: '$(TestResultsArtifactName)'
        maximumParallelJobs: 8
        maximumAllowedFailures: 5 # Maximum allowed failures for a successful build


- stage: Publish_Stage
  displayName: 'Publish Stage:'
  jobs:

  # Optional job to push to Azure Artifact feed. Just pass in
  # the GUID of the artifact feed as ArtifactFeedID to enable.
  - job: Publish_Azure_Artifacts
    condition: and(succeeded(), ne(variables['ArtifactFeedID'], ''))
    pool:
      vmImage: 'windows-2019'

    steps:
      # We checkout here because we need to publish the source code along with the symbols for debugging
    - checkout: self # self represents the repo where the initial Pipelines YAML file was found
      fetchDepth: '1'  # the depth of commits to ask Git to fetch

    - template: 'build/azure-templates/show-all-environment-variables.yml'

    - task: DownloadPipelineArtifact@0
      displayName: 'Download Build Artifacts: $(VersionArtifactName)'
      inputs:
        artifactName: '$(VersionArtifactName)'
        targetPath: '$(System.DefaultWorkingDirectory)/$(VersionArtifactName)'

      # For debugging this pipeline
    #- pwsh: | 
    #    Get-ChildItem -Path $(System.DefaultWorkingDirectory)
    #    Get-ChildItem -Path '$(VersionArtifactName)'

      # NOTE: We are setting Build.BuildNumber here to the NuGet package version to work around the limitation that
      # the version cannot be passed to the Index Sources & Publish Symbols task.
    - pwsh: |
        $version = Get-Content '$(VersionArtifactName)/$(PackageVersionFileName)' -Raw
        Write-Host "##vso[task.setvariable variable=PackageVersion;]$version"
        Write-Host "##vso[build.updatebuildnumber]$version"
      displayName: 'Read PackageVersion from File to Build.BuildNumber'
    - template: 'build/azure-templates/show-all-environment-variables.yml'

    - template: 'build/azure-templates/publish-nuget-packages.yml'
      parameters:
        artifactFeedID: '$(ArtifactFeedID)'
        nugetArtifactName: '$(NuGetArtifactName)'
        debugArtifactName: '$(DebugArtifactName)'

- stage: Release_Stage
  displayName: 'Release Stage:'
  jobs:
  - job: Release
    condition: and(succeeded(), eq(variables['IsRelease'], 'true'))
    displayName: 'Build Release Artifacts for [VOTE]'
    pool:
      vmImage: 'windows-2019'

    steps:
    # We checkout here because we need to publish the source code along with the binaries per Apache's releae policy
    - checkout: self # self represents the repo where the initial Pipelines YAML file was found
      fetchDepth: '1'  # the depth of commits to ask Git to fetch

    - template: 'build/azure-templates/show-all-environment-variables.yml'


    - task: 'DownloadPipelineArtifact@0'
      displayName: 'Download Build Artifacts: $(NuGetArtifactName)'
      inputs:
        artifactName: '$(NuGetArtifactName)'
        targetPath: '$(Build.ArtifactStagingDirectory)/$(NuGetArtifactName)'


    - task: DownloadPipelineArtifact@0
      displayName: 'Download Build Artifacts: $(VersionArtifactName)'
      inputs:
        artifactName: '$(VersionArtifactName)'
        targetPath: '$(Build.ArtifactStagingDirectory)/$(VersionArtifactName)'
        
    - template: 'build/azure-templates/show-all-files.yml' # Uncomment for debugging

      # NOTE: We are setting Build.BuildNumber here to the NuGet package version to work around the limitation that
      # the version cannot be passed to the Index Sources & Publish Symbols task.
    - pwsh: |
        $version = Get-Content '$(Build.ArtifactStagingDirectory)/$(VersionArtifactName)/$(PackageVersionFileName)' -Raw
        $vcsLabel = 'Lucene.Net_' + $version.Replace('.', '_').Replace('-', '_')
        Write-Host "##vso[task.setvariable variable=VCSLabel;]$vcsLabel"
        Write-Host "##vso[task.setvariable variable=PackageVersion;]$version"
        Write-Host "##vso[build.updatebuildnumber]$version"
      displayName: 'Build VCS Label and Rehydrate Version Variables'
    - pwsh: |
        $files = 'build.bat','Version.props'
        foreach ($file in $files) {
            Copy-Item -Path "$(Build.ArtifactStagingDirectory)/$(VersionArtifactName)/$file" -Destination "$(Build.SourcesDirectory)/$file" -Force -ErrorAction Continue
        }
      displayName: 'Update build.bat and Version.props to build only version $(PackageVersion)'
    - template: 'build/azure-templates/show-all-environment-variables.yml'

    - task: CopyFiles@2
      displayName: 'Copy Source Code Files to: $(Build.ArtifactStagingDirectory)/srctemp'
      inputs:
        SourceFolder: '$(Build.SourcesDirectory)'
        Contents: |
         **
         !.git/**/*
         !branding/**/*
         !release/**/*
         !src/**/bin/**/*
         !src/**/obj/**/*
         branding/logo/lucene-net-icon-128x128.png
        TargetFolder: '$(Build.ArtifactStagingDirectory)/srctemp'

    - task: ArchiveFiles@2
      displayName: 'Archive Source Code Files'
      inputs:
        rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/srctemp'
        includeRootFolder: false
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(ReleaseArtifactName)/Apache-Lucene.Net-$(PackageVersion).src.zip'

    - task: CopyFiles@2
      displayName: 'Copy License/Notice Files to: $(NuGetArtifactName)'
      inputs:
        SourceFolder: '$(Build.SourcesDirectory)'
        Contents: |
         LICENSE.txt
         NOTICE.txt
        TargetFolder: '$(Build.ArtifactStagingDirectory)/$(NuGetArtifactName)'

    - task: ArchiveFiles@2
      displayName: 'Archive Binary Files'
      inputs:
        rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/$(NuGetArtifactName)'
        includeRootFolder: false
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(ReleaseArtifactName)/Apache-Lucene.Net-$(PackageVersion).bin.zip'

    - pwsh: |
        $dir = '$(Build.ArtifactStagingDirectory)/$(ReleaseArtifactName)'
        if (!(Test-Path $dir)) { New-Item -ItemType Directory -Path "$dir" -Force }
        $nl = [Environment]::NewLine
        "TODO: Review: http://www.apache.org/legal/release-policy.html" + $nl + `
        "TODO: Tag Repository" + $nl + `
        "  commit: $(Build.SourceVersion)" + $nl + `
        "  tag: $(VCSLabel)" + $nl + `
        "TODO: Sign release artifacts (see https://www.apache.org/dev/release-signing.html)" + $nl + `
        "TODO: Push release artifacts to dev (https://dist.apache.org/repos/dist/dev/lucenenet/)" + $nl + `
        "TODO: Start release [VOTE] (see https://www.apache.org/foundation/voting.html)" + $nl | Out-File -FilePath "$dir/RELEASE-TODO.txt" -Force
      displayName: 'Write RELEASE-TODO.txt'

    - task: PublishPipelineArtifact@1
      displayName: 'Publish Artifact: $(ReleaseArtifactName)'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/$(ReleaseArtifactName)'
        artifact:  '$(ReleaseArtifactName)'
        publishLocation: 'pipeline'

# LUCENENET TODO: Write VCS Label (git tag) automatically