Skip to content

Non destructive PATH configuration #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2023
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
50 changes: 27 additions & 23 deletions src/jenv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,34 @@ Import-Module $PSScriptRoot\jenv-autoscan.psm1 -Force
$JENV_VERSION = "v2.0.32"

#region Remove any java versions from path
# Get all javas except for our fake java script
$javaPaths = (Get-Command java -All | Where-Object { $_.source -ne ((get-item $PSScriptRoot).parent.fullname + "\java.bat") }).source
# Only change something when java versions are found
if ($javaPaths.Length -gt 0) {
Write-Host "JEnv is changing your environment variables. This process could take longer but it happens only when a java executable is found in your path"
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
$systemPath = [System.Environment]::GetEnvironmentVariable("PATH", "MACHINE").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
# Remove all javas from path
$userPath = ($userPath | Where-Object { !$javaPaths.Contains($_ + "\java.exe") } ) -join ";"
$systemPath = ($systemPath | Where-Object { !$javaPaths.Contains($_ + "\java.exe") } ) -join ";"
[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) # Set globally

try {
[System.Environment]::SetEnvironmentVariable("PATH", $systemPath, [System.EnvironmentVariableTarget]::Machine) # Set globally
}
catch [System.Management.Automation.MethodInvocationException] {
Write-Host "JEnv wants to change your system environment vars. Therefore you need to restart it with administration rights. This should only once be required. If you dont want to, you have to call JEnv on every terminal opening to change your session vars"
}
$path = $userPath + ";" + $systemPath
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
$systemPath = [System.Environment]::GetEnvironmentVariable("PATH", "MACHINE").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
# Get all jenvs
$jenvPaths = (Get-Command jenv -All).source
# Only change something when jenv versions are found
if ($jenvPaths.Length -gt 0) {
Write-Host "JEnv is changing your environment variables. This process could take longer but it happens only when a jenv executable is found in your path"
# Remove all jenvs from path
$userPath = ($userPath | Where-Object { !$jenvPaths.Contains($_ + "\jenv.bat") } ) -join ";"
$systemPath = ($systemPath | Where-Object { !$jenvPaths.Contains($_ + "\jenv.bat") } ) -join ";"
}

$Env:PATH = $path # Set for powershell users
if ($output) {
Set-Content -path "jenv.path.tmp" -value $path # Create temp file so no restart of the active shell is required
}
# Add JEnv to the beginning of the system path
$currentJenvPath = (get-item $PSScriptRoot).parent.fullname
$systemPath = $currentJenvPath + ";" + $systemPath

[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) # Set globally
try {
[System.Environment]::SetEnvironmentVariable("PATH", $systemPath, [System.EnvironmentVariableTarget]::Machine) # Set globally
}
catch [System.Management.Automation.MethodInvocationException] {
Write-Host "JEnv wants to change your system environment vars. Therefore you need to restart it with administration rights. This should only once be required. If you dont want to, you have to call JEnv on every terminal opening to change your session vars"
}
$path = $systemPath + ";" + $userPath

$Env:PATH = $path # Set for powershell users
if ($output) {
Set-Content -path "jenv.path.tmp" -value $path # Create temp file so no restart of the active shell is required
}
#endregion

Expand Down