parameters:
  jobName: ""
  displayName: ""
  imageName: ""
  globalToolExeName: 'pwsh.exe'
  globalToolPackageName: 'PowerShell.Windows.x64'


jobs:
- job: ${{ parameters.jobName }}
  displayName: ${{ parameters.displayName }}
  pool:
    vmImage: ${{ parameters.imageName }}
  steps:
    - checkout: self
      clean: true

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

    - pwsh: |
        $dotnetMetadataPath = "$(Build.SourcesDirectory)/DotnetRuntimeMetadata.json"
        $dotnetMetadataJson = Get-Content $dotnetMetadataPath -Raw | ConvertFrom-Json

        # Channel is like: $Channel = "5.0.1xx-preview2"
        $Channel = $dotnetMetadataJson.sdk.channel

        $sdkVersion = (Get-Content "$(Build.SourcesDirectory)/global.json" -Raw | ConvertFrom-Json).sdk.version
        Import-Module "$(Build.SourcesDirectory)/build.psm1" -Force

        Find-Dotnet

        if(-not (Get-PackageSource -Name 'dotnet' -ErrorAction SilentlyContinue))
        {
            $nugetFeed = ([xml](Get-Content $(Build.SourcesDirectory)/nuget.config -Raw)).Configuration.packagesources.add | Where-Object { $_.Key -eq 'dotnet' } | Select-Object -ExpandProperty Value
            Register-PackageSource -Name 'dotnet' -Location $nugetFeed -ProviderName NuGet
            Write-Verbose -Message "Register new package source 'dotnet'" -verbose
        }

        ## Install latest version from the channel

        #Install-Dotnet -Channel "$Channel" -Version $sdkVersion
        Start-PSBootstrap

        Write-Verbose -Message "Installing .NET SDK completed." -Verbose

      displayName: Install .NET

    - pwsh: |
        $branch = $ENV:BUILD_SOURCEBRANCH
        $version = $branch -replace '^.*(release[-/])v'
        $vstsCommandString = "vso[task.setvariable variable=PowerShellVersion]$version"
        Write-Verbose -Message "Version is $version" -Verbose
        Write-Host -Object "##$vstsCommandString"
      displayName: Set PowerShell Version

    - pwsh: |
        $env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
        Import-Module "$(Build.SourcesDirectory)/build.psm1" -Force
        Start-PSBootstrap -Force

        $toolPath = New-Item -ItemType Directory "$(System.DefaultWorkingDirectory)/toolPath" | Select-Object -ExpandProperty FullName

        dotnet tool install --add-source "$ENV:PIPELINE_WORKSPACE/releasePipeline/finalResults" --tool-path $toolPath --version '$(PowerShellVersion)' '${{ parameters.globalToolPackageName }}'

        Get-ChildItem -Path $toolPath

      displayName: Install global tool

    - pwsh: |
        $toolPath = "$(System.DefaultWorkingDirectory)/toolPath/${{ parameters.globalToolExeName }}"

        if (-not (Test-Path $toolPath))
        {
            throw "Tool is not installed at $toolPath"
        }
        else
        {
            Write-Verbose -Verbose "Tool found at: $toolPath"
        }
      displayName: Validate tool is installed

    - pwsh: |
        Import-Module "$(Build.SourcesDirectory)/build.psm1" -Force
        Start-PSBootstrap -Force

        $exeName = if ($IsWindows) { "pwsh.exe" } else { "pwsh" }

        $toolPath = "$(System.DefaultWorkingDirectory)/toolPath/${{ parameters.globalToolExeName }}"

        $versionFound = & $toolPath -c '$PSVersionTable.PSVersion.ToString()'

        if ( '$(PowerShellVersion)' -ne $versionFound)
        {
            throw "Expected version of global tool not found. Installed version is $versionFound"
        }
        else
        {
            write-verbose -verbose "Found expected version: $versionFound"
        }

        $dateYear = & $toolPath -c '(Get-Date).Year'

        if ( $dateYear -ne [DateTime]::Now.Year)
        {
            throw "Get-Date returned incorrect year: $dateYear"
        }
        else
        {
            write-verbose -verbose "Got expected year: $dateYear"
        }
      displayName: Basic validation
