.net Assembly Versioning using Powershell

[cmdletBinding()]
param
(
///[parameter(Mandatory =$true)]
$PathToProjectRoot,
[parameter(Mandatory =$true)]
$major,
[parameter(Mandatory =$true)]
$minor,
[parameter(Mandatory =$true)]
$svcpck,
[parameter(Mandatory =$true)]
$rev,
///[parameter(Mandatory =$true)]
$AssemblyInfoFileName

)
#Unit Testing data
<#$PathToProjectRoot = “C:\BambooBld\SVE-SVC-JOB1″
$major=”1″
$minor=”0″
$svcpck=”3″
$rev=”76”
$AssemblyInfoFileName = “GlobalAssemblyInfo”#>
$PathToProjectRoot = $PathToProjectRoot.Trim(“\”)

if (($major -eq $null) -or ($major -eq “”)) { throw “Major version CANNOT be blank!” }
if (($minor -eq $null) -or ($minor -eq “”)) { throw “Minor version CANNOT be blank!” }
if (($svcpck -eq $null) -or ($svcpck -eq “”)) { throw “Service Pack version CANNOT be blank!” }
if (($rev -eq $null) -or ($rev -eq “”)) { throw “Hotfix version CANNOT be blank!” }

$version = “$major.$minor.$svcpck.$rev”

$asseblyInfoList = Get-ChildItem $PathToProjectRoot -Include “$AssemblyInfoFileName.*” -Recurse

if ($asseblyInfoList.Count -eq 0)
{
throw “No $AssemblyInfoFileName Found”
}
else
{
foreach ($assemblyInfoFile in $asseblyInfoList)
{
write-output “Version Insertion for $assemblyInfoFile.FullName”
$assemblyInfoFile | Set-ItemProperty -Name IsReadonly -Value $false
$assemblyVersionPattern = ‘AssemblyVersion\(“[0-9]+(\.([0-9]+|\*)){1,3}”\)’
$fileVersionPattern = ‘AssemblyFileVersion\(“[0-9]+(\.([0-9]+|\*)){1,3}”\)’
$assemblyVersion = ‘AssemblyVersion(“‘ + $version + ‘”)’;
$fileVersion = ‘AssemblyFileVersion(“‘ + $version + ‘”)’;
(Get-Content -Path $assemblyInfoFile.FullName) | ForEach-Object { %{$_ -replace $assemblyVersionPattern, $assemblyVersion } | %{$_ -replace $fileVersionPattern,$fileVersion}} | Out-File $assemblyInfoFile.FullName -Encoding utf8 -Force
write-output “Printing content of $assemblyInfoFile.FullName”
Get-Content -Path $assemblyInfoFile.FullName
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *