steps:
- checkout: self
  clean: true

- task: DownloadPipelineArtifact@2
  inputs:
    source: specific
    project: PowerShellCore
    pipeline: '696'
    preferTriggeringPipeline: true
    runVersion: latestFromBranch
    runBranch: '$(Build.SourceBranch)'
    artifact: BuildInfoJson
    path: '$(Pipeline.Workspace)/releasePipeline/BuildInfoJson'

- pwsh: |
    Import-Module '$(Build.SourcesDirectory)/tools/ci.psm1'
    $jsonFile = Get-Item "$ENV:PIPELINE_WORKSPACE/releasePipeline/BuildInfoJson/*.json"
    $fileName = Split-Path $jsonFile -Leaf

    $dateTime = [datetime]::UtcNow
    $dateTime = [datetime]::new($dateTime.Ticks - ($dateTime.Ticks % [timespan]::TicksPerSecond), $dateTime.Kind)

    $metadata = Get-Content ./tools/metadata.json | ConvertFrom-Json
    $stableRelease = $metadata.StableRelease.Latest
    $ltsRelease = $metadata.LTSRelease.Latest

    $buildInfo = Get-Content $jsonFile | ConvertFrom-Json
    $buildInfo.ReleaseDate = $dateTime

    $targetFile = "$ENV:PIPELINE_WORKSPACE/$fileName"
    ConvertTo-Json -InputObject $buildInfo | Out-File $targetFile -Encoding ascii

    if ($stableRelease -or $fileName -eq "preview.json") {
      Set-BuildVariable -Name CopyMainBuildInfo -Value YES
    } else {
      Set-BuildVariable -Name CopyMainBuildInfo -Value NO
    }

    Set-BuildVariable -Name BuildInfoJsonFile -Value $targetFile

    ## Create 'lts.json' if it's the latest stable and also a LTS release.

    if ($fileName -eq "stable.json") {
      if ($ltsRelease) {
        $ltsFile = "$ENV:PIPELINE_WORKSPACE/lts.json"
        Copy-Item -Path $targetFile -Destination $ltsFile -Force
        Set-BuildVariable -Name LtsBuildInfoJsonFile -Value $ltsFile
        Set-BuildVariable -Name CopyLTSBuildInfo -Value YES
      } else {
        Set-BuildVariable -Name CopyLTSBuildInfo -Value NO
      }

      $releaseTag = $buildInfo.ReleaseTag
      $version = $releaseTag -replace '^v'
      $semVersion = [System.Management.Automation.SemanticVersion] $version

      $versionFile = "$ENV:PIPELINE_WORKSPACE/$($semVersion.Major)-$($semVersion.Minor).json"
      Copy-Item -Path $targetFile -Destination $versionFile -Force
      Set-BuildVariable -Name VersionBuildInfoJsonFile -Value $versionFile
      Set-BuildVariable -Name CopyVersionBuildInfo -Value YES
    } else {
      Set-BuildVariable -Name CopyVersionBuildInfo -Value NO
    }
  displayName: Download and Capture NuPkgs

- task: AzureFileCopy@2
  displayName: 'AzureBlob build info JSON file Copy'
  inputs:
    SourcePath: '$(BuildInfoJsonFile)'
    azureSubscription: '$(AzureFileCopySubscription)'
    Destination: AzureBlob
    storage: '$(StorageAccount)'
    ContainerName: BuildInfo
  condition: and(succeeded(), eq(variables['CopyMainBuildInfo'], 'YES'))

- task: AzureFileCopy@2
  displayName: 'AzureBlob build info ''lts.json'' Copy when needed'
  inputs:
    SourcePath: '$(LtsBuildInfoJsonFile)'
    azureSubscription: '$(AzureFileCopySubscription)'
    Destination: AzureBlob
    storage: '$(StorageAccount)'
    ContainerName: BuildInfo
  condition: and(succeeded(), eq(variables['CopyLTSBuildInfo'], 'YES'))

- task: AzureFileCopy@2
  displayName: 'AzureBlob build info ''Major-Minor.json'' Copy when needed'
  inputs:
    SourcePath: '$(VersionBuildInfoJsonFile)'
    azureSubscription: '$(AzureFileCopySubscription)'
    Destination: AzureBlob
    storage: '$(StorageAccount)'
    ContainerName: BuildInfo
  condition: and(succeeded(), eq(variables['CopyVersionBuildInfo'], 'YES'))
