Skip to content

Commit 6247cb4

Browse files
SyncFileContentsSyncFileContents
authored andcommitted
Sync scripts\PSBuild.psm1
1 parent 47c2c10 commit 6247cb4

1 file changed

Lines changed: 57 additions & 33 deletions

File tree

scripts/PSBuild.psm1

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ function Get-VersionNotes {
854854
if ($rangeFrom -ne "") {
855855
try {
856856
# Try to get the SHA for the from tag, but don't error if it doesn't exist
857-
$fromSha = "git rev-list -n 1 $rangeFrom 2>$null" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
857+
$fromSha = "git rev-list -n 1 $rangeFrom 2>`$null" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
858858
if ($LASTEXITCODE -ne 0) {
859859
Write-Information "Warning: Could not find SHA for tag $rangeFrom. Using fallback range." -Tags "Get-VersionNotes"
860860
$gitSuccess = $false
@@ -866,7 +866,7 @@ function Get-VersionNotes {
866866
$range = "$fromSha..$ToSha"
867867
} elseif ($gitSuccess) {
868868
# For already tagged versions, get the SHA for the to tag
869-
$toShaResolved = "git rev-list -n 1 $rangeTo 2>$null" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
869+
$toShaResolved = "git rev-list -n 1 $rangeTo 2>`$null" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
870870
if ($LASTEXITCODE -ne 0) {
871871
Write-Information "Warning: Could not find SHA for tag $rangeTo. Using fallback range." -Tags "Get-VersionNotes"
872872
$gitSuccess = $false
@@ -888,7 +888,7 @@ function Get-VersionNotes {
888888
$range = $ToSha
889889
} else {
890890
try {
891-
$toShaResolved = "git rev-list -n 1 $rangeTo 2>$null" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
891+
$toShaResolved = "git rev-list -n 1 $rangeTo 2>`$null" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
892892
if ($LASTEXITCODE -eq 0) {
893893
$range = $toShaResolved
894894
} else {
@@ -925,49 +925,33 @@ function Get-VersionNotes {
925925
# First try with standard filters
926926
$rawCommits = "git log --pretty=format:`"$format`" --perl-regexp --regexp-ignore-case --grep=`"$EXCLUDE_PRS`" --invert-grep --committer=`"$EXCLUDE_BOTS`" --author=`"$EXCLUDE_BOTS`" `"$range`"" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
927927

928-
# Convert to array if needed
929-
if ($null -eq $rawCommits) {
930-
$rawCommits = @()
931-
} elseif ($rawCommits -isnot [array]) {
932-
$rawCommits = @($rawCommits)
933-
}
928+
# Ensure $rawCommits is an array
929+
$rawCommits = ConvertTo-ArraySafe -InputObject $rawCommits
934930

935931
# If no commits found, try with just PR exclusion but no author filtering
936932
if ($rawCommits.Count -eq 0) {
937933
Write-Information "No commits found with standard filters, trying with relaxed author/committer filters..." -Tags "Get-VersionNotes"
938934
$rawCommits = "git log --pretty=format:`"$format`" --perl-regexp --regexp-ignore-case --grep=`"$EXCLUDE_PRS`" --invert-grep `"$range`"" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
939935

940-
# Convert to array if needed
941-
if ($null -eq $rawCommits) {
942-
$rawCommits = @()
943-
} elseif ($rawCommits -isnot [array]) {
944-
$rawCommits = @($rawCommits)
945-
}
936+
# Ensure $rawCommits is an array
937+
$rawCommits = ConvertTo-ArraySafe -InputObject $rawCommits
946938
}
947939

948940
# If still no commits, try with no filtering at all - show everything in the range
949941
if ($rawCommits.Count -eq 0) {
950942
Write-Information "Still no commits found, trying with no filters..." -Tags "Get-VersionNotes"
951943
$rawCommits = "git log --pretty=format:`"$format`" `"$range`"" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
952944

953-
# Convert to array if needed
954-
if ($null -eq $rawCommits) {
955-
$rawCommits = @()
956-
} elseif ($rawCommits -isnot [array]) {
957-
$rawCommits = @($rawCommits)
958-
}
945+
# Ensure $rawCommits is an array
946+
$rawCommits = ConvertTo-ArraySafe -InputObject $rawCommits
959947

960948
# If it's a prerelease version, include also version update commits
961949
if ($versionType -eq "prerelease" -and $rawCommits.Count -eq 0) {
962950
Write-Information "Looking for version update commits for prerelease..." -Tags "Get-VersionNotes"
963951
$rawCommits = "git log --pretty=format:`"$format`" --grep=`"Update VERSION to`" `"$range`"" | Invoke-ExpressionWithLogging -ErrorAction SilentlyContinue
964952

965-
# Convert to array if needed
966-
if ($null -eq $rawCommits) {
967-
$rawCommits = @()
968-
} elseif ($rawCommits -isnot [array]) {
969-
$rawCommits = @($rawCommits)
970-
}
953+
# Ensure $rawCommits is an array
954+
$rawCommits = ConvertTo-ArraySafe -InputObject $rawCommits
971955
}
972956
}
973957
}
@@ -1080,11 +1064,7 @@ function New-Changelog {
10801064
$changelog = ""
10811065

10821066
# Make sure tags is always an array
1083-
if ($null -eq $tags) {
1084-
$tags = @()
1085-
} elseif ($tags -isnot [array]) {
1086-
$tags = @($tags)
1087-
}
1067+
$tags = ConvertTo-ArraySafe -InputObject $tags
10881068

10891069
# Check if we have any tags at all
10901070
$hasTags = $tags.Count -gt 0
@@ -1921,6 +1901,49 @@ function Set-GitIdentity {
19211901
Assert-LastExitCode "Failed to configure git user email"
19221902
}
19231903

1904+
function ConvertTo-ArraySafe {
1905+
<#
1906+
.SYNOPSIS
1907+
Safely converts an object to an array, even if it's already an array, a single item, or null.
1908+
.DESCRIPTION
1909+
Ensures that the returned object is always an array, handling PowerShell's behavior
1910+
where single item arrays are automatically unwrapped.
1911+
.PARAMETER InputObject
1912+
The object to convert to an array.
1913+
.OUTPUTS
1914+
Returns an array, even if the input is null or a single item.
1915+
#>
1916+
[CmdletBinding()]
1917+
[OutputType([object[]])]
1918+
param (
1919+
[Parameter(ValueFromPipeline=$true)]
1920+
[object]$InputObject
1921+
)
1922+
1923+
begin {
1924+
$outputArray = @()
1925+
}
1926+
1927+
process {
1928+
if ($null -eq $InputObject) {
1929+
return @()
1930+
}
1931+
1932+
if ($InputObject -is [array]) {
1933+
# Force array context to handle single-item arrays
1934+
$outputArray = @($InputObject)
1935+
}
1936+
else {
1937+
# Single item, make it an array
1938+
$outputArray = @($InputObject)
1939+
}
1940+
}
1941+
1942+
end {
1943+
return $outputArray
1944+
}
1945+
}
1946+
19241947
#endregion
19251948

19261949
#region High-Level Workflows
@@ -2229,7 +2252,8 @@ Export-ModuleMember -Function Assert-LastExitCode,
22292252
Get-GitLineEnding,
22302253
Set-GitIdentity,
22312254
Write-InformationStream,
2232-
Invoke-ExpressionWithLogging
2255+
Invoke-ExpressionWithLogging,
2256+
ConvertTo-ArraySafe
22332257

22342258
# High-level workflow functions
22352259
Export-ModuleMember -Function Invoke-BuildWorkflow,

0 commit comments

Comments
 (0)