Created
January 17, 2011 23:36
-
-
Save jstangroome/783707 to your computer and use it in GitHub Desktop.
Update build definition paths by branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 2.0 | |
[CmdletBinding()] | |
param ( | |
[parameter(Mandatory=$true)] | |
[string] | |
$BuildDefinitionName, | |
[parameter(Mandatory=$true)] | |
[string] | |
$ProjectName) | |
$ErrorActionPreference = 'Stop' | |
Set-StrictMode -Version Latest | |
Add-Type -AssemblyName 'Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a' | |
Add-Type -AssemblyName 'Microsoft.TeamFoundation.Build.Client, Version=10.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a' | |
$Collection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList | |
$BuildServer = $Collection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]) | |
if ($BuildDefinitionName.Contains('*')) { | |
$BuildServer.QueryBuildDefinitions($ProjectName) | | |
Where-Object { | |
$_.Name -like $BuildDefinitionName -and | |
$_.Name -match $BranchNamePattern | |
} | | |
ForEach-Object { | |
& $MyInvocation.MyCommand.Path $_.Name $ProjectName | |
} | |
return | |
} | |
$Definition = $BuildServer.GetBuildDefinition($ProjectName, $BuildDefinitionName) | |
$BranchNamePattern = '^(?<branch>[^_]+)_' | |
if ($Definition.Name -match $BranchNamePattern) { | |
$BranchName = $Matches.branch | |
} else { | |
throw "Build definition name '{0}' does not match expected '<branch>_' pattern." -f $Definition.Name | |
} | |
$BranchRoot = '$/{0}/Branches/{1}' -f $ProjectName, $BranchName | |
if ($BranchName -eq 'Trunk') { | |
$BranchRoot = '$/{0}/{1}' -f $ProjectName, $BranchName | |
} | |
$BranchRootReplacePattern = '^\$/{0}/(?:Trunk|Branches/{1})' -f [Regex]::Escape($ProjectName), [Regex]::Escape($BranchName) | |
"Using branch root '$BranchRoot' for build '$BuildDefinitionName'." | |
$Dirty = $false | |
$Definition.Workspace.Mappings | | |
Where-Object { | |
$_.ServerItem -match $BranchRootReplacePattern -and | |
-not $_.ServerItem.StartsWith($BranchRoot) | |
} | | |
ForEach-Object { | |
$Old = $_.ServerItem | |
$_.ServerItem = $Old -replace $BranchRootReplacePattern, $BranchRoot | |
"Changed workspace mapping '$Old' to '$($_.ServerItem)'." | |
$Dirty = $true | |
} | |
if ($Definition.Process.ServerPath -match $BranchRootReplacePattern -and | |
-not $Definition.Process.ServerPath.StartsWith($BranchRoot)) { | |
$Old = $Definition.Process.ServerPath | |
$New = $Old -replace $BranchRootReplacePattern, $BranchRoot | |
$Templates = $BuildServer.QueryProcessTemplates($ProjectName) | |
$Template = $Templates | | |
Where-Object { $_.ServerPath -eq $New } | | |
Select-Object -First 1 | |
if (-not $Template) { | |
$Template = $BuildServer.CreateProcessTemplate($ProjectName, $New) | |
$Template.Save() | |
} | |
$Definition.Process = $Template | |
"Changed process template '$Old' to '$New'." | |
$Dirty = $true | |
} | |
$Parameters = [xml]$Definition.ProcessParameters | |
$Old = $Parameters.Dictionary.BuildSettings.ProjectsToBuild | |
$Projects = $Old -split ',' | | |
ForEach-Object { | |
$_ -replace $BranchRootReplacePattern, $BranchRoot | |
} | |
$New = [string]::Join(',', @($Projects)) | |
if ($Old -ne $New) { | |
$Parameters.Dictionary.BuildSettings.ProjectsToBuild = $New | |
$Definition.ProcessParameters = $Parameters.InnerXml | |
"Changed projects to build '$Old' to '$New'." | |
$Dirty = $true | |
} | |
if ($Dirty) { | |
$Definition.Save() | |
'Saved.' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment