Created
August 11, 2010 07:40
-
-
Save jstangroome/518638 to your computer and use it in GitHub Desktop.
Automatic TFS Checkout for PowerShell ISE
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
if ($global:ProfileEventSubscribers) { | |
$global:ProfileEventSubscribers | | |
ForEach-Object { | |
Unregister-Event -SourceIdentifier $_.Name | |
} | |
} | |
$global:ProfileEventSubscribers = @() | |
$global:IsTfsInstalled = $false | |
try { | |
[Reflection.Assembly]::Load('Microsoft.TeamFoundation.VersionControl.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') | Out-Null | |
$global:IsTfsInstalled = $true | |
} catch { | |
Write-Warning 'TFS assembly could not be found. Ensure Team Explorer 2010 is installed.' | |
} | |
function global:Set-TfsCheckedOut ( | |
[string] | |
$Path, | |
[switch] | |
$Force | |
) { | |
Write-Verbose "Set-TfsCheckedOut -Path '$Path' -Force:`$$Force" | |
if (-not $Force -and -not (Get-Item -Path $Path).IsReadOnly) { return } | |
$WorkstationType = [Microsoft.TeamFoundation.VersionControl.Client.Workstation] | |
if (-not $WorkstationType::Current.IsMapped($Path)) { return } | |
$WorkspaceInfo = $WorkstationType::Current.GetLocalWorkspaceInfo($Path) | |
$Collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($WorkspaceInfo.ServerUri) | |
$Collection.EnsureAuthenticated() | |
$Workspace = $WorkspaceInfo.GetWorkspace($Collection) | |
$Workspace.PendEdit($Path) | Out-Null | |
} | |
function global:Register-ISEFileEvent ($ISEFile) { | |
if (-not $global:IsTfsInstalled) { return } | |
Write-Verbose "Subscribing to ($($ISEFile.DisplayName)).PropertyChanged" | |
$global:ProfileEventSubscribers += Register-ObjectEvent ` | |
-InputObject $ISEFile ` | |
-EventName PropertyChanged ` | |
-Action { | |
if ($eventArgs.PropertyName -eq 'IsSaved' -and | |
-not $sender.IsSaved) { | |
Set-TfsCheckedOut -Path $sender.FullPath | |
} | |
} | |
} | |
Write-Verbose 'Subscribing to $psISE.CurrentPowerShellTab.Files.CollectionChanged' | |
$global:ProfileEventSubscribers += Register-ObjectEvent ` | |
-InputObject $psISE.CurrentPowerShellTab.Files ` | |
-EventName CollectionChanged ` | |
-Action { | |
if ($eventArgs.Action -eq 'Add') { | |
$eventArgs.NewItems | | |
ForEach-Object { | |
Register-ISEFileEvent -ISEFile $_ | |
} | |
} | |
} | |
$psISE.CurrentPowerShellTab.Files | | |
ForEach-Object { | |
Register-ISEFileEvent -ISEFile $_ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to embed it with my PS ISE?