Forked from derekgates/SetAssemblyInfoToVersion.ps1
Created
February 29, 2016 22:08
-
-
Save RSchwoerer/06d0d16c4f8a5f7e04f5 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 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
# SetAssemblyVersion.ps1 | |
# | |
# Sets the AssemblyVersion and AssemblyFile version attributes in assemblyinfo.cs or assemblyinfo.vb files | |
# for the specified path. | |
# | |
# from: https://gist.github.com/derekgates/4678882 | |
# | |
# 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( | |
[switch]$h=$false, | |
[string]$path=$pwd, | |
[ValidateScript({ | |
If ($_ -notmatch "[0-9]+(\.([0-9]+|\*)){1,3}") { | |
Throw "$_ is not a valid version number." | |
} Else {$True} | |
})] | |
[string]$version, | |
[ValidateScript({ | |
If ($_ -notmatch "[0-9]+(\.([0-9]+|\*)){1,3}") { | |
Throw "$_ is not a valid version number." | |
} Else {$True} | |
})] | |
[string]$fileversion="" | |
) | |
function Help { | |
"Sets the AssemblyVersion and AssemblyFileVersion of AssemblyInfo.cs files`n" | |
" > .\SetAssemblyVersion.ps1 -version [VersionNumber] -fileversion [FileVersionNumber] -path [SearchPath]" | |
" " | |
" [VersionNumber] The AssemblyVerion number to set, for example: 1.1.9301.0" | |
" [FileVersionNumber] The AssemblyFileVersion number to set." | |
" [SearchPath] The path to search for AssemblyInfo files." | |
" " | |
"Example > SetAssemblyVersion.ps1 -version 1.1.42 -fileversion 1.1.42.* -path ./ " | |
" " | |
} | |
function Update-SourceVersion | |
{ | |
Param ([string]$Version) | |
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' | |
$fileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' | |
$assemblyVersionValue = 'AssemblyVersion("' + $Version + '")' | |
$fileversion = if($fileversion) { $fileversion } else { $version } | |
$fileVersionValue = 'AssemblyFileVersion("' + $fileversion + '")' | |
foreach ($o in $input) | |
{ | |
Write-Host "Updating '$($o.FullName)' " | |
Set-ItemProperty $o.FullName -name IsReadOnly -value $false | |
(Get-Content $o.FullName) | ForEach-Object { | |
% {$_ -replace $assemblyVersionPattern, $assemblyVersionValue } | | |
% {$_ -replace $fileVersionPattern, $fileVersionValue } | |
} | 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 ; | |
} | |
Write-Host "done." | |
} | |
# --------------------------------------------------- | |
if($h) | |
{ | |
Help | |
exit 1 | |
} | |
Write-Host "Updating: path= '$path' version= '$version' fileversion= '$fileversion'" | |
Update-AllAssemblyInfoFiles $version | |
exit $LASTEXITCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment