Last active
April 14, 2021 20:07
-
-
Save derekgates/4678882 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) | 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
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 theGet-Content
call. Now it works perfectly :)