Skip to content

Commit

Permalink
Merge pull request #47 from rcsilva83/non-destructive-path-config
Browse files Browse the repository at this point in the history
Non destructive PATH configuration
  • Loading branch information
rcsilva83 authored Jan 3, 2023
2 parents cd5bdfe + e47c521 commit 7af183d
Showing 1 changed file with 27 additions and 23 deletions.
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.1.0"

#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

0 comments on commit 7af183d

Please sign in to comment.