Skip to content

Instantly share code, notes, and snippets.

@jstangroome
Created August 11, 2010 07:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstangroome/518638 to your computer and use it in GitHub Desktop.
Save jstangroome/518638 to your computer and use it in GitHub Desktop.
Automatic TFS Checkout for PowerShell ISE
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 $_
}
@borismod
Copy link

How to embed it with my PS ISE?

@adambu
Copy link

adambu commented Jan 12, 2018

Oh, I see it's a profile file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment