diff --git a/eng/pipelines/build-core-lib.yml b/eng/pipelines/build-core-lib.yml index ac18021963..9ddbeb1a92 100644 --- a/eng/pipelines/build-core-lib.yml +++ b/eng/pipelines/build-core-lib.yml @@ -107,101 +107,19 @@ extends: artifactName: PackageArtifacts steps: + # Compute AssemblyVersion and PackageVersion # -> Update version.yml - - powershell: | - - # Example with FileVersion: "1.2.4" and PackageSuffix: "RC.1" - # Build.BuildNumber = 1.2.4.23296.1 - # = $(FileVersion).$(Year:yy)$(DayOfYear).$(Rev:r) - - # Default values - $branch = "PR" - $package = "" - - # To Test? - $toTest = "true" - - # BranchName = dev, main, archive or PR - if ("$(Build.SourceBranchName)" -eq "main") - { - $branch = "main" - } - elseif ("$(Build.SourceBranchName)" -eq "dev") - { - $branch = "dev" - } - elseif ("$(Build.SourceBranch)" -like "refs/heads/archives/*") - { - $branch = "archive" - } - else - { - $branch = "PR" - } - - # [1, 2, 4, 23296, 1] - $builds = "$(Build.BuildNumber)".Split('.') - - # 1.2.4.23296 - $assembly = "$($builds[0]).$($builds[1]).$($builds[2]).$($builds[3])" - - # Main or Archive without PackageSuffix: 1.2.4 - # Main or Archive with PackageSuffix: 1.2.4-rc.1 - if ("$branch" -eq "main" -or "$branch" -eq "archive") - { - # Main without PackageSuffix - if ("$(PackageSuffix)" -eq "") - { - $package = "$($builds[0]).$($builds[1]).$($builds[2])" - } - - # Main with PackageSuffix - else - { - $package = "$($builds[0]).$($builds[1]).$($builds[2])-$(PackageSuffix)" - } - - $toTest = "true" - } - - # Dev: 1.2.4-preview-23296-1 - elseif ("$branch" -eq "dev") - { - $package = "$($builds[0]).$($builds[1]).$($builds[2])-preview.$($builds[3]).$($builds[4])" - $toTest = "true" - } - - # Other branches: 1.2.4-preview-23296-1 - else - { - $package = "$($builds[0]).$($builds[1]).$($builds[2])-preview.$($builds[3]).$($builds[4])" - $toTest = "true" - } - - if ("${{ parameters.Tests }}" -eq "") - { - $toTest = "false" - } - - # Set the output variable for use in other tasks. - Write-Host "##vso[task.setvariable variable=AssemblyVersion]${assembly}" - Write-Host "##vso[task.setvariable variable=PackageVersion]${package}" - Write-Host "##vso[task.setvariable variable=ShouldTest]${toTest}" - displayName: Compute AssemblyVersion and PackageVersion - - # Display computed variables - - script: | - echo 🔸 FileVersion = $(FileVersion) - echo 🔸 PackageSuffix = $(PackageSuffix) - echo 🔸 Build.BuildNumber = $(Build.BuildNumber) - echo 🔸 Build.SourceBranch = $(Build.SourceBranch) - echo ----------------------------------------------- - echo 🔸 AssemblyVersion = $(AssemblyVersion) - echo 🔸 PackageVersion = $(PackageVersion) - echo ----------------------------------------------- - echo 🔸 ShouldTest = $(ShouldTest) - displayName: Display computed variables + - task: PowerShell@2 + displayName: 'Compute AssemblyVersion and PackageVersion' + inputs: + targetType: 'filePath' + filePath: $(System.DefaultWorkingDirectory)/eng/pipelines/compute-version-variables.ps1 + arguments: > # Use this to avoid newline characters in multiline string + -branchName "$(Build.SourceBranchName)" + -buildNumber "$(Build.BuildNumber)" + -packageSuffix "$(PackageSuffix)" + -testProjects "${{ parameters.Tests }}" # Install NuGet tools - task: NuGetToolInstaller@1 diff --git a/eng/pipelines/compute-version-variables.ps1 b/eng/pipelines/compute-version-variables.ps1 new file mode 100644 index 0000000000..b659b0fd1f --- /dev/null +++ b/eng/pipelines/compute-version-variables.ps1 @@ -0,0 +1,130 @@ +<# +.DESCRIPTION + Compute AssemblyVersion and PackageVersion + +.PARAMETER branchName + The branch of the trigger repo for which the build has been queued. + Please, use $(Build.SourceBranchName) + +.PARAMETER buildNumber + The name of the completed build, also known as the run number. + This value is formatted like "[Major].[Minor].[Revision].[Year:00].[DayOfYear].[BuildRevision]" (e.g. "4.6.1.24123.3") + Please, use $(Build.BuildNumber) + +.PARAMETER packageSuffix + Suffix to add to the computed version. Example "Preview", "RC.1", ... + This suffix overrides the one calculated for dev our main branches. + Default is "". + +.PARAMETER testProjects + Projects to tests, to define the variable "ShouldTest". + Not empty, ShouldTest will be "true". + +.EXAMPLE + $> .\compute-version-variables -branchName "dev" -buildNumber "4.6.1.24123.3" -packageSuffix "Preview" + +.EXAMPLE + # Compute AssemblyVersion and PackageVersion + # -> Update version.yml + - task: PowerShell@2 + displayName: 'Compute AssemblyVersion and PackageVersion' + inputs: + targetType: 'filePath' + filePath: $(System.DefaultWorkingDirectory)/eng/pipelines/compute-version-variables.ps1 + arguments: > # Use this to avoid newline characters in multiline string + -branchName "$(Build.SourceBranchName)" + -buildNumber "$(Build.BuildNumber)" + -packageSuffix "$(PackageSuffix)" + -testProjects "${{ parameters.Tests }}" + +#> + +param ( + [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$branchName, + [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$buildNumber, + [string]$packageSuffix, + [string]$testProjects +) + +Write-Host "Compute AssemblyVersion and PackageVersion." +Write-Host "" + +# Default values +$branch = "PR" +$package = "" + +# To Test? +$toTest = "true" + +# BranchName = dev, main, archive or PR +if ($branchName -eq "main") { + $branch = "main" +} +elseif ($branchName -eq "dev") { + $branch = "dev" +} +# elseif ("$(Build.SourceBranch)" -like "refs/heads/archives/*") +elseif ($branchName -like "*/archives/*") { + $branch = "archive" +} +else { + $branch = "PR" +} + +# [1, 2, 4, 23296, 1] +$builds = $buildNumber.Split('.') + +# 1.2.4 +$fileVersion = "$($builds[0]).$($builds[1]).$($builds[2])" + +# 1.2.4.23296 +$assembly = "$($builds[0]).$($builds[1]).$($builds[2]).$($builds[3])" + +# Main or Archive without PackageSuffix: 1.2.4 +# Main or Archive with PackageSuffix: 1.2.4-rc.1 +if ($branch -eq "main" -or $branch -eq "archive") { + # Main without PackageSuffix + if ($packageSuffix -eq "") { + $package = "$($builds[0]).$($builds[1]).$($builds[2])" + } + + # Main with PackageSuffix + else { + $package = "$($builds[0]).$($builds[1]).$($builds[2])-$packageSuffix" + } + + $toTest = "true" +} + +# Dev: 1.2.4-preview-23296-1 +elseif ($branch -eq "dev") { + $package = "$($builds[0]).$($builds[1]).$($builds[2])-preview.$($builds[3]).$($builds[4])" + $toTest = "true" +} + +# Other branches: 1.2.4-preview-23296-1 +else { + $package = "$($builds[0]).$($builds[1]).$($builds[2])-preview.$($builds[3]).$($builds[4])" + $toTest = "true" +} + +if ($testProjects -eq "") { + $toTest = "false" +} + +# Set the output variable for use in other tasks. +Write-Host "##vso[task.setvariable variable=AssemblyVersion]$assembly" +Write-Host "##vso[task.setvariable variable=PackageVersion]$package" +Write-Host "##vso[task.setvariable variable=ShouldTest]$toTest" + +# Display computed versions +Write-Host "" +Write-Host "----------------------------------------------- " +Write-Host " - Branch = $branch " +Write-Host " - BuildNumber = $buildNumber " +Write-Host " - PackageSuffix = $packageSuffix " +Write-Host "----------------------------------------------- " +Write-Host " -> AssemblyVersion = $assembly " +Write-Host " -> PackageVersion = $package " +Write-Host " -> ShouldTest = $toTest " +Write-Host "----------------------------------------------- "