Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 29 additions & 59 deletions winpython/portable/launchers_final/scripts/WinPython_PS_Prompt.ps1
Original file line number Diff line number Diff line change
@@ -1,77 +1,47 @@
### WinPython_PS_Prompt.ps1 ###

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

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

# rem 2023-02-12 try utf-8 on console
$env:PYTHONIOENCODING = "utf-8"

if (-not $env:PATH.ToLower().Contains(";"+ $env:WINPYDIR.ToLower()+ ";")) {
$env:PATH = "$env:WINPYDIR\Lib\site-packages\PyQt5;$env:WINPYDIR\;$env:WINPYDIR\DLLs;$env:WINPYDIR\Scripts;$env:WINPYDIR\..\t;$env:WINPYDIR\..\n;$env:path" }

#rem force default pyqt5 kit for Spyder if PyQt5 module is there
if (Test-Path "$env:WINPYDIR\Lib\site-packages\PyQt5\__init__.py") { $env:QT_API = "pyqt5" }

$output = & $env:PYTHON ($env:WINPYDIRBASE + '\scripts\WinPythonIni.py')
$pairs = $output -split '&&'
$pair = $pair -replace '^(?i)set\s+',''
foreach ($pair in $pairs) {
$pair = $pair.Trim()
if ($pair -eq '') { continue }
foreach ($tmp_pair in ($tmp_output -split '&&')) {
$tmp_pair = $tmp_pair.Trim()
if ($tmp_pair -eq '') { continue }

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

# Find first '=' so values can contain '='
$idx = $pair.IndexOf('=')
if ($idx -lt 0) {
Write-Warning "Skipping invalid pair (no '='): '$pair'"
$tmp_idx = $tmp_pair.IndexOf('=')
if ($tmp_idx -lt 0) {
Write-Warning "Skipping invalid pair (no '='): '$tmp_pair'"
continue
}

$name = $pair.Substring(0, $idx).Trim()
$value = $pair.Substring($idx + 1).Trim()
$tmp_name = $tmp_pair.Substring(0, $tmp_idx).Trim()
$tmp_value = $tmp_pair.Substring($tmp_idx + 1).Trim()

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

# Set as a PowerShell global variable
Set-Variable -Name $name -Value $value -Scope Global -Force

# Also set as environment variable for child processes
$env:name = $value

# If running in GH Actions, also append to GITHUB_ENV so future steps see it
if ($env:GITHUB_ENV) {
# Use Add-Content to append "NAME=value" to the GITHUB_ENV file
# Escape any newlines in $value to avoid breaking the file format
$escapedValue = $value -replace "`n", '%0A' -replace "`r", ''
Add-Content -Path $env:GITHUB_ENV -Value "$name=$escapedValue"
}

#Write-Host "Set `$${name} = $value"
# set as environment variable for child processes like python
Set-Item -Path "Env:\$tmp_name" -Value $tmp_value
#Write-Host $tmp_name " = " $tmp_value
}

# directory of the running script (fallback to MyInvocation for older hosts)
$scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Definition }

# emulate %__CD% and build scripts\ path
$envCD = $env:__CD__
$scriptsPath = if ($envCD) { Join-Path $envCD 'scripts' } else { $null }
# emulate %__CD%
$tmp_envCD = $env:__CD__
if (($tmp_dp0 -eq $tmp_envCD ) -or ($tmp_dp0 -eq { Join-Path $tmp_envCD 'scripts' } )) {
Set-Location -LiteralPath $WINPYWORKDIR1
}

# target to change into (environment variable WINPYWORKDIR1)
$target = $WINPYWORKDIR1
# Clean-up NameSpace
rv tmp_pair
rv tmp_name
rv tmp_value
rv tmp_idx

if ($target) {
if ($scriptsPath -and $scriptDir -and ($scriptsPath -eq $scriptDir)) {
Set-Location -LiteralPath $target
}
elseif ($envCD -and $scriptDir -and ($envCD -eq $scriptDir)) {
Set-Location -LiteralPath $target
}
}
rv tmp_dp0
rv tmp_output
rv tmp_envCD