Skip to content

Instantly share code, notes, and snippets.

@maxfunke
Created January 11, 2019 20:45
Show Gist options
  • Save maxfunke/a2b54f9a09aa2c3c2ce5881a333901f4 to your computer and use it in GitHub Desktop.
Save maxfunke/a2b54f9a09aa2c3c2ce5881a333901f4 to your computer and use it in GitHub Desktop.
changes version number in c# project files and SharedAssemblyInfo
<#
.SYNOPSIS
Update Software Version.
.DESCRIPTION
Finds SharedAssemblyInfo.cs in the solution and installer script in deployment directory.
Replaces code parts that define the software version in those files.
.EXAMPLE
"ChangeSoftwareVersion.ps1 -LocalPath . -Version 0.99.99"
.PARAMETER LocalPath
The path to the branch, with the installer script and SharedAssemblyInfo, which contain the software version.
.PARAMETER Version
The demanded software version. (Format: ^\d{1,3}\.\d{1,3}\.\d{1,3}\Z)
#>
Param(
[Parameter(Mandatory=$true, Position=0)][string] $LocalPath,
[Parameter(Mandatory=$true, Position=1)][string] $Version
)
# PARAMETER VALIDATION
if(!(test-path $LocalPath))
{
$localPathNotFoundEx = [System.Management.Automation.ItemNotFoundException] "The local path '${LocalPath}' is not found. Did you sync VCS to your local disk?";
Throw $localPathNotFoundEx;
exit 1;
}
$ValidVersionFormat = '^\d{1,3}\.\d{1,3}\.\d{1,3}\Z';
if(!($Version -match $ValidVersionFormat))
{
$versionFormatInvalidEx = New-Object System.FormatException "The demanded version does not match the required format: [d*.d*.d*]"
Throw $versionFormatInvalidEx;
exit 1;
}
# FILES WITH VERSION NUMBERS
$SharedAssemblyInfo = Get-ChildItem -Path $LocalPath -Filter "SharedAssemblyInfo.cs" -Recurse -Force;
$InstallerScript = Get-ChildItem -Path $LocalPath -Filter "Installer.iss" -Recurse -Force;
# REGEX FOR THOSE FILES
$SharedAssemblyRegex = 'Version\(\"\d{1,3}\.\d{1,3}\.\d{1,3}\"\)';
$InstallerScriptRegex = '[[:blank:]]*#define MyAppVersion \"\d{1,3}\.\d{1,3}\.\d{1,3}\"';
# EXECUTE VERSION CHANGE 1
Write-Output "========================";
$NewVersionString_SharedAssemblyInfo = "Version(`"$Version`")";
Write-Output "`$SharedAssemblyInfo.Fullname: $($SharedAssemblyInfo.Fullname)";
Write-Output "`$NewVersionString_SharedAssemblyInfo: $($NewVersionString_SharedAssemblyInfo)";
$Content = [System.IO.File]::ReadAllText($SharedAssemblyInfo.Fullname)
$NewContent = $Content -replace $SharedAssemblyRegex, $NewVersionString_SharedAssemblyInfo
$MatchingString = $Content | Select-String $SharedAssemblyRegex -AllMatches;
$MatchingString | ForEach-Object {$_.Matches} | ForEach-Object {$_.Value} {
"Replaced in: $($SharedAssemblyInfo.Fullname)" | Write-Host;
"old line: $($_)" | Write-Host;
"new line: $($NewVersionString_SharedAssemblyInfo)" | Write-Host;
}
[System.IO.File]::WriteAllText($SharedAssemblyInfo.Fullname, $NewContent)
# $NewContent | Write-Host;
# EXECUTE VERSION CHANGE 2
Write-Output "========================";
$NewVersionString_InstallerScript = " #define MyAppVersion `"$Version`""
Write-Output "`$InstallerScript.Fullname: $($InstallerScript.Fullname)";
Write-Output "`$NewVersionString_InstallerScript: $($NewVersionString_InstallerScript)";
$Content = [System.IO.File]::ReadAllText($InstallerScript.Fullname)
$NewContent = $Content -replace $InstallerScriptRegex, $NewVersionString_InstallerScript
$MatchingString = $Content | Select-String $InstallerScriptRegex -AllMatches;
$MatchingString | ForEach-Object {$_.Matches} | ForEach-Object {$_.Value} {
"Replaced in: $($InstallerScript.Fullname)" | Write-Host;
"old line: $($_)" | Write-Host;
"new line: $($NewVersionString_InstallerScript)" | Write-Host;
}
[System.IO.File]::WriteAllText($InstallerScript.Fullname, $NewContent)
# $NewContent | Write-Host;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment