Skip to content

Instantly share code, notes, and snippets.

@EricRohlfs
Created October 28, 2016 17:20
Show Gist options
  • Save EricRohlfs/6a1f4e0a4ff294d3e2c3415383f633c3 to your computer and use it in GitHub Desktop.
Save EricRohlfs/6a1f4e0a4ff294d3e2c3415383f633c3 to your computer and use it in GitHub Desktop.
# not really complete, but does keep the first few digits in a sharedassembly.cs file script.
##-----------------------------------------------------------------------
## <copyright file="ApplyVersionToAssemblies.ps1">(c) http://TfsBuildExtensions.codeplex.com/. This source is subject to the Microsoft Permissive License. See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx. All other rights reserved.</copyright>
##-----------------------------------------------------------------------
# Look for a 0.0.0.0 pattern in the build number.
# If found use it to version the assemblies.
#
# For example, if the 'Build number format' build process parameter
# $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
# then your build numbers come out like this:
# "Build HelloWorld_2013.07.19.1"
# This script would then apply version 2013.07.19.1 to your assemblies.
# Enable -Verbose option
[CmdletBinding()]
# Disable parameter
# Convenience option so you can debug this script or disable it in
# your build definition without having to remove it from
# the 'Post-build script path' build process parameter.
param([switch]$Disable)
if ($PSBoundParameters.ContainsKey('Disable'))
{
Write-Verbose "Script disabled; no actions will be taken on the files."
}
# Regular expression pattern to find the version in the build number
# and then apply it to the assemblies
$VersionRegex = "\d+\.\d+\.\d+\.\d+"
# 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:TF_BUILD -and -not ($Env:TF_BUILD_SOURCESDIRECTORY -and $Env:TF_BUILD_BUILDNUMBER))
{
Write-Error "You must set the following environment variables"
Write-Error "to test this script interactively."
Write-Host '$Env:TF_BUILD_SOURCESDIRECTORY - For example, enter something like:'
Write-Host '$Env:TF_BUILD_SOURCESDIRECTORY = "C:\code\FabrikamTFVC\HelloWorld"'
Write-Host '$Env:TF_BUILD_BUILDNUMBER - For example, enter something like:'
Write-Host '$Env:TF_BUILD_BUILDNUMBER = "Build HelloWorld_0000.00.00.0"'
exit 1
}
# Make sure path to source code directory is available
if (-not $Env:TF_BUILD_SOURCESDIRECTORY)
{
Write-Error ("TF_BUILD_SOURCESDIRECTORY environment variable is missing.")
exit 1
}
elseif (-not (Test-Path $Env:TF_BUILD_SOURCESDIRECTORY))
{
Write-Error "TF_BUILD_SOURCESDIRECTORY does not exist: $Env:TF_BUILD_SOURCESDIRECTORY"
exit 1
}
Write-Verbose "TF_BUILD_SOURCESDIRECTORY: $Env:TF_BUILD_SOURCESDIRECTORY"
# Make sure there is a build number
if (-not $Env:TF_BUILD_BUILDNUMBER)
{
Write-Error ("TF_BUILD_BUILDNUMBER environment variable is missing.")
exit 1
}
Write-Verbose "TF_BUILD_BUILDNUMBER: $Env:TF_BUILD_BUILDNUMBER"
# Get and validate the version data
# $VersionData = [regex]::matches($Env:TF_BUILD_BUILDNUMBER,$VersionRegex)
# switch($VersionData.Count)
#{
# 0
# {
# Write-Error "Could not find version number data in TF_BUILD_BUILDNUMBER."
# exit 1
# }
# 1 {}
# default
# {
# Write-Warning "Found more than instance of version data in TF_BUILD_BUILDNUMBER."
# Write-Warning "Will assume first instance is version."
# }
#}
$NewVersion = $VersionData[0]
Write-Verbose "Version: $NewVersion"
# Apply the version to the assembly property files
$files = gci $Env:TF_BUILD_SOURCESDIRECTORY -recurse -include "*" |
?{ $_.PSIsContainer } |
foreach { gci -Path $_.FullName -Recurse -include "SharedAssemblyInfo.*" }
if($files)
{
Write-Verbose "Will apply $NewVersion to $($files.count) files."
foreach ($file in $files) {
if(-not $Disable)
{
$filecontent = Get-Content($file)
attrib $file -r
$matches = [regex]::matches($filecontent,$VersionRegex)
$vparts = $matches[0].value.Split('.')
$newNumber = "$($vparts[0]).$($vparts[1]).$Env:TF_BUILD_BUILDNUMBER.$($vparts[3])"
write $newNumber
$filecontent -replace $VersionRegex, $newNumber | Out-File $file
Write-Verbose "$file.FullName - version applied"
}
}
}
else
{
Write-Warning "Found no files."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment