From 08abc7d6989066fe611180e0af58b9af59630018 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 9 Sep 2024 23:44:31 -0400 Subject: [PATCH] refactor(cli): doom.ps1: simplify Also ensures envvars persist into the after-script, and are cleared at the correct time (and only once). --- bin/doom.ps1 | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/bin/doom.ps1 b/bin/doom.ps1 index a8f77646b..be05fe305 100644 --- a/bin/doom.ps1 +++ b/bin/doom.ps1 @@ -7,32 +7,34 @@ if (!(Get-Command -Erroraction silentlycontinue emacs.exe)) { $doom = "$PSScriptRoot/doom" $emacs = if ($env:EMACS) { $env:EMACS } else { (Get-Command emacs.exe).Path } -$emacsargs = "-q", "--no-site-file", "--batch" $oldemacsdir = $env:EMACSDIR try { - $env:EMACSDIR = if (-not $env:EMACSDIR) { (get-item $PSScriptRoot).parent.FullName } else { $env:EMACSDIR } - $env:__DOOMSH = if (-not $env:__DOOMSH) { "ps1" } else { $env:__DOOMSH } - $env:__DOOMPID = if (-not $env:__DOOMPID) { $PID } else { $env:__DOOMPID } - $env:__DOOMSTEP = if (-not $env:__DOOMSTEP) { 0 } else { $env:__DOOMSTEP } - $cols = (Get-Host).UI.RawUI.WindowSize.Width - $lines = (Get-Host).UI.RawUI.WindowSize.Height - $env:__DOOMGEOM = if (-not $env:__DOOMGEOM) { "$cols`x$lines" } else { $env:__DOOMGEOM } + if (-not $env:EMACSDIR) { $env:EMACSDIR = (get-item $PSScriptRoot).parent.FullName; } + if (-not $env:__DOOMSH) { $env:__DOOMSH = "ps1"; } + if (-not $env:__DOOMPID) { $env:__DOOMPID = $PID; } + if (-not $env:__DOOMSTEP) { $env:__DOOMSTEP = 0; } + if (-not $env:__DOOMGEOM) { + $cols = (Get-Host).UI.RawUI.WindowSize.Width + $lines = (Get-Host).UI.RawUI.WindowSize.Height + $env:__DOOMGEOM = "$cols`x$lines" + } # $env:__DOOMGPIPE = if (-not $env:__DOOMGPIPE) { $env:__DOOMPIPE } else { $env:__DOOMGPIPE } # $env:__DOOMPIPE = "" - & $emacs $emacsargs --load "$doom" -- --no-color $args - $exit = $LASTEXITCODE + & $emacs -q --no-site-file --batch --load "$doom" -- --no-color $args + if ($LASTEXITCODE -eq 254) { + & pwsh "$($env:temp)\doom.$($env:__DOOMPID).$($env:__DOOMSTEP).ps1" $PSCommandPath $args + $exit = $LASTEXITCODE + } + exit $exit } finally { - $env:EMACSDIR = $oldemacsdir - Remove-Item Env:\__DOOMSH - Remove-Item Env:\__DOOMPID - Remove-Item Env:\__DOOMSTEP - Remove-Item Env:\__DOOMGEOM + # Only clear the env at top-level + if ($env:__DOOMSTEP -eq 0) { + $env:EMACSDIR = $oldemacsdir + Remove-Item Env:\__DOOMSH + Remove-Item Env:\__DOOMPID + Remove-Item Env:\__DOOMSTEP + Remove-Item Env:\__DOOMGEOM + } } - -if ($exit -eq 254) { - & pwsh "$($env:temp)\doom.$($env:__DOOMPID).$($env:__DOOMSTEP).ps1" $PSCommandPath $args - $exit = $LASTEXITCODE -} -exit $exit