Skip to content

Instantly share code, notes, and snippets.

@robhruska
Created April 26, 2016 16:02
Show Gist options
  • Save robhruska/0fe4a3f48cc48b67a3dbe36371f0525f to your computer and use it in GitHub Desktop.
Save robhruska/0fe4a3f48cc48b67a3dbe36371f0525f to your computer and use it in GitHub Desktop.
# Changes the existing project.json "version" into a prerelease string for a given branch
# name and build number. Prerelease strings are truncated to < 20 characters to work within
# NuGet length restrictions. Non-alphanumeric characters are sanitized from the branch name.
# Accommodates build numbers up to 5 digits.
#
# Examples:
#
# Version: 1.5.0
# Branch: Foo
# BuildNumber: 123
# NewVersion: 1.5.0-Foo123
#
# (Existing prerelease strings in the version field are preserved)
# Version: 1.5.0-beta001
# Branch: Foo
# BuildNumber: 123
# NewVersion: 1.5.0-beta001Foo123
#
# (Prerelease strings are truncated to < 20 characters)
# Version: 1.5.0-beta001
# Branch: MyReallyLongBranchName
# BuildNumber: 12345
# NewVersion: 1.5.0-beta001MyReally12345
# Usage:
# SetPrereleaseVersionInProjectJson.ps1 <branch-name> <build-number>
if ($args.Length -ne 2) {
throw [ArgumentException] "Usage: SetPrereleaseVersionInProjectJson.ps1 <branch-name> <build-number>"
}
$Branch = $args[0]
$BuildNumber = $args[1]
if ($Branch -eq "master") {
"Will not append prerelease version for master branch" | Write-Output
exit 0
}
$ProjectJsonFile = ".\project.json"
$JsonIn = (Get-Content $ProjectJsonFile) -join "`n" | ConvertFrom-Json
# NuGet has a 20-character limit on version length. We add 5 here to accommodate the build
# number that we're going to append to the prerelease version. We'll account for that limit
# when combining the version and prerelease here.
# https://nuget.codeplex.com/workitem/3426
$ParsedVersion = ($JsonIn.version | Select-String '(\d+\.\d+\.\d+)(-?)(.*)' | Select -Expand Matches)
"Building prerelease string from Branch={0} BuildNumber={1} ParsedVersion={2}" -f $Branch , $BuildNumber , $ParsedVersion.Groups[0].Value | Write-Output
# Groups[3] will be any existing prerelease that's hard-coded into the project.json. We'll
# keep that in the new version string we build (with the branch name and build number), but
# will have to subtract its length.
$VersionLength = $ParsedVersion.Groups[3].Length
$MaxBranchNameLength = (20 - ($VersionLength + 5))
# Remove any extraneous special characters from the branch. We just want an alphanumeric prerelease string.
$SanitizedBranch = ("${Branch}" -replace '[^a-zA-Z0-9]', '')
$SanitizedBranch = ($SanitizedBranch.Substring(0, [math]::min($MaxBranchNameLength, $SanitizedBranch.Length)) + $BuildNumber)
$NewVersion = "{0}-{1}{2}" -f $ParsedVersion.Groups[1].Value , $ParsedVersion.Groups[3].Value , $SanitizedBranch
"Built prerelease string NewVersion={0} VersionLength={1} MaxBranchNameLength={2} SanitizedBranch={3}" -f $NewVersion , $VersionLength , $MaxBranchNameLength , $SanitizedBranch | Write-Output
$JsonIn.version = $NewVersion
$JsonOut = ConvertTo-Json $JsonIn -Depth 999
"Writing JSON to {0}:`n{1}" -f $ProjectJsonFile , $JsonOut | Write-Output
$JsonOut | Out-File $ProjectJsonFile -Encoding "UTF8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment