Team Foundation Server has a great API and you can use it to extend its capabilities or automatize tasks and so on. This API can be also used from PowerShell, which demonstrates this post. You have to know only how to use .NET from PowerShell. Following code uses TFS API to get count of new lines of code added in specified changeset:
1: ################################################################################
2: #
3: # Get-AddedLinesCountInChangeset
4: #
5: ################################################################################
6: 7: param
8: (9: [string] $tfsUrl = $(throw "TFS URL is required"),
10: [int] $changeSetId = $(throw "Changeset ID is required")
11: ) 12: 13: [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
14: [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
15: 16: function LinesCountInFile($item, $version)
17: { 18: $tempFile = [System.IO.Path]::GetTempFileName() 19: 20: $itemVersion = $vcs.GetItem($item.ServerItem, $version) 21: $itemVersion.DownloadFile($tempFile) 22: $linesCount = (Get-Content $tempFile | Measure-Object).Count 23: Remove-Item -Force $tempFile 24: 25: $linesCount 26: } 27: 28: function ProcessChange($change)
29: {30: if($change.Item.ItemType -eq "Folder")
31: {32: return $False
33: } 34: 35: $extensions = ".cs", ".aspx", ".ascx", ".xml", ".xaml", ".config"
36: 37: foreach ($extension in $extensions)
38: {39: if ($change.Item.ServerItem.EndsWith($extension))
40: {41: return $True
42: } 43: } 44: 45: return $False
46: } 47: 48: $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsUrl) 49: $vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) 50: 51: $thisVersionSpec = New-Object Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec $changeSetId 52: $previousVersionSpec = New-Object Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec ($changeSetId - 1) 53: 54: $chs = $vcs.GetChangeset($changeSetId) 55: $totalLinesCount = 0 56: Write-Host 57: 58: foreach ($change in $chs.Changes)
59: {60: if (ProcessChange($change))
61: {62: $nameParts = $change.Item.ServerItem.Split("/")
63: $fileName = $nameParts[$nameParts.Length - 1] 64: 65: $thisLength = LinesCountInFile $change.Item $thisVersionSpec 66: 67: if ($change.ChangeType.ToString().Contains("Add"))
68: { 69: $previousLength = 0 70: }71: else
72: { 73: $previousLength = LinesCountInFile $change.Item $previousVersionSpec 74: } 75: 76: $added = $thisLength - $previousLength 77: 78: Write-Host "$fileName $added" -Fore Yellow
79: if ($added -gt 0) { $totalLinesCount = $totalLinesCount + $added }
80: } 81: } 82: 83: Write-Host "------------------------------------------------------"
84: Write-Host "Total Lines: $totalLinesCount" -Fore Green
85: Write-HostIf you use TFS 2005/2008 then specify only server url, when TFS 2010 then specify url including the collection.

![]() | ![]() |







Žiadne komentáre:
Zverejnenie komentára