-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitcherDevEnv.ps1
executable file
·63 lines (50 loc) · 1.47 KB
/
SwitcherDevEnv.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$directoryToChoose = "C:\Program Files\Java"
function switchJavaInteractively
{
$javaHomeDirectories = getJavaHomeDirectories
$newJavaHome = getUserChoiceFor($javaHomeDirectories)
setJavaHomeTo($newJavaHome)
for ($i = 0; $i -lt $javaHomeDirectories.Count; $i++) {
$javaHomeDirectory=$javaHomeDirectories[$i]
$javaBin=$javaHomeDirectory+"\bin"
removeFromEnvironmentPath($javaBin)
removeFromEnvironmentPath($javaHomeDirectory)
}
addJavaHomeToPath
}
function removeFromEnvironmentPath{
param([string]$pathToRemove = "")
[Environment]::SetEnvironmentVariable("Path",$env:Path.Replace($pathToRemove, ""))
removeDoubleSemicolonsInPath
}
function setJavaHomeTo
{
param([string]$newJavaHome)
$env:JAVA_HOME = $newJavaHome
[Environment]::SetEnvironmentVariable("JAVA_HOME", $newJavaHome)
}
function addJavaHomeToPath
{
[Environment]::SetEnvironmentVariable("Path", $env:JAVA_HOME + "\bin;" + $env:Path)
}
function getUserChoiceFor
{
param([Object[]]$paths)
Write-Host "Choose your JDK"
for ($i = 0; $i -lt $paths.Count; $i++) {
$installDir = $paths[$i]
Write-Host "[$i]" "-" $installDir
}
$userInput = Read-Host "Input: "
#todo marmer handle invalid input
return $paths[$userInput]
}
function getJavaHomeDirectories
{
return (ls $directoryToChoose).FullName
}
function removeDoubleSemicolonsInPath
{
$env:Path = $env:Path.Replace(";;", ";")
}
switchJavaInteractively