script file changes

This commit is contained in:
Brett Hewitson 2022-12-22 21:31:58 +10:00
parent d5d8c2c9a6
commit 501e2755bb
2 changed files with 150 additions and 67 deletions

View file

@ -2,33 +2,29 @@ Param (
[string]$applicationName, [string]$applicationName,
[string]$pathToSearch, [string]$pathToSearch,
[string]$buildNumber, [string]$buildNumber,
[regex]$pattern = "\d+\.\d+\.\d+\.\d+", [regex]$pattern = '\d+\.\d+\.\d+\.\d+',
[string]$patternSplitCharacters = ".", [string]$patternSplitCharacters = '.',
[int]$patternExpectedVersionNumbers = 4, [int]$patternExpectedVersionNumbers = 4,
[int]$versionNumbersInVersion = 4, [int]$versionNumbersInVersion = 4,
[string]$searchFilter = "AssemblyInfo.*", [string]$searchFilter = 'AssemblyInfo.*',
[string]$debugMode = 'false' [string]$debugMode = 'false'
) )
# Declare functions function Replace-Version($content, $version, $attribute) {
function Replace-Version($content, $version, $attribute)
{
$exitFunction = $false $exitFunction = $false
$content | % { $content | % {
if ($_ -match 'exclude from semantic versioning') if ($_ -match 'exclude from semantic versioning') {
{
Write-Host " * Skipping $attribute due to exclude" Write-Host " * Skipping $attribute due to exclude"
$exitFunction = $true $exitFunction = $true
} }
if ($_ -match 'include semantic versioning' -and $_ -notmatch "include semantic versioning - $applicationName")
{ if ($_ -match 'include semantic versioning' -and $_ -notmatch "include semantic versioning - $applicationName") {
Write-Host " * Skipping $attribute due to include not matching" Write-Host " * Skipping $attribute due to include not matching"
$exitFunction = $true $exitFunction = $true
} }
} }
if ($exitFunction) if ($exitFunction) {
{
return $content return $content
} }
@ -37,8 +33,7 @@ function Replace-Version($content, $version, $attribute)
$versionReplaced = $false $versionReplaced = $false
$content = $content | % { $content = $content | % {
if ($_ -match $pattern) if ($_ -match $pattern) {
{
$versionReplaced = $true $versionReplaced = $true
$_ = $_ -replace [regex]::Escape($Matches[0]),$versionAttribute $_ = $_ -replace [regex]::Escape($Matches[0]),$versionAttribute
Write-Host " * Replaced $($Matches[0]) with $versionAttribute" Write-Host " * Replaced $($Matches[0]) with $versionAttribute"
@ -46,8 +41,7 @@ function Replace-Version($content, $version, $attribute)
$_ $_
} }
if (-not $versionReplaced) if (-not $versionReplaced) {
{
$content += [Environment]::NewLine + $versionAttribute $content += [Environment]::NewLine + $versionAttribute
Write-Host " * Added $versionAttribute to end of content" Write-Host " * Added $versionAttribute to end of content"
} }
@ -55,15 +49,13 @@ function Replace-Version($content, $version, $attribute)
return $content return $content
} }
function Get-VersionString($numberOfVersions, $extractedBuildNumbers) function Get-VersionString($numberOfVersions, $extractedBuildNumbers) {
{ return [string]::Join('.',($extractedBuildNumbers | select -First ($numberOfVersions)))
return [string]::Join(".",($extractedBuildNumbers | select -First ($numberOfVersions)))
} }
$ErrorActionPreference = "Stop" $ErrorActionPreference = 'Stop'
if ($debugMode -eq "true") if ($debugMode -eq 'true') {
{
Write-Host "##[section]Starting: DEBUG INFORMATION" Write-Host "##[section]Starting: DEBUG INFORMATION"
Write-Host "##[debug]applicationName = $applicationName" Write-Host "##[debug]applicationName = $applicationName"
@ -79,16 +71,14 @@ if ($debugMode -eq "true")
Write-Host "" Write-Host ""
} }
if ($buildNumber -match $pattern -ne $true) if ($buildNumber -match $pattern -ne $true) {
{
Write-Host "Could not extract a version from [$buildNumber] using pattern [$pattern]" Write-Host "Could not extract a version from [$buildNumber] using pattern [$pattern]"
exit 2 exit 2
} }
# Set version variables # Set version variables
$extractedBuildNumbers = @($Matches[0].Split(([char[]]$patternSplitCharacters))) $extractedBuildNumbers = @($Matches[0].Split(([char[]]$patternSplitCharacters)))
if ($extractedBuildNumbers.Length -ne $patternExpectedVersionNumbers) if ($extractedBuildNumbers.Length -ne $patternExpectedVersionNumbers) {
{
Write-Host "The extracted build number $($Matches[0]) does not contain the expected $patternExpectedVersionNumbers elements" Write-Host "The extracted build number $($Matches[0]) does not contain the expected $patternExpectedVersionNumbers elements"
exit 2 exit 2
} }
@ -112,5 +102,5 @@ Get-ChildItem -Path $pathToSearch -Filter $searchFilter -Recurse | %{
$content | Set-Content $_.FullName -Encoding UTF8 $content | Set-Content $_.FullName -Encoding UTF8
} }
Write-Host "Done" Write-Host 'Done'
exit 0 exit 0

View file

@ -1 +1,94 @@
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 = "Basic $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