Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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!).
# 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) | 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
@dan-turner

This comment has been minimized.

Show comment
Hide comment
@dan-turner

dan-turner May 8, 2015

G'day mate, great job with this, I do have one suggestion though. I noticed in the diff after running this script that an extra random "Â" character appears before the "©" copyright symbol. The fix was to also add -encoding UTF8 to the Get-Content call. Now it works perfectly :)

G'day mate, great job with this, I do have one suggestion though. I noticed in the diff after running this script that an extra random "Â" character appears before the "©" copyright symbol. The fix was to also add -encoding UTF8 to the Get-Content call. Now it works perfectly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment