-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
vsts-tfvc-shared.psm1
309 lines (259 loc) · 11 KB
/
vsts-tfvc-shared.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import-module "Microsoft.TeamFoundation.DistributedTask.Task.Internal"
import-module "Microsoft.TeamFoundation.DistributedTask.Task.Common"
Write-Warning ("Version 1.x of this task is deprecated. Please update to 2.x before logging any issues.")
function Load-Assembly {
[cmdletbinding()]
param(
[string] $name,
[array] $ProbingPaths = @()
)
if ($ProbingPaths.Length -eq 0)
{
Write-Debug "Setting default assembly locations"
if ($env:AGENT_HOMEDIRECTORY -ne $null )
{
$ProbingPaths += (Join-Path $env:AGENT_HOMEDIRECTORY "\Agent\Worker\")
}
if ($env:AGENT_SERVEROMDIRECTORY -ne $null)
{
$ProbingPaths += $env:AGENT_SERVEROMDIRECTORY
}
$VS1464Path = (Get-ItemProperty -LiteralPath "HKLM:\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0" -Name 'ShellFolder' -ErrorAction Ignore).ShellFolder
if ($VS1464Path -ne $null)
{
$ProbingPaths += (Join-Path $VS1464Path "\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\")
}
$VS1432Path = (Get-ItemProperty -LiteralPath "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0" -Name 'ShellFolder' -ErrorAction Ignore).ShellFolder
if ($VS1432Path -ne $null)
{
$ProbingPaths += (Join-Path $VS1432Path "\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\")
}
}
foreach($a in [System.AppDomain]::CurrentDomain.GetAssemblies())
{
if ($a.Name -eq $Name)
{
return $a
}
}
$assemblyToLoad = New-Object System.Reflection.AssemblyName $name
foreach ($path in $ProbingPaths)
{
$path = [System.IO.Path]::Combine($path, "$($assemblyToLoad.Name).dll")
if (Test-Path -PathType Leaf -LiteralPath $path)
{
if ([System.Reflection.AssemblyName]::GetAssemblyName($path).Name -eq $assemblyToLoad.Name)
{
Write-Debug "Loading assembly: $path"
$assembly = [System.Reflection.Assembly]::LoadFrom($path)
return;
}
}
}
throw "Could not load assembly: $Name"
}
Load-Assembly "Microsoft.TeamFoundation.Client"
Load-Assembly "Microsoft.TeamFoundation.Common"
Load-Assembly "Microsoft.TeamFoundation.VersionControl.Client"
Load-Assembly "Microsoft.TeamFoundation.WorkItemTracking.Client"
Load-Assembly "Microsoft.TeamFoundation.Diff"
$OnNonFatalError = [Microsoft.TeamFoundation.VersionControl.Client.ExceptionEventHandler] {
param($sender, $e)
if ($e.Exception -ne $null -and $e.Exception.Message -ne $null)
{
Write-Warning $e.Exception.Message
}
if ($e.Failure -ne $null -and $e.Failure.Message -ne $null)
{
Write-Warning $e.Failure.Message
if ($e.Failure.Warnings -ne $null -and $e.Failure.Warnings.Length -gt 0)
{
foreach ($warning in $e.Failure.Warnings)
{
Write-Warning $warning.ParentOrChildTask
}
}
}
}
function Get-SourceProvider {
[cmdletbinding()]
param()
Write-Debug "Entering Get-SourceProvider"
$provider = @{
Name = $env:BUILD_REPOSITORY_PROVIDER
SourcesRootPath = $env:BUILD_SOURCESDIRECTORY
TeamProjectId = $env:SYSTEM_TEAMPROJECTID
}
$success = $false
try {
if ($provider.Name -eq 'TfsVersionControl') {
$RepositoryName = $env:BUILD_REPOSITORY_NAME
if ($env:BUILD_REPOSITORY_NAME -eq '')
{
$RepositoryName = $env:SYSTEM_TEAMPROJECT
}
$serviceEndpoint = Get-ServiceEndpoint -Context $distributedTaskContext -Name $RepositoryName
$tfsClientCredentials = Get-TfsClientCredentials -ServiceEndpoint $serviceEndpoint
$provider.TfsTeamProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(
$serviceEndpoint.Url,
$tfsClientCredentials)
$versionControlServer = $provider.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$versionControlServer.add_NonFatalError($OnNonFatalError)
$workstation = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current
$workstation.EnsureUpdateWorkspaceInfoCache($versionControlServer, $versionControlServer.AuthorizedUser)
$provider.VersionControlServer = $versionControlServer;
$provider.Workspace = $versionControlServer.TryGetWorkspace($provider.SourcesRootPath)
if (!$provider.Workspace) {
Write-Verbose "Unable to determine workspace from source folder: $($provider.SourcesRootPath)"
Write-Verbose "Attempting to resolve workspace recursively from locally cached info."
$workspaceInfos = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfoRecursively($provider.SourcesRootPath);
if ($workspaceInfos) {
foreach ($workspaceInfo in $workspaceInfos) {
Write-Verbose "Cached workspace info discovered. Server URI: $($workspaceInfo.ServerUri) ; Name: $($workspaceInfo.Name) ; Owner Name: $($workspaceInfo.OwnerName)"
try {
$provider.Workspace = $versionControlServer.GetWorkspace($workspaceInfo)
break
} catch {
Write-Verbose "Determination failed. Exception: $_"
}
}
}
}
if ((!$provider.Workspace) -and $env:BUILD_REPOSITORY_TFVC_WORKSPACE) {
Write-Verbose "Attempting to resolve workspace by name: $env:BUILD_REPOSITORY_TFVC_WORKSPACE"
try {
$provider.Workspace = $versionControlServer.GetWorkspace($env:BUILD_REPOSITORY_TFVC_WORKSPACE, '.')
} catch [Microsoft.TeamFoundation.VersionControl.Client.WorkspaceNotFoundException] {
Write-Verbose "Workspace not found."
} catch {
Write-Verbose "Determination failed. Exception: $_"
}
}
if (!$provider.Workspace) {
Write-Warning ("Unable to determine workspace from source folder $($provider.SourcesRootPath).")
return
}
if ($provider.Workspace.Location -eq "Server")
{
Write-Warning "Server workspace support is experimental."
}
$provider.Workspace.Refresh()
$success = $true
return New-Object psobject -Property $provider
}
Write-Warning ("Only TfsVersionControl source providers are supported for TFVC tasks. Repository type: $provider")
return
} finally {
if (!$success) {
Invoke-DisposeSourceProvider -Provider $provider
}
Write-Debug "Leaving Get-SourceProvider"
}
}
function Invoke-DisposeSourceProvider {
[cmdletbinding()]
param($Provider)
Write-Debug "Entering Invoke-DisposeSourceProvider"
if ($Provider)
{
if ($Provider.VersionControlServer)
{
$Provider.VersionControlServer.remove_NonFatalError($OnNonFatalError)
}
if ($Provider.TfsTeamProjectCollection) {
Write-Verbose 'Disposing tfsTeamProjectCollection'
$Provider.TfsTeamProjectCollection.Dispose()
$Provider.TfsTeamProjectCollection = $null
}
}
Write-Debug "Leaving Invoke-DisposeSourceProvider"
}
function Detect-WorkspaceChanges {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
$Provider,
[Parameter(Mandatory=$true)]
[array] $Items,
[Parameter(Mandatory=$true)]
[Microsoft.TeamFoundation.VersionControl.Client.RecursionType] $RecursionType,
[Microsoft.TeamFoundation.VersionControl.Client.ChangeType] $ChangeType
)
Write-Debug "Entering Detect-WorkspaceChanges"
try
{
$AllWorkspaceChanges = @()
$ItemSpecs = @( Convert-ToItemSpecs -Paths $Items -RecursionType $RecursionType )
$provider.Workspace.Refresh()
$CurrentPendingChanges = $provider.Workspace.GetPendingChanges([Microsoft.TeamFoundation.VersionControl.Client.ItemSpec[]]$ItemSpecs, $false)
$workspaceChanges = $provider.Workspace.GetPendingChangesWithCandidates($ItemSpecs, $false, [ref] $AllWorkspaceChanges)
$detectedChanges = $AllWorkspaceChanges | Where-Object { $CurrentPendingChanges.ServerItem -notcontains $_.ServerItem } | Where-Object { (($_.ChangeType -band $ChangeType) -eq $ChangeType) -and $_.IsCandidate}
return $detectedChanges | %{ $_.ServerItem }
}
finally
{
Write-Debug "Leaving Detect-WorkspaceChanges"
}
}
function AutoPend-WorkspaceChanges {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
$Provider,
[Parameter(Mandatory=$true)]
[array] $Items,
[Parameter(Mandatory=$true)]
[Microsoft.TeamFoundation.VersionControl.Client.RecursionType] $RecursionType,
[Microsoft.TeamFoundation.VersionControl.Client.ChangeType] $ChangeType
)
Write-Debug "Entering AutoPend-WorkspaceChanges"
$DetectedChanges = @(Detect-WorkspaceChanges -Provider $Provider -Items $Items -RecursionType $RecursionType -ChangeType $Changetype)
if ($DetectedChanges.Length -le 0)
{
Write-Output "No $ChangeType detected."
return
}
Write-Output "Pending $($ChangeType): "
$DetectedChanges | %{ Write-output $_ }
switch ($ChangeType)
{
"Delete"
{
$provider.Workspace.PendDelete(
$DetectedChanges,
[Microsoft.TeamFoundation.VersionControl.Client.RecursionType]"None",
[Microsoft.TeamFoundation.VersionControl.Client.LockLevel]"Unchanged",
$true,
$true
) |Out-Null
}
"Add"
{
$provider.Workspace.PendAdd(
$DetectedChanges,
$false, #recursive
$null,
[Microsoft.TeamFoundation.VersionControl.Client.LockLevel]"Unchanged",
$false,
$false, #silent
$true #ApplyLocalItemExclusions, Since GetPendingChangesWithCandidates ignores these anyway.
) |Out-Null
}
default
{
Write-Error "Unsupported auto-pend operation: $ChangeType"
}
}
Write-Debug "Leaving AutoPend-WorkspaceChanges"
}
function Convert-ToItemSpecs {
param (
[array] $Paths = @(),
[Microsoft.TeamFoundation.VersionControl.Client.RecursionType] $RecursionType = "None"
)
return @([Microsoft.TeamFoundation.VersionControl.Client.ItemSpec]::FromStrings($Paths, $RecursionType))
}
Export-ModuleMember -Function Invoke-DisposeSourceProvider
Export-ModuleMember -Function Get-SourceProvider
Export-ModuleMember -Function AutoPend-WorkspaceChanges
Export-ModuleMember -Function Convert-ToItemSpecs