tasks/versioning.tasks.ps1

# Control flags
$SkipGitVersion = $false

# Options
$GitVersionToolVersion = "5.2.0"
$EnableGitVersionAdoVariableWorkaround = $false

# Synopsis: Run GitVersion tool
task GitVersion -If {!$SkipGitVersion} {
    Install-DotNetTool -Name "GitVersion.Tool" -Version $GitVersionToolVersion
    $gitVersionOutputJson = exec { dotnet-gitversion /output json /nofetch }
    
    Write-Build Cyan "GitVersion Output:`n$gitVersionOutputJson"

    $env:GitVersionOutput = $gitVersionOutputJson
    $script:GitVersion = $gitVersionOutputJson | ConvertFrom-Json -AsHashtable

    # Set the native GitVersion output as environment variables and build server variables
    foreach ($var in $script:GitVersion.Keys) {
        Set-Item -Path "env:GITVERSION_$var" -Value $GitVersion[$var]
        Set-BuildServerVariable -Name $var -Value $GitVersion[$var]
    }
}

task ApplyAzureDevopsGitVersionVariablesWorkaround -If {$EnableGitVersionAdoVariableWorkaround} `
                                                   -After GitVersion {
    # Starting with GitVersion 5.2.0, GitVersion includes isOutput=true on all of the variables it
    # sets, which is a breaking change. It means that these variables are no longer available with
    # the same name - they must now be qualified by the name of the task that produced them.
    # You can't turn this off, so we have to adapt to it. This just republishes them by their
    # old names, meaning anything previously depending on those names will continue to work.

    Write-Build White "Setting backwards-compatible GitVersion variables for Azure DevOps"
    Set-BuildServerVariable -Name "GitVersion.SemVer" $GitVersion.SemVer
    Set-BuildServerVariable -Name "GitVersion.PreReleaseTag" $GitVersion.PreReleaseTag
    Set-BuildServerVariable -Name "GitVersion.MajorMinorPatch" $GitVersion.MajorMinorPatch
}