Skip to content

Instantly share code, notes, and snippets.

@ElanHasson
Forked from bcnzer/SetCsProjVersion.ps1
Last active June 21, 2017 01:00
Show Gist options
  • Save ElanHasson/5bb3f323bc63845847208ce55779305b to your computer and use it in GitHub Desktop.
Save ElanHasson/5bb3f323bc63845847208ce55779305b to your computer and use it in GitHub Desktop.
Powershell script for generating a version number and inserting it into the csproj
param(
[Parameter(Mandatory = $true)]
[string] $csprojPath,
[Parameter(Mandatory = $true)]
[string] $newVersion
)
Write-Host "Starting process of generating new version number for the csproj"
foreach ($filePath in (Get-ChildItem -Path $csprojPath -Recurse)) {
Write-Host "Setting version of $filePath..."
$splitNumber = $newVersion.Split(".")
if ( $splitNumber.Count -eq 4 ) {
$majorNumber = $splitNumber[0]
$minorNumber = $splitNumber[1]
$revisionNumber = $splitNumber[3]
# I need to keep my build number under the 65K int limit, hence this hack of a method
$myBuildNumber = (Get-Date).Year + ((Get-Date).Month * 31) + (Get-Date).Day
$myBuildNumber = $majorNumber + "." + $minorNumber + "." + $myBuildNumber + "." + $revisionNumber
$xml = New-Object XML
$xml.Load($filePath)
$xml.Project.PropertyGroup.Version = $myBuildNumber
$xml.Save($filePath)
Write-Host "Updated csproj "$csprojPath" and set to version "$myBuildNumber
}
else {
Write-Host "ERROR: Something was wrong with the build number"
}
}
@ElanHasson
Copy link
Author

Added params and support for multiple projects to update.

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