Skip to content

Instantly share code, notes, and snippets.

@scottdurow
Created April 28, 2021 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottdurow/fe844a012c388f2e3e8bafdf4a2d897d to your computer and use it in GitHub Desktop.
Save scottdurow/fe844a012c388f2e3e8bafdf4a2d897d to your computer and use it in GitHub Desktop.
Update Solution Version PowerShell
$VersionRegex = "\d+\.\d+\.(\d+\.\d+)"
# Apply the version to the solution
$NewVersion = "1.0.$($VersionData[0].Groups[1])"
Write-Output "Solution Version: $NewVersion"
$VersionRegexProject = "<Version>\d+\.\d+.\d+.\d+</Version>"
$file = "$($Env:BUILD_SOURCESDIRECTORY)\SolutionPackage\package\Other\Solution.xml"
Write-Output $file
$filecontent = Get-Content($file)
attrib $file -r
$filecontent -replace $VersionRegexProject, "<Version>$NewVersion</Version>" | Out-File $file -Encoding ascii
Write-Verbose "$($file) - Solution version applied"
# Apply the version to PCF Manifest
$NewVersion = "0.$($VersionData[0].Groups[1])"
Write-Output "PCF Version: $NewVersion"
$VersionRegexProject = "version=""\d+\.\d+\.\d+"""
$file = "$($Env:BUILD_SOURCESDIRECTORY)\PCF\ControlManifest.Input.xml"
Write-Output $file
$filecontent = Get-Content($file)
attrib $file -r
$filecontent -replace $VersionRegexProject, "version=""$NewVersion""" | Out-File $file -Encoding ascii
Write-Verbose "$($file) - PCF version applied"
# Apply the version to the assembly property files
$NewVersion = "1.0.$($VersionData[0].Groups[1])"
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
Write-Output "Plugin Vesion: $NewVersion"
$VersionRegexProject = "AssemblyVersion\(""\d+\.\d+\.\d+\.\d+""\)"
$file = "$($Env:BUILD_SOURCESDIRECTORY)\Plugins\Properties\AssemblyInfo.cs"
Write "$($file)"
$filecontent = Get-Content($file)
attrib $file -r
$filecontent -replace $VersionRegexProject, "AssemblyVersion(""$NewVersion"")" | Out-File $file
Write-Verbose "$($file) - Plugin version applied"
@daryllabar
Copy link

I got errors with this. Ended up adding some additional logging and error checking, and parametrizing the solution and plugin path. Haven't gotten around to needing the PCF yet, so that's also commented out. Also not sure how to handle the build number. 1.0.20210429.10 is invalid because '20210429' is too big for the plugin to build, so I've substringed it to 'YYMM'.

param($solutionPath, $pluginPath)
#$env:BUILD_BUILDNUMBER = '20210429.10'
#$env:BUILD_SOURCESDIRECTORY = 'C:\_dev\Acme\Dataverse'
$versionRegex = "\d+\.\d+(\.\d+\.\d+)*"
$solutionFileVersionRegex = "<Version>\d+\.\d+.\d+.\d+</Version>"
$projectVersionRegex = "AssemblyVersion\(""\d+\.\d+\.\d+\.\d+""\)"

if (-not $solutionPath)
{
	Write-Error ("SolutionPath paramter is missing!")
	exit 1
}

if (-not $pluginPath)
{
	Write-Error ("PluginPath paramter is missing!")
	exit 1
}

# If this script is not running on a build server, remind user to 
# set environment variables so that this script can be debugged
if(-not ($Env:BUILD_SOURCESDIRECTORY -and $Env:BUILD_BUILDNUMBER))
{
	Write-Error "You must set the following environment variables"
	Write-Error "to test this script interactively."
	Write-Error 'For example, enter something like:'
	Write-Error '$Env:BUILD_SOURCESDIRECTORY = "C:\code\FabrikamTFVC\HelloWorld"'
	Write-Error 'For example, enter something like:'
	Write-Error '$Env:BUILD_BUILDNUMBER = "Build HelloWorld_0000.00.00.0"'
	exit 1
}
	
# Make sure path to source code directory is available
if (-not $Env:BUILD_SOURCESDIRECTORY)
{
	Write-Error ("BUILD_SOURCESDIRECTORY environment variable is missing.")
	exit 1
}
elseif (-not (Test-Path $Env:BUILD_SOURCESDIRECTORY))
{
	Write-Error "BUILD_SOURCESDIRECTORY does not exist: $Env:BUILD_SOURCESDIRECTORY"
	exit 1
}
Write-Verbose "BUILD_SOURCESDIRECTORY: $Env:BUILD_SOURCESDIRECTORY"
	
# Make sure there is a build number
if (-not $Env:BUILD_BUILDNUMBER)
{
	Write-Error ("BUILD_BUILDNUMBER environment variable is missing.")
	exit 1
}
Write-Verbose "BUILD_BUILDNUMBER: $Env:BUILD_BUILDNUMBER"
	
# Get and validate the version data
$Version = [regex]::matches($Env:BUILD_BUILDNUMBER,$versionRegex)[0].Value 
Write-Verbose "Version: $Version"
$Version = ($Version).substring(2,4) + "." + ($Version).split('.')[1]
Write-Verbose "Formatted Version: $Version"

# Apply the version to the solution
$file = "$($Env:BUILD_SOURCESDIRECTORY)\$solutionPath" 
Write-Output "Loading File: $file"
$filecontent = Get-Content($file)
$currentFileVersion = [regex]::matches($filecontent,$solutionFileVersionRegex)[0].Value 
$newVersion = $currentFileVersion -replace "\d+\.\d+</Version>", "$Version</Version>"
Write-Output "Updating solution version from: $currentFileVersion to: $newVersion"
attrib $file -r
$filecontent -replace $solutionFileVersionRegex, $newVersion | Out-File $file -Encoding ascii
Write-Output "$($file) - Solution version applied"

# Apply the version to PCF Manifest
#$newVersion = "0.$($VersionData[0].Groups[1])"
#Write-Output "PCF Version: $newVersion"
#$solutionFileVersionRegex = "version=""\d+\.\d+\.\d+"""
#$file = "$($Env:BUILD_SOURCESDIRECTORY)\PCF\ControlManifest.Input.xml" 
#Write-Output $file
#$filecontent = Get-Content($file)
#attrib $file -r
#$filecontent -replace $solutionFileVersionRegex, "version=""$newVersion""" | Out-File $file -Encoding ascii
#Write-Verbose "$($file) - PCF version applied"

# Apply the version to the assembly property files
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$file = "$($Env:BUILD_SOURCESDIRECTORY)\$pluginPath\Properties\AssemblyInfo.cs" 
Write-Output "Loading File: $file"
$filecontent = Get-Content($file)
$currentFileVersion = [regex]::matches($filecontent, $projectVersionRegex)[0].Value 
$newVersion = $currentFileVersion -replace "\d+\.\d+""\)", "$Version"")"
Write-Output "Updating plugin assembly version from: $currentFileVersion to: $newVersion"
attrib $file -r
$filecontent -replace $projectVersionRegex, $newVersion | Out-File $file
Write-Output "$($file) - Plugin version applied"

@scottdurow
Copy link
Author

I am guessing because you’ve named your build using year month day format? I use semantic versioning - $(SourceBranchName)_$(majorVersion).$(minorVersion).$(BuildId)$(Rev:.r)

@daryllabar
Copy link

I didn't touch it, so yes? Any benefit to your versioning?

@scottdurow
Copy link
Author

Easier to read imho

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