1313#
1414# You should have received a copy of the GNU General Public License
1515# along with this program. If not, see <https://www.gnu.org/licenses/>.
16- $PyAutoEnvDir = " ${PSScriptRoot} "
17-
18- if (Test-Path alias:cd) {
19- Remove-Alias - Name cd - Force - Scope Global
20- }
16+ $pyAutoEnvDir = " ${PSScriptRoot} "
2117
2218<#
2319. SYNOPSIS
@@ -35,12 +31,12 @@ function Invoke-PyAutoEnv() {
3531 if (-Not (Get-Command " python" - ErrorAction SilentlyContinue)) {
3632 return
3733 }
38- $PyAutoEnv = Join-Path " ${PyAutoEnvDir } " " pyautoenv.py"
39- if (Test-Path " ${PyAutoEnv } " ) {
40- $Expression = " $ ( python " ${PyAutoEnv } " -- pwsh) "
41- if (${Expression } ) {
42- Invoke-Expression " ${Expression } "
43- }
34+ $pyAutoEnv = Join-Path " ${pyAutoEnvDir } " " pyautoenv.py"
35+ if (Test-Path " ${pyAutoEnv } " ) {
36+ $expression = " $ ( python " ${pyAutoEnv } " -- pwsh) "
37+ if (${expression } ) {
38+ Invoke-Expression " ${expression } "
39+ }
4440 }
4541}
4642
@@ -51,12 +47,46 @@ function Invoke-PyAutoEnv() {
5147 https://github.com/hsaunders1904/pyautoenv/
5248#>
5349function Invoke-PyAutoEnvVersion () {
54- $PyAutoEnv = Join-Path " ${PyAutoEnvDir } " " pyautoenv.py"
55- python " ${PyAutoEnv } " -- version
50+ $pyAutoEnv = Join-Path " ${pyAutoEnvDir } " " pyautoenv.py"
51+ python " ${pyAutoEnv } " -- version
5652}
5753
58- function cd () {
59- Set-Location @Args && Invoke-PyAutoEnv
54+ <#
55+ . SYNOPSIS
56+ Create a proxy function definition for a Cmdlet that executes pyautoenv.
57+ . LINK
58+ https://github.com/hsaunders1904/pyautoenv/
59+ #>
60+ function New-PyAutoEnvProxyFunctionDefinition ([string ] $commandString )
61+ {
62+ # Generate base code for the Proxy function.
63+ $originalCommand = Get-Command - Name " $commandString " - CommandType Cmdlet
64+ $metaData = New-Object System.Management.Automation.CommandMetaData $originalCommand
65+ $proxyCode = [System.Management.Automation.ProxyCommand ]::Create($metaData )
66+
67+ # Find the 'end' block of Set-Location's source.
68+ $ast = [System.Management.Automation.Language.Parser ]::ParseInput($proxyCode , [ref ]$null , [ref ]$null )
69+ $endBlock = $ast.EndBlock.Extent.Text
70+ $endBlockClosingIndex = $endBlock.LastIndexOf (' }' )
71+ if ($endBlockClosingIndex -Le 0 ) {
72+ # If we can't find the opening brace, something's not right, so exit early
73+ # without editing the proxy to avoid breaking things.
74+ $body = $ast.ToString ()
75+ return " function $commandString {`n ${body} `n }"
76+ }
77+
78+ # Insert the pyautoenv function call into the 'end' block of the proxy code.
79+ $tab = " "
80+ $insert = " `n ${tab} try {`n ${tab}${tab} Invoke-PyAutoEnv`n ${tab} } catch {}`n "
81+ $newEndBlockOpen = $endBlock.Substring (0 , $endBlockClosingIndex ) + $insert
82+ $newEndBlock = $newEndBlockOpen + $endBlock.Substring ($endBlockClosingIndex )
83+ $updatedProxyCmd = $proxyCode.Replace ($endBlock , $newEndBlock )
84+ return " function global:$commandString {`n $updatedProxyCmd `n }"
6085}
6186
62- Invoke-PyAutoEnv
87+ foreach ($commandName in (" Set-Location" , " Push-Location" , " Pop-Location" )) {
88+ Invoke-Expression (& {
89+ (New-PyAutoEnvProxyFunctionDefinition " $commandName " | Out-String )
90+ })
91+ }
92+ Invoke-PyAutoEnv # Look for environment in initial directory.
0 commit comments