From 5861cd4aaa7382df3a0c7e3a14795515d5ea20e6 Mon Sep 17 00:00:00 2001 From: Brett Hewitson Date: Thu, 29 Dec 2022 15:11:11 +1000 Subject: [PATCH] pipeline file changes --- ...mantic-versioning-to-assembyinfo-files.yml | 97 +++++++++++++++++++ .azure/pipelines/azure-pipelines-build.yml | 8 +- ...eAgentCapabilitiesEnvironmentVariables.ps1 | 94 ------------------ 3 files changed, 104 insertions(+), 95 deletions(-) create mode 100644 .azure/pipelines/apply-semantic-versioning-to-assembyinfo-files.yml delete mode 100644 .azure/scripts/CreateAgentCapabilitiesEnvironmentVariables.ps1 diff --git a/.azure/pipelines/apply-semantic-versioning-to-assembyinfo-files.yml b/.azure/pipelines/apply-semantic-versioning-to-assembyinfo-files.yml new file mode 100644 index 00000000..dac86bd5 --- /dev/null +++ b/.azure/pipelines/apply-semantic-versioning-to-assembyinfo-files.yml @@ -0,0 +1,97 @@ +parameters: +- name: ApplicationName + type: string +- name: PathToSearch + type: string + +steps: +- task: PowerShell@2 + displayName: Apply Semantic Versioning to AssemblyInfo Files + inputs: + targetType: inline + script: | + [string]$ApplicationName = ${{parameters.ApplicationName}} + [string]$PathToSearch = ${{parameters.PathToSearch}} + [string]$BuildNumber = $(Build.BuildNumber) + [string]$SearchFilter = 'AssemblyInfo.*' + + [string]$PatternSplitCharacters = '.' + [regex]$Pattern = '\d+\.\d+\.\d+\.\d+' + [int]$PatternExpectedVersionNumbers = 4 + [int]$VersionNumbersInVersion = 4 + + Function Replace-Version($content, $version, $attribute) { + $exitFunction = $false + $content | % { + if ($_ -match 'exclude from semantic versioning') { + Write-Host " * Skipping $attribute due to exclude" + $exitFunction = $true + } + + if ($_ -match 'include semantic versioning' -and $_ -notmatch "include semantic versioning - $ApplicationName") { + Write-Host " * Skipping $attribute due to include not matching" + $exitFunction = $true + } + } + + if ($exitFunction) { + return $content + } + + $versionAttribute = "[assembly: $attribute(""$version"")]" + $Pattern = "\[assembly: $attribute\("".*""\)\]" + $versionReplaced = $false + + $content = $content | % { + if ($_ -match $Pattern) { + $versionReplaced = $true + $_ = $_ -replace [regex]::Escape($Matches[0]),$versionAttribute + Write-Host " * Replaced $($Matches[0]) with $versionAttribute" + } + $_ + } + + if (-not $versionReplaced) { + $content += [Environment]::NewLine + $versionAttribute + Write-Host " * Added $versionAttribute to end of content" + } + + return $content + } + + Function Get-VersionString($numberOfVersions, $extractedBuildNumbers) { + return [string]::Join('.',($extractedBuildNumbers | select -First ($numberOfVersions))) + } + + $ErrorActionPreference = 'Stop' + + if ($BuildNumber -match $Pattern -ne $true) { + Write-Host "Could not extract a version from [$BuildNumber] using pattern [$Pattern]" + exit 2 + } + + # Set version variables + $extractedBuildNumbers = @($Matches[0].Split(([char[]]$PatternSplitCharacters))) + if ($extractedBuildNumbers.Length -ne $PatternExpectedVersionNumbers) { + Write-Host "The extracted build number $($Matches[0]) does not contain the expected $PatternExpectedVersionNumbers elements" + exit 2 + } + + $version = Get-VersionString -numberOfVersions $VersionNumbersInVersion -extractedBuildNumbers $extractedBuildNumbers + $fileVersion = Get-VersionString -numberOfVersions $VersionNumbersInVersion -extractedBuildNumbers $extractedBuildNumbers + Write-Host "Using version $version and file version $fileVersion" + + # iterate the search path (and sub directories) looking for files that match the search filter + Get-ChildItem -Path $PathToSearch -Filter $SearchFilter -Recurse | % { + Write-Host " -> Checking $($_.FullName)" + + # remove the read-only bit on the file + Set-ItemProperty $_.FullName -name IsReadOnly -value $false + + # run the regex replace + $content = Get-Content $_.FullName + $content = Replace-Version -content $content -version $version -attribute 'AssemblyVersion' + $content = Replace-Version -content $content -version $fileVersion -attribute 'AssemblyFileVersion' + $content | Set-Content $_.FullName -Encoding UTF8 + } + pwsh: true diff --git a/.azure/pipelines/azure-pipelines-build.yml b/.azure/pipelines/azure-pipelines-build.yml index 027660d6..1f8f29d3 100644 --- a/.azure/pipelines/azure-pipelines-build.yml +++ b/.azure/pipelines/azure-pipelines-build.yml @@ -53,8 +53,14 @@ stages: inputs: restoreSolution: '${{parameters.SolutionFile}}' + - template: apply-semantic-versioning-to-assembyinfo-files.yml@self + parameters: + ApplicationName: ${{parameters.ApplicationName}} + PathToSearch: '$(Build.SourcesDirectory)/src' + - task: PowerShell@2 - displayName: Apply Semantic Versioning to AssemblyInfo Files + displayName: Apply Semantic Versioning to AssemblyInfo Files (old) + enabled: false inputs: targetType: filePath filePath: './.azure/scripts/ApplySemanticVersioningToAssemblies.ps1' diff --git a/.azure/scripts/CreateAgentCapabilitiesEnvironmentVariables.ps1 b/.azure/scripts/CreateAgentCapabilitiesEnvironmentVariables.ps1 deleted file mode 100644 index 8ed43c7a..00000000 --- a/.azure/scripts/CreateAgentCapabilitiesEnvironmentVariables.ps1 +++ /dev/null @@ -1,94 +0,0 @@ -Param ( - [string]$CollectionUri, - [int]$AgentId, - [string]$AgentName, - [string]$AccessToken, - [string]$DebugMode = 'false' -) - -Function Get-AzureDevopsAgent() { - param( - [Parameter(Mandatory = $true)] [string]$baseUri, - [Parameter(Mandatory = $true)] [string]$accessToken, - [Parameter(Mandatory = $true)] [int]$agentId, - [Parameter(Mandatory = $true)] [string]$agentName - ) - - try { - [Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls13" - - $headers = @{ - Authorization = "Bearer $accessToken" - } - - $uri = "$($baseUri)/distributedtask/pools?api-version=6.0" - $responsePools = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -UseBasicParsing - - foreach ($pool in $responsePools.Value) { - - $uri = "$($baseUri)/distributedtask/pools/$($pool.Id)/agents?api-version=6.0&includeCapabilities=true" - $responseAgents = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -UseBasicParsing - - $agents = $responseAgents.Value.Where({$_.id -eq $agentId -and $_.name -eq $agentName}) - if (!($agents) -or $agents.Count -eq 0) { - continue - } - - if ($agents.Count -gt 1) { - throw "Multiple agents ($($agents.Count)) found with id: $agentId and name: $agentName" - } - - return $agents.Item(0) - } - - Write-Host -ForeGroundColor Yellow 'Agent NOT found' - return $null - } - catch { - - Write-Host -ForeGroundColor Red 'Unhandled exception occurred during agent fetch!' - Write-Host -ForegroundColor Red $_.Exception.Message - throw - } -} - -Function Output-AgentCapabilities() { - param( - [Parameter(Mandatory = $true)] [PSCustomObject]$capabilities, - [Parameter(Mandatory = $true)] [string]$capabilityType - ) - - [int]$count = 0 - foreach ($capability in $capabilities.PSObject.Properties) { - $envName = "AgentCapabilities.$($capabilityType).$($capability.Name)".Replace('_', '.') - [System.Environment]::SetEnvironmentVariable($envName, $($capability.Value)) - - $count = $count + 1 - } - - Write-Host -ForeGroundColor Cyan "Created $count AgentCapabilities.$capabilityType environment variables" -} - -$ErrorActionPreference = 'Stop' - -if ($debugMode -eq "true") -{ - Write-Host "##[section]Starting: DEBUG INFORMATION" - - Write-Host "##[debug]CollectionUri = $CollectionUri" - Write-Host "##[debug]AgentId = $AgentId" - Write-Host "##[debug]AgentName = $AgentName" - Write-Host "##[debug]AccessToken = $AccessToken" - - Write-Host "##[section]Finishing: DEBUG INFORMATION" - Write-Host "" -} - -$AgentData = Get-AzureDevopsAgent -baseUri "$($CollectionUri)_apis" -accessToken $AccessToken -agentId $AgentId -agentName $AgentName -if ($AgentData) { - #Output-AgentCapabilities -capabilities $AgentData.systemCapabilities -capabilityType 'System' - Output-AgentCapabilities -capabilities $AgentData.userCapabilities -capabilityType 'User' -} - -Write-Host 'Done' -exit 0 \ No newline at end of file