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,93 +2,83 @@ 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
{ $content | % {
$exitFunction = $false if ($_ -match 'exclude from semantic versioning') {
$content | %{ Write-Host " * Skipping $attribute due to exclude"
if ($_ -match 'exclude from semantic versioning') $exitFunction = $true
{ }
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"
if ($_ -match 'include semantic versioning' -and $_ -notmatch "include semantic versioning - $applicationName") $exitFunction = $true
{ }
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"
}
if ($exitFunction)
{
return $content 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) 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"
Write-Host "##[debug]pathToSearch = $pathToSearch" Write-Host "##[debug]pathToSearch = $pathToSearch"
Write-Host "##[debug]versionFile = $versionFile" Write-Host "##[debug]versionFile = $versionFile"
Write-Host "##[debug]pattern = $pattern" Write-Host "##[debug]pattern = $pattern"
Write-Host "##[debug]patternSplitCharacters = $patternSplitCharacters" Write-Host "##[debug]patternSplitCharacters = $patternSplitCharacters"
Write-Host "##[debug]patternExpectedVersionNumbers = $patternExpectedVersionNumbers" Write-Host "##[debug]patternExpectedVersionNumbers = $patternExpectedVersionNumbers"
Write-Host "##[debug]versionNumbersInVersion = $versionNumbersInVersion" Write-Host "##[debug]versionNumbersInVersion = $versionNumbersInVersion"
Write-Host "##[debug]searchFilter = $searchFilter" Write-Host "##[debug]searchFilter = $searchFilter"
Write-Host "##[section]Finishing: DEBUG INFORMATION" Write-Host "##[section]Finishing: DEBUG INFORMATION"
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
} }
@ -98,7 +88,7 @@ $fileVersion = Get-VersionString -numberOfVersions $versionNumbersInVersion -ext
Write-Host "Using version $version and file version $fileVersion" Write-Host "Using version $version and file version $fileVersion"
# iterate the search path (and sub directories) looking for files that match the search filter # iterate the search path (and sub directories) looking for files that match the search filter
Get-ChildItem -Path $pathToSearch -Filter $searchFilter -Recurse | %{ Get-ChildItem -Path $pathToSearch -Filter $searchFilter -Recurse | % {
Write-Host " -> Checking $($_.FullName)" Write-Host " -> Checking $($_.FullName)"
# remove the read-only bit on the file # remove the read-only bit on the file
@ -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