Skip to content

Instantly share code, notes, and snippets.

@aivascu
Last active August 29, 2015 14:24
Show Gist options
  • Save aivascu/399863c62d5653a5a0a1 to your computer and use it in GitHub Desktop.
Save aivascu/399863c62d5653a5a0a1 to your computer and use it in GitHub Desktop.
Updates assembly versions
# Usage: Update-Version.ps1 -Version [-Path] [-Verbose]
# Example: .\Update-Version.ps1 -Version "1.2.3.4" -Path ..\Path\To\Sln -Verbose
param(
[Parameter(Mandatory = $false)]
[string]$Path,
[Parameter(Mandatory = $true)]
[string]$Version)
function Update-AssemblyInfo(
[string]$FilePath = $(throw "FilePath is required"),
[string]$AssemblyVersion = $(throw "AssemblyVersion is required"),
[string]$AssemblyFileVersion = $(throw "AssemblyFileVersion is required"))
{
if(Test-Path $FilePath) {
$FileContent = Get-Content $FilePath |
%{$_ -replace '(?<=AssemblyVersion\(")[0-9]+(\.([0-9]+|\*)){1,3}(?=\"\))', $AssemblyVersion } |
%{$_ -replace '(?<=AssemblyFileVersion\(")[0-9]+(\.([0-9]+|\*)){1,3}(?=\"\))', $AssemblyFileVersion }
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($true)
$FullPath = Resolve-Path -Path $FilePath
[System.IO.File]::WriteAllLines($FullPath, $FileContent, $Utf8NoBomEncoding)
$VerboseMsg = "File updated {0}" -f $FilePath
Write-Verbose $VerboseMsg
}
}
function Update-Wxs(
[string]$FilePath = $(throw "FilePath is required"),
[string]$ProductVersion = $(throw "ProductVersion is required"))
{
if(Test-Path $FilePath) {
$UpgradeGuid = [System.Guid]::NewGuid();
$FileContent = Get-Content $FilePath |
%{$_ -replace '(?<=\<Product .* Version=\")[0-9]+(\.([0-9]+|\*)){1,3}(?=\")', $ProductVersion } |
%{$_ -replace '(?<=\<Product .* UpgradeCode=\")[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}(?=\")', $UpgradeGuid }
$Utf8Encoding = New-Object System.Text.UTF8Encoding($true)
$FullPath = Resolve-Path -Path $FilePath
[System.IO.File]::WriteAllLines($FullPath, $FileContent, $Utf8Encoding)
$VerboseMsg = "File updated {0}" -f $FilePath
Write-Verbose $VerboseMsg
}
}
if([string]::IsNullOrEmpty($Path))
{
$Path = ".\";
}
if([string]::IsNullOrEmpty($Version) -eq $false)
{
$Version | ? {$_ -match '(?<=.*)[0-9]+(\.([0-9]+|\*)){1,3}(?=.*)'} ; $Version = $matches[0]
}
else
{
$Version = '1.0.0.0';
}
$FolderList = Get-ChildItem -path $Path | Where-Object {$_.PsisContainer -eq "True"} | Select-Object name
foreach($Folder in $FolderList)
{
foreach($File in "AssemblyInfo.cs","AssemblyInfo.vb")
{
$FullFilePath = "{0}\{1}\Properties\{2}" -f $Path, $Folder.Name, $File
if(Test-Path $FullFilePath)
{
Update-AssemblyInfo -FilePath $FullFilePath -AssemblyVersion $Version -AssemblyFileVersion $Version
}
}
foreach($File in "Product.wxs")
{
$FullFilePath = "{0}\{1}\{2}" -f $Path, $Folder.Name, $File
if(Test-Path $FullFilePath)
{
Update-Wxs -FilePath $FullFilePath -ProductVersion $Version
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment