Last active
April 11, 2024 22:53
-
-
Save lowleveldesign/663de4e0d5a071f938e6f7c82d7ca9a0 to your computer and use it in GitHub Desktop.
A script to update version numbers in .NET builds, based on the tag name, date, and GitHub action run number. Creates unique version strings for each build.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = "Stop" | |
function Update-AssemblyInfoVersionFiles ([string]$versionIdentifier) | |
{ | |
$srcPath = $pwd | |
$today = [DateTime]::Today | |
$ghref = [System.IO.Path]::GetFileName("$env:GITHUB_REF") | |
if ($ghref -match "^(\d+\.\d+)(\-.+)?$") { | |
$buildNumber = "{0:yy}{1}.{2}" -f $today,$today.DayOfYear,($env:GITHUB_RUN_NUMBER % [int16]::MaxValue) | |
$version = "$($Matches[1]).$buildNumber" | |
} elseif ($ghref -match "^(\d+\.\d+\.\d+)(\-.+)?$") { | |
$buildNumber = $env:GITHUB_RUN_NUMBER % [int16]::MaxValue | |
$version = "$($Matches[1]).$buildNumber" | |
} else { | |
Write-Error "Invalid tag name for the release." | |
} | |
Write-Host "Updating $versionIdentifier in path $srcPath for version $version" | |
foreach ($file in $(Get-ChildItem -Path $srcPath -Include "*.fsproj","*.csproj","*.nuspec" -recurse)) | |
{ | |
$r = [regex]"<$versionIdentifier>([0-9]+\.[0-9]+)(\.([0-9]+|\*))+" | |
Write-Host "Processing '$($file.FullName)'" | |
#version replacements | |
(Get-Content -Encoding utf8 $file.FullName) | % { | |
$m = $r.Matches($_) | |
if ($m -and $m.Success) { | |
$s = $r.Replace($_, "<$versionIdentifier>$version") | |
Write-Host "Change version to $s" | |
$s | |
} else { | |
$_ | |
} | |
} | Set-Content -Encoding utf8 $file.FullName -Force | |
} | |
foreach ($file in $(Get-ChildItem -Path $srcPath -Include "AssemblyInfo.cs" -recurse)) | |
{ | |
$r = [regex]"$versionIdentifier\(`"([0-9]+\.[0-9]+)(\.([0-9]+|\*))+`"\)" | |
Write-Host "Processing '$($file.FullName)'" | |
#version replacements | |
(Get-Content -Encoding utf8 $file.FullName) | % { | |
$m = $r.Matches($_) | |
if ($m -and $m.Success) { | |
$s = $r.Replace($_, "$versionIdentifier(`"$version`")") | |
Write-Host "Change version to $s" | |
$s | |
} else { | |
$_ | |
} | |
} | Set-Content -Encoding utf8 $file.FullName -Force | |
} | |
} | |
Update-AssemblyInfoVersionFiles "FileVersion" -Verbose | |
Update-AssemblyInfoVersionFiles "AssemblyVersion" -Verbose | |
Update-AssemblyInfoVersionFiles "version" -Verbose | |
Update-AssemblyInfoVersionFiles "Version" -Verbose | |
Update-AssemblyInfoVersionFiles "PackageVersion" -Verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment