pf-dev.ps1
function Test-Powershell_ISE { return $host.Name.Contains('PowerShell ISE') } function Test-Powershell_VsCode { return $host.Name.Contains('Visual Studio Code Host') } function Test-Powershell_Editor { return (Test-Powershell_ISE) -or (Test-Powershell_VsCode) } function Test-Powershell_RemoteSession { return $Host.Name.Equals('ServerRemoteHost') } function Open-PS_Folder { param ( [Parameter(ValueFromPipeline=$true)] $path, [Switch]$CurrentFile ) begin { if ($CurrentFile -or -not $path) { $currentFilePath = $PSise.CurrentFile.FullPath if ($currentFilePath) { $folder = Split-Path $currentFilePath -Parent Push-Location $folder } } } process { $path = Get-Path $path if (-not $path) { return } $current = $path do { $item = Get-Item -Path $current -ErrorAction SilentlyContinue if ($item) { $attr = ( $item.Attributes -split ', ' ) if ( $attr -contains 'Directory' ) { break } } $current = Split-Path $current -Parent } while ($current) if ( $item ) { $folder = Get-Path -path $item Push-Location $folder } } } function Open-PS_Folder:::Example { Open-PS_Folder "C:\code" } function Open-PS_Line { param ( $file = (throw '$file parameter required'), $line = 1, $column = 1 ) $File = Get-Path -path $file if ( Test-Powershell_ISE ) { ise $File $iseFile = wait-until -timeout '00:01:00' -testInterval '00:00:00.1' { $PSise.CurrentPowerShellTab.Files | Where-Object FullPath -EQ $file } $PSise.CurrentPowerShellTab.Files.SetSelectedFile($iseFile) $iseFile.Editor.SetCaretPosition($Line,$Column) } if ( Test-Powershell_VsCode ) { code "$file`:$line`:$column" } } function Open-PS_Definition { param ( [Parameter(ValueFromPipeline=$true)] $functionaName ) process { if (-not $functionaName) { if ($psISE) { $functionaName = $psISE.CurrentFile.Editor.SelectedText } $functionaName = $^.Split(' ') } $functionList = Get-ChildItem function: | Where-Object name -eq $functionaName foreach ($function in $functionList) { $startPos = $function.ScriptBlock.StartPosition # Moves first to the end and then to the start so the function is displayed at the top of the editor Open-PS_Line -file $function.ScriptBlock.File -line $startPos.EndLine -column $startPos.EndColumn Open-PS_Line -file $function.ScriptBlock.File -line $startPos.StartLine -column $startPos.StartColumn } if ( $functionaName -like '*.ps1' -or $functionaName -like '*.psm1' ) { $m = Get-Module | Where-Object ModuleType -eq Script $matcheList = $m.ModuleBase | Get-ChildItem -Include '*.ps1', '*.psm1' -Recurse | Where-Object Name -Like $functionaName $matcheList | ForEach-Object { Open-PS_Line -file $_ } } } } function Open-PS_Definition:::Example { Open-PS_Definition -functionaName Invoke-Script_InMutexBlock:::Test 'Common.ps1' | Open-PS_Definition } function Open-PS_LastError { if (-not $Global:Error) { return } $lastError = $Global:Error | Where-Object ScriptStackTrace | Select-Object -First 1 if (-not $lastError) { return } $info = $lastError.InvocationInfo Open-PS_Line -file $info.ScriptName -line $info.ScriptLineNumber -column $info.OffsetInLine } function Open-File { param ( [Parameter(ValueFromPipeline=$true)] $path, $root, [Switch]$multiple ) process { $path = Get-Path $path $root = Get-Path $root if (-not $path) { return } $src = Get-ChildItem -Path $root -Recurse -ErrorAction SilentlyContinue | Where-Object FullName -Like $path if ($src -and -not $src.Count) { ise $src.fullname } } } function Open-File:::Example { Open-File -path *OhmsTC.ccfapp.xml } |