Skip to content

Commit 981f16c

Browse files
committed
Implemented uninstall. Closes #25
Fixed Write-Host issue introduced in last commit Created User-Prompt in util
1 parent 9dd4df0 commit 981f16c

File tree

8 files changed

+108
-10
lines changed

8 files changed

+108
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Example: `jenv remove jdk15`
6767
*jenv link `<Executable name>`*
6868
Example: `jenv link javac`
6969

70-
10) **COMING SOON: Uninstall jenv and automatically restore a Java version of your choice**
70+
10) **Uninstall jenv and automatically restore a Java version of your choice**
7171
*jenv uninstall `<name>`*
7272
Example: `jenv uninstall jdk17`
7373
## How does this work?

src/jenv-change.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Invoke-Change {
1616
# Check if specified JEnv is avaible
1717
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
1818
if ($null -eq $jenv) {
19-
Write-Host 'Theres no JEnv with name {0} Consider using "jenv list"' -f $name
19+
Write-Host ('Theres no JEnv with name {0} Consider using "jenv list"' -f $name)
2020
return
2121
}
2222
else {

src/jenv-list.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function Invoke-List {
22
param (
3-
[object]$config,
4-
[boolean]$help
3+
[Parameter(Mandatory = $true)][object]$config,
4+
[Parameter(Mandatory = $true)][boolean]$help
55
)
66

77
if ($help) {

src/jenv-local.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function Invoke-Local {
3333
if ($jenv.path -eq (Get-Location)) {
3434
# if path is used replace with new version
3535
$jenv.name = $name
36-
Write-Output "Your replaced your java version for {0} {1}" -f (Get-Location), $name
36+
Write-Output ("Your replaced your java version for {0} {1}" -f (Get-Location), $name)
3737
return
3838
}
3939
}
@@ -44,6 +44,6 @@ function Invoke-Local {
4444
name = $name
4545
}
4646

47-
Write-Output "{0} is now your local java version for {1}" -f (Get-Location), $name
47+
Write-Output ("{0} is now your local java version for {1}" -f (Get-Location), $name)
4848
}
4949
}

src/jenv-uninstall.psm1

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
function Invoke-Uninstall {
2+
param (
3+
[Parameter(Mandatory = $true)][boolean]$help,
4+
[Parameter(Mandatory = $true)][object]$config,
5+
[Parameter(Mandatory = $true)][string]$name
6+
)
7+
8+
if ($help) {
9+
Write-Host '"jenv uninstall" <name>'
10+
Write-Host 'This command deletes jenv and restores the specified jenv as java'
11+
Write-Host '<name> is the alias you asigned to the path with "jenv add <name> <path>"'
12+
}
13+
else {
14+
15+
# Check if specified JEnv is avaible
16+
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
17+
if ($null -eq $jenv) {
18+
Write-Host ('Theres no JEnv with name {0} Consider using "jenv list"' -f $name)
19+
return
20+
}
21+
22+
# Provides the Open-Prompt function
23+
Import-Module $PSScriptRoot\util.psm1
24+
25+
# Abort Uninstall
26+
if ((Open-Prompt "Uninstalling JEnv" "Are you sure you want to delete JEnv entirely from this computer?" "Yes", "No" "This will remove JEnv from your computer", "Last chance to abort the disaster" 1) -eq 1) {
27+
Write-Host "Aborted uninstallation"
28+
return
29+
}
30+
31+
#region Restore the specified java version
32+
33+
# Restore PATH
34+
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
35+
$systemPath = [System.Environment]::GetEnvironmentVariable("PATH", "MACHINE").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
36+
37+
# Filter out the jenv path
38+
$root = (get-item $PSScriptRoot).parent.fullname
39+
$userPath = ($userPath | Where-Object { $_ -ne $root } ) -join ";"
40+
$systemPath = ($systemPath | Where-Object { $_ -ne $root } ) -join ";"
41+
42+
#Update user path
43+
$userPath = $userPath + ";" + $jenv.path + "\bin"
44+
45+
# Set the new PATH
46+
$path = $userPath + ";" + $systemPath
47+
$Env:PATH = $path # Set for powershell users
48+
if ($output) {
49+
Set-Content -path "jenv.path.tmp" -value $path # Create temp file so no restart of the active shell is required
50+
}
51+
52+
# Restore JAVA_HOME
53+
$Env:JAVA_HOME = $jenv.path # Set for powershell users
54+
if ($output) {
55+
Set-Content -path "jenv.home.tmp" -value $jenv.path # Create temp file so no restart of the active shell is required
56+
}
57+
58+
# Set globally
59+
Write-Host "JEnv is changing your environment variables. This process could take longer"
60+
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $javahome, [System.EnvironmentVariableTarget]::User)
61+
[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) # Set globally
62+
63+
# Either delete %appdata%/jenv or keep config
64+
$uninstall = Open-Prompt "Uninstalling JEnv" "Do you want to keep your config file" "Yes", "No" "If you reinstall JEnv later it will use all your configured java_homes and locals", "If you reinstall JEnv it has to be set up from the ground on. Pick this if you dont plan reinstalling JEnv" 0
65+
if ($uninstall -eq 1) {
66+
Remove-Item $env:appdata/jenv -recurse -force
67+
}
68+
#endregion
69+
70+
# Delete jenv folder
71+
Remove-Item (get-item $PSScriptRoot).Parent.FullName -Recurse -Force
72+
73+
# Exit the script so jenv.ps1 wont continue to run
74+
Write-Host "Successfully uninstalled JEnv"
75+
Exit 0
76+
77+
78+
}
79+
}

src/jenv-use.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Invoke-Use {
2828
# Check if specified JEnv is avaible
2929
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
3030
if ($null -eq $jenv) {
31-
Write-Host 'Theres no JEnv with name {0} Consider using "jenv list"' -f $name
31+
Write-Host ('Theres no JEnv with name {0} Consider using "jenv list"' -f $name)
3232
return
3333
}
3434
else {

src/jenv.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ param (
1818
"jenv use <name>" Applys the given Java-Version locally for the current shell
1919
"jenv local <name>" Will use the given Java-Version whenever in this folder. Will set the Java-version for all subfolders as well
2020
#>
21-
[Parameter(Position = 0)][validateset("list", "add", "change", "use", "remove", "local", "getjava", "link")] [string]$action,
21+
[Parameter(Position = 0)][validateset("list", "add", "change", "use", "remove", "local", "getjava", "link", "uninstall")] [string]$action,
2222

2323
# Displays this helpful message
2424
[Alias("h")]
@@ -40,6 +40,7 @@ Import-Module $PSScriptRoot\jenv-use.psm1 -Force
4040
Import-Module $PSScriptRoot\jenv-local.psm1 -Force
4141
Import-Module $PSScriptRoot\jenv-getjava.psm1 -Force
4242
Import-Module $PSScriptRoot\jenv-link.psm1 -Force
43+
Import-Module $PSScriptRoot\jenv-uninstall.psm1 -Force
4344
#endregion
4445

4546
#region Installation
@@ -108,7 +109,7 @@ if (!($config | Get-Member global)) {
108109
#region Apply java_home for jenv local
109110
$localname = ($config.locals | Where-Object { $_.path -eq (Get-Location) }).name
110111
$javahome = ($config.jenvs | Where-Object { $_.name -eq $localname }).path
111-
if ($null -eq $local) {
112+
if ($null -eq $localname) {
112113
$javahome = $config.global
113114
}
114115
$Env:JAVA_HOME = $javahome # Set for powershell users
@@ -125,21 +126,23 @@ if ($help -and $action -eq "") {
125126
Write-Host '"jenv use <name>" Applys the given Java-Version locally for the current shell'
126127
Write-Host '"jenv local <name>" Will use the given Java-Version whenever in this folder. Will set the Java-version for all subfolders as well'
127128
Write-Host '"jenv link <executable>" Creates shortcuts for executables inside JAVA_HOME. For example "javac"'
129+
Write-Host '"jenv uninstall <name>" Deletes JEnv and restores the specified java version to the system. You may keep your config file'
128130
Write-Host 'Get help for individual commands using "jenv <list/add/remove/change/use/local> --help"'
129131
}
130132
else {
131133

132134
# Call the specified command
133135
# Action has to be one of the following because of the validateset
134136
switch ( $action ) {
135-
list { Invoke-List $config $help @arguments }
137+
list { Invoke-List $config $help }
136138
add { Invoke-Add $config $help @arguments }
137139
remove { Invoke-Remove $config $help @arguments }
138140
use { Invoke-Use $config $help $output @arguments }
139141
change { Invoke-Change $config $help $output @arguments }
140142
local { Invoke-Local $config $help @arguments }
141143
getjava { Get-Java $config }
142144
link { Invoke-Link $help @arguments }
145+
uninstall { Invoke-Uninstall $help $config @arguments }
143146
}
144147

145148
#region Save the config

src/util.psm1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function Open-Prompt {
2+
param (
3+
[Parameter(Mandatory = $true)][string]$title,
4+
[Parameter(Mandatory = $true)][string]$question,
5+
[Parameter(Mandatory = $true)][string[]]$choices,
6+
[Parameter(Mandatory = $true)][string[]]$choice_descriptions,
7+
[Parameter(Mandatory = $true)][int]$default_choice
8+
)
9+
10+
[System.Management.Automation.Host.ChoiceDescription[]] $options = @()
11+
for ($i = 0; $i -lt $choices.Length; $i++) {
12+
$options += New-Object System.Management.Automation.Host.ChoiceDescription ("&{0}" -f $choices[$i]), $choice_descriptions[$i]
13+
}
14+
15+
return $Host.UI.PromptForChoice($title, $question, $options, $default_choice)
16+
}

0 commit comments

Comments
 (0)