jobs:

- job: BuildJob
  displayName: Build
  condition: succeeded()
  pool:
    name: PowerShell
  strategy:
    matrix:
      Build (x64,release):
        BuildConfiguration: release
        BuildPlatform: any cpu
        Architecture: x64
      Build (x86,release):
        BuildConfiguration: release
        BuildPlatform: any cpu
        Architecture: x86
      Build (arm,release):
        BuildConfiguration: release
        BuildPlatform: any cpu
        Architecture: arm
      Build (arm64,release):
        BuildConfiguration: release
        BuildPlatform: any cpu
        Architecture: arm64
      Build (fxdependent,release):
        BuildConfiguration: release
        BuildPlatform: any cpu
        Architecture: fxdependent

  steps:

  - checkout: self
    clean: true
    persistCredentials: true

  - task: PkgESSetupBuild@10
    displayName: 'Initialize build'
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      useDfs: false
      productName: PowerShellCore
      branchVersion: true
      disableWorkspace: true
      disableBuildTools: true
      disableNugetPack: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
        tools/releaseBuild/setReleaseTag.ps1 -ReleaseTag $(ReleaseTag)
    displayName: 'Set ReleaseTag Variable'

  - powershell: |
       Write-Verbose -Verbose "$(Architecture)"

       if ('$(Architecture)' -eq 'fxdependent' -and '$(ReleaseTag)' -match '6.0.*')
       {
           $vstsCommandString = "vso[task.setvariable variable=SkipFxDependent]true"
       }
       else
       {
           $vstsCommandString = "vso[task.setvariable variable=SkipFxDependent]false"
       }

       Write-Verbose -Message "$vstsCommandString " -Verbose
       Write-Host -Object "##$vstsCommandString"
    displayName: 'Skip FxDependent for PS v6.0.*'

  - powershell: |
       Import-Module $(Build.SourcesDirectory)/build.psm1 -Force
       New-NugetConfigFile -NugetFeedUrl $(AzDevOpsFeed) -UserName $(AzDevOpsFeedUserName) -ClearTextPAT $(AzDevOpsFeedPAT) -FeedName AzDevOpsFeed -Destination $(Build.SourcesDirectory)/src/Modules

       if(-not (Test-Path "$(Build.SourcesDirectory)/src/Modules/nuget.config"))
       {
           throw "nuget.config is not created"
       }
    displayName: 'Add nuget.config for AzDevOps feed for PSGallery modules '
    condition: ne(Variables['SkipFxDependent'], 'true')

  - powershell: |
       $version = $env:ReleaseTag.Substring(1)
       $vstsCommandString = "vso[task.setvariable variable=Version]$version"
       Write-Host "sending " + $vstsCommandString
       Write-Host "##$vstsCommandString"
    displayName: 'Set Version Varibale'
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       docker container prune --force
       docker container ls --all --format '{{ json .ID }}' | ConvertFrom-Json | ForEach-Object {docker container rm --force --volumes $_}
    displayName: 'remove all containers  [Port to PSRelease]'
    continueOnError: true
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       docker image ls --format '{{ json .}}'|ConvertFrom-Json| ForEach-Object {
           if($_.tag -eq '<none>')
           {
               $formatString = 'yyyy-MM-dd HH:mm:ss zz00'
               $createdAtString = $_.CreatedAt.substring(0,$_.CreatedAt.Length -4)
               $createdAt = [DateTime]::ParseExact($createdAtString, $formatString,[System.Globalization.CultureInfo]::InvariantCulture)
               if($createdAt -lt (Get-Date).adddays(-1))
               {
                    docker image rm $_.ID
               }
           }
       }
       exit 0
    displayName: 'remove old images [Port to PSRelease]'
    continueOnError: true
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       Write-verbose "--docker info---" -verbose
       docker info
       Write-verbose "--docker image ls---" -verbose
       docker image ls
       Write-verbose "--docker container ls --all---" -verbose
       docker container ls --all
       Write-verbose "--git branch ---" -verbose
       git branch
       exit 0
    displayName: 'Get Environment'
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
      tools/releaseBuild/vstsbuild.ps1 -ReleaseTag $(ReleaseTag) -Name win-$(Architecture)-symbols
    displayName: 'Build Windows Universal - $(Architecture) Symbols zip'
    continueOnError: true
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       if ("$env:Architecture" -eq 'fxdependent')
       {
           $(Build.SourcesDirectory)\tools\releaseBuild\updateSigning.ps1 -SkipPwshExe
       }
       else
       {
           $(Build.SourcesDirectory)\tools\releaseBuild\updateSigning.ps1
       }
    displayName: 'Update Signing Xml'
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       $vstsCommandString = "vso[task.setvariable variable=Symbols]${env:Symbols_$(Architecture)}"
       Write-Host "sending " + $vstsCommandString
       Write-Host "##$vstsCommandString"
    displayName: 'Get Symbols path [Update build.json]'
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

  - task: PkgESCodeSign@10
    displayName: 'CodeSign $(Architecture)'
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      signConfigXml: '$(Build.SourcesDirectory)\tools\releaseBuild\signing.xml'
      inPathRoot: '$(Symbols)'
      outPathRoot: '$(Symbols)\signed'
      binVersion: $(SigingVersion)
      binVersionOverride: $(SigningVersionOverride)
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       New-Item -ItemType Directory -Path $(Symbols)\signed -Force
    displayName: 'Create empty signed folder'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'Manual'), ne(variables['SkipFxDependent'], 'true'))

  - powershell: |
       tools/releaseBuild/vstsbuild.ps1 -ReleaseTag $(ReleaseTag) -Name win-$(Architecture)-package -BuildPath $(Symbols) -SignedFilesPath $(Symbols)\signed
    displayName: 'Build Windows Universal - $(Architecture) Package'
    continueOnError: true
    condition: and(succeeded(), ne(variables['SkipFxDependent'], 'true'))

- job: ComponentRegistrationJob
  displayName: Component Registration
  dependsOn: BuildJob
  condition: succeeded()
  pool:
    name: PowerShell
  strategy:
    matrix:
      release-anycpu:
        BuildConfiguration: release
        BuildPlatform: any cpu

  steps:

  - powershell: |
      ./tools/releaseBuild/setReleaseTag.ps1 -ReleaseTag $(ReleaseTag)
    displayName: 'Set ReleaseTag Variable'

  - powershell: |
       $version = $env:ReleaseTag.Substring(1)
       $vstsCommandString = "vso[task.setvariable variable=Version]$version"
       Write-Host "sending " + $vstsCommandString
       Write-Host "##$vstsCommandString"
    displayName: 'Set Version Variable'

  - powershell: |
       docker container prune --force
       docker container ls --all --format '{{ json .ID }}' | ConvertFrom-Json | ForEach-Object {docker container rm --force --volumes $_}
    displayName: 'Remove all containers'
    continueOnError: true

  - powershell: |
       docker image ls --format '{{ json .}}'|ConvertFrom-Json| ForEach-Object {
           if($_.tag -eq '<none>')
           {
               $formatString = 'yyyy-MM-dd HH:mm:ss zz00'
               $createdAtString = $_.CreatedAt.substring(0,$_.CreatedAt.Length -4)
               $createdAt = [DateTime]::ParseExact($createdAtString, $formatString,[System.Globalization.CultureInfo]::InvariantCulture)
               if($createdAt -lt (Get-Date).adddays(-1))
               {
                    docker image rm $_.ID
               }
           }
       }
       exit 0
    displayName: 'Remove old images'
    continueOnError: true

  - powershell: |
       Write-verbose "--docker info---" -verbose
       docker info
       Write-verbose "--docker image ls---" -verbose
       docker image ls
       Write-verbose "--docker container ls --all---" -verbose
       docker container ls --all
       Write-verbose "--git branch ---" -verbose
       git branch
       exit 0
    displayName: 'Get Environment'

  - powershell: |
       ./tools/releaseBuild/vstsbuild.ps1 -ReleaseTag $(ReleaseTag) -Name win-x64-component-registration
    displayName: 'Build Windows Universal - Component Registration'

  - task: ms.vss-governance-buildtask.governance-build-task-component-detection.ComponentGovernanceComponentDetection@0
    displayName: 'Component Detection'
    inputs:
      sourceScanPath: '$(componentregistration)'
      snapshotForceEnabled: true

- job: PackageSigningJob
  displayName: Package signing
  dependsOn: BuildJob
  condition: succeeded()
  pool:
    name: Package ES CodeHub Lab E
  strategy:
    matrix:
      release-anycpu:
        BuildConfiguration: release
        BuildPlatform: any cpu

  steps:

  - powershell: |
       tools/releaseBuild/setReleaseTag.ps1 -ReleaseTag $(ReleaseTag)
    displayName: 'Set ReleaseTag Variable'
    continueOnError: true

  - powershell: |
       $version = $env:ReleaseTag.Substring(1)
       $vstsCommandString = "vso[task.setvariable variable=Version]$version"
       Write-Host "sending " + $vstsCommandString
       Write-Host "##$vstsCommandString"

       $azureVersion = $env:ReleaseTag.ToLowerInvariant() -replace '\.', '-'
       $vstsCommandString = "vso[task.setvariable variable=AzureVersion]$azureVersion"
       Write-Host "sending " + $vstsCommandString
       Write-Host "##$vstsCommandString"

    displayName: 'Set Version Variable'
    continueOnError: true

  - task: DownloadBuildArtifacts@0
    displayName: 'Download artifacts'
    inputs:
      downloadType: specific
    continueOnError: true

  - powershell: |
      dir "$(System.ArtifactsDirectory)\*" -Recurse
    displayName: 'dir artifacts directory'
    continueOnError: true

  - powershell: |
      Expand-Archive -Path "$(System.ArtifactsDirectory)\results\PowerShell-$(Version)-symbols-win-x86.zip" -Destination "$(Build.StagingDirectory)\symbols\x86"
    displayName: 'Expand symbols zip - x86'
    continueOnError: true

  - powershell: |
      Expand-Archive -Path "$(System.ArtifactsDirectory)\results\PowerShell-$(Version)-symbols-win-x64.zip" -Destination "$(Build.StagingDirectory)\symbols\x64"
    displayName: 'Expand symbols zip - x64'
    continueOnError: true

  - powershell: |
      Expand-Archive -Path "$(System.ArtifactsDirectory)\results\PowerShell-$(Version)-symbols-win-fxdependent.zip" -Destination "$(Build.StagingDirectory)\symbols\fxdependent"
    displayName: 'Expand symbols zip - fxdependent'
    continueOnError: true

  - powershell: |
      tools/releaseBuild/generatePackgeSigning.ps1 -AuthenticodeFiles "$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-x64.msi","$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-x86.msi" -path "$(System.ArtifactsDirectory)\package.xml"
    displayName: 'Generate Package Signing Xml'

  - powershell: |
      Get-Content "$(System.ArtifactsDirectory)\package.xml"
    displayName: 'print signing xml'
    continueOnError: true

  - task: PkgESCodeSign@10
    displayName: 'CodeSign $(System.ArtifactsDirectory)\package.xml'
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      signConfigXml: '$(System.ArtifactsDirectory)\package.xml'
      outPathRoot: '$(Build.StagingDirectory)\signedPackages'
      binVersion: $(SigingVersion)
      binVersionOverride: $(SigningVersionOverride)
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(Build.StagingDirectory)\signedPackages\PowerShell-$(Version)-win-x64.msi"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[Create script] upload signed msi - x64'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - task: AzureFileCopy@1
    displayName: 'upload signed msi to Azure - x64'
    inputs:
      SourcePath: '$(Build.StagingDirectory)\signedPackages\PowerShell-$(Version)-win-x64.msi'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(Build.StagingDirectory)\signedPackages\PowerShell-$(Version)-win-x86.msi"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[create script] upload signed msi - x86'
    continueOnError: true

  - task: AzureFileCopy@1
    displayName: 'upload signed msi to Azure - x86'
    inputs:
      SourcePath: '$(Build.StagingDirectory)\signedPackages\PowerShell-$(Version)-win-x86.msi'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-x64.zip"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[Create script] upload signed zip - x64'
    continueOnError: true

  - task: AzureFileCopy@1
    displayName: 'upload signed zip to Azure - x64'
    inputs:
      SourcePath: '$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-x64.zip'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-x86.zip"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[create script] upload signed zip - x86'
    continueOnError: true

  - task: AzureFileCopy@1
    displayName: 'upload signed zip to Azure - x86'
    inputs:
      SourcePath: '$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-x86.zip'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-arm32.zip"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[create script] upload signed zip - arm'
    continueOnError: true

  - task: AzureFileCopy@1
    displayName: 'upload signed zip to Azure - arm'
    inputs:
      SourcePath: '$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-arm32.zip'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-arm64.zip"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[create script] upload signed zip - arm64'
    continueOnError: true

  - task: AzureFileCopy@1
    displayName: 'upload signed zip to Azure - arm64'
    inputs:
      SourcePath: '$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-arm64.zip'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))

  - powershell: |
       $packagePath = "$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-fxdependent.zip"
       if(Test-Path -Path $packagePath)
       {
           Write-Host "##vso[artifact.upload containerfolder=signedResults;artifactname=signedResults]$packagePath"
       }
    displayName: '[create script] upload signed zip - fxdependent'
    continueOnError: true

  - task: AzureFileCopy@1
    displayName: 'upload signed zip to Azure - fxdependent'
    inputs:
      SourcePath: '$(System.ArtifactsDirectory)\signed\PowerShell-$(Version)-win-fxdependent.zip'
      azureSubscription: '$(AzureFileCopySubscription)'
      Destination: AzureBlob
      storage: '$(StorageAccount)'
      ContainerName: '$(AzureVersion)'
    continueOnError: true
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Manual'))
