Skip to content

Commit 188d964

Browse files
committed
simplify further WinPython_PS_Prompt.ps1
1 parent c2be573 commit 188d964

File tree

1 file changed

+35
-52
lines changed

1 file changed

+35
-52
lines changed
Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,60 @@
11
### WinPython_PS_Prompt.ps1 ###
22

3-
$env:WINPYDIRBASE = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition) + "\.."
4-
# get a normalize path
5-
$env:WINPYDIRBASE = [System.IO.Path]::GetFullPath( $env:WINPYDIRBASE )
3+
# $env:PYTHONIOENCODING = "utf-8"
64

7-
$env:WINPYDIR = $env:WINPYDIRBASE+ "\python"
8-
# 2019-08-25 pyjulia needs absolutely a variable PYTHON=%WINPYDIR%python.exe
9-
$env:PYTHON = $env:WINPYDIRBASE+ "\python\python.exe"
5+
$tmp_dp0 = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
6+
$tmp_output = & ([System.IO.Path]::Combine( $tmp_dp0 , "..", "python" , "python.exe")) ([System.IO.Path]::Combine($tmp_dp0 , "WinPythonIni.py"))
107

11-
# rem 2023-02-12 try utf-8 on console
12-
$env:PYTHONIOENCODING = "utf-8"
13-
14-
if (-not $env:PATH.ToLower().Contains(";"+ $env:WINPYDIR.ToLower()+ ";")) {
15-
$env:PATH = "$env:WINPYDIR\Lib\site-packages\PyQt5;$env:WINPYDIR\;$env:WINPYDIR\DLLs;$env:WINPYDIR\Scripts;$env:WINPYDIR\..\t;$env:WINPYDIR\..\n;$env:path" }
16-
17-
#rem force default pyqt5 kit for Spyder if PyQt5 module is there
18-
if (Test-Path "$env:WINPYDIR\Lib\site-packages\PyQt5\__init__.py") { $env:QT_API = "pyqt5" }
19-
20-
$output = & $env:PYTHON ($env:WINPYDIRBASE + '\scripts\WinPythonIni.py')
21-
$pairs = $output -split '&&'
22-
$pair = $pair -replace '^(?i)set\s+',''
23-
foreach ($pair in $pairs) {
24-
$pair = $pair.Trim()
25-
if ($pair -eq '') { continue }
8+
foreach ($tmp_pair in ($tmp_output -split '&&')) {
9+
$tmp_pair = $tmp_pair.Trim()
10+
if ($tmp_pair -eq '') { continue }
2611

2712
# Remove leading "set " (case-insensitive)
28-
$pair = $pair -replace '^(?i)set\s+',''
13+
$tmp_pair = $tmp_pair -replace '^(?i)set\s+',''
2914

3015
# Find first '=' so values can contain '='
31-
$idx = $pair.IndexOf('=')
32-
if ($idx -lt 0) {
33-
Write-Warning "Skipping invalid pair (no '='): '$pair'"
16+
$tmp_idx = $tmp_pair.IndexOf('=')
17+
if ($tmp_idx -lt 0) {
18+
Write-Warning "Skipping invalid pair (no '='): '$tmp_pair'"
3419
continue
3520
}
3621

37-
$name = $pair.Substring(0, $idx).Trim()
38-
$value = $pair.Substring($idx + 1).Trim()
22+
$tmp_name = $tmp_pair.Substring(0, $tmp_idx).Trim()
23+
$tmp_value = $tmp_pair.Substring($tmp_idx + 1).Trim()
3924

4025
# Unquote a quoted value (single or double quotes)
41-
if ($value -match '^(["''])(.*)\1$') { $value = $Matches[2] }
26+
if ($tmp_value -match '^(["''])(.*)\1$') { $tmp_value = $Matches[2] }
4227

4328
# Set as a PowerShell global variable
44-
Set-Variable -Name $name -Value $value -Scope Global -Force
45-
46-
# Also set as environment variable for child processes
47-
$env:name = $value
29+
if ($tmp_name -ne "HOME") { Set-Variable -Name $tmp_name -Value $tmp_value }
30+
Set-Variable -Name $tmp_name -Value $tmp_value -Scope Global -Force
31+
# set as environment variable for child processes
32+
Set-Item -Path "Env:\$tmp_name" -Value $tmp_value
33+
# Write-Host $tmp_name " = " $tmp_value
4834

4935
# If running in GH Actions, also append to GITHUB_ENV so future steps see it
5036
if ($env:GITHUB_ENV) {
5137
# Use Add-Content to append "NAME=value" to the GITHUB_ENV file
52-
# Escape any newlines in $value to avoid breaking the file format
53-
$escapedValue = $value -replace "`n", '%0A' -replace "`r", ''
54-
Add-Content -Path $env:GITHUB_ENV -Value "$name=$escapedValue"
38+
# Escape any newlines in $tmp_value to avoid breaking the file format
39+
$tmp_escapedValue = $tmp_value -replace "`n", '%0A' -replace "`r", ''
40+
Add-Content -Path $env:GITHUB_ENV -Value "$tmp_name=$tmp_escapedValue"
5541
}
5642

57-
#Write-Host "Set `$${name} = $value"
43+
#Write-Host "Set `$${name} = $tmp_value"
5844
}
5945

60-
# directory of the running script (fallback to MyInvocation for older hosts)
61-
$scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Definition }
62-
63-
# emulate %__CD% and build scripts\ path
64-
$envCD = $env:__CD__
65-
$scriptsPath = if ($envCD) { Join-Path $envCD 'scripts' } else { $null }
46+
# emulate %__CD%
47+
$tmp_envCD = $env:__CD__
48+
if (($tmp_dp0 -eq $tmp_envCD ) -or ($tmp_dp0 -eq { Join-Path $tmp_envCD 'scripts' } )) {
49+
Set-Location -LiteralPath $WINPYWORKDIR1
50+
}
6651

67-
# target to change into (environment variable WINPYWORKDIR1)
68-
$target = $WINPYWORKDIR1
52+
# Clean-up NameSpace
53+
rv tmp_pair
54+
rv tmp_name
55+
rv tmp_value
56+
rv tmp_idx
6957

70-
if ($target) {
71-
if ($scriptsPath -and $scriptDir -and ($scriptsPath -eq $scriptDir)) {
72-
Set-Location -LiteralPath $target
73-
}
74-
elseif ($envCD -and $scriptDir -and ($envCD -eq $scriptDir)) {
75-
Set-Location -LiteralPath $target
76-
}
77-
}
58+
rv tmp_dp0
59+
rv tmp_output
60+
rv tmp_envCD

0 commit comments

Comments
 (0)