-
-
Save maslbl4/9ef1f25adb67361b07a2bcf736a38db8 to your computer and use it in GitHub Desktop.
Changes version information in AssemblyInfo.cs files recursively from a given path. I use this with TeamCity to control the version number as the built in Build Feature runs before my psake script can obtain the version (clearing out the version!).
This file contains 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
# SetAssemblyVersion.ps1 | |
# | |
# http://www.luisrocha.net/2009/11/setting-assembly-version-with-windows.html | |
# http://blogs.msdn.com/b/dotnetinterop/archive/2008/04/21/powershell-script-to-batch-update-assemblyinfo-cs-with-new-version.aspx | |
# http://jake.murzy.com/post/3099699807/how-to-update-assembly-version-numbers-with-teamcity | |
# https://github.com/ferventcoder/this.Log/blob/master/build.ps1#L6-L19 | |
Param( | |
[string]$path=$pwd | |
) | |
function Help { | |
"Sets the AssemblyVersion and AssemblyFileVersion of AssemblyInfo.cs files`n" | |
".\SetAssemblyVersion.ps1 [VersionNumber] -path [SearchPath]`n" | |
" [VersionNumber] The version number to set, for example: 1.1.9301.0" | |
" [SearchPath] The path to search for AssemblyInfo files.`n" | |
} | |
function Update-SourceVersion | |
{ | |
Param ([string]$Version) | |
$NewVersion = 'AssemblyVersion("' + $Version + '")'; | |
$NewFileVersion = 'AssemblyFileVersion("' + $Version + '")'; | |
foreach ($o in $input) | |
{ | |
Write-Host "Updating '$($o.FullName)' -> $Version" | |
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' | |
$fileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' | |
$assemblyVersion = 'AssemblyVersion("' + $version + '")'; | |
$fileVersion = 'AssemblyFileVersion("' + $version + '")'; | |
(Get-Content $o.FullName -encoding utf8) | ForEach-Object { | |
% {$_ -replace $assemblyVersionPattern, $assemblyVersion } | | |
% {$_ -replace $fileVersionPattern, $fileVersion } | |
} | Out-File $o.FullName -encoding UTF8 -force | |
} | |
} | |
function Update-AllAssemblyInfoFiles ( $version ) | |
{ | |
Write-Host "Searching '$path'" | |
foreach ($file in "AssemblyInfo.cs", "AssemblyInfo.vb" ) | |
{ | |
get-childitem $path -recurse |? {$_.Name -eq $file} | Update-SourceVersion $version ; | |
} | |
} | |
# validate arguments | |
if ($args -ne $null) { | |
$version = $args[0] | |
if (($version -eq '/?') -or ($version -notmatch "[0-9]+(\.([0-9]+|\*)){1,3}")) { | |
Help | |
exit 1; | |
} | |
} else { | |
Help | |
exit 1; | |
} | |
Update-AllAssemblyInfoFiles $version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment