Skip to content

Commit f105bd5

Browse files
committed
Replaced if else with return to avoid unnecessary nesting
1 parent 89ac338 commit f105bd5

File tree

8 files changed

+148
-155
lines changed

8 files changed

+148
-155
lines changed

src/jenv-add.psm1

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,32 @@ function Invoke-Add {
1414
Write-Host 'You have to register your JEnvs first before you can use "jenv change", "jenv use" or "jenv local"'
1515
Write-Host 'List all registered java versions with "jenv list"'
1616
Write-Host 'This command is not used to specify local JEnvs. Use "jenv local" for this approach'
17+
return
1718
}
18-
else {
1919

20-
# Name cannot be remove due to the local remove
21-
if ($name -eq "remove") {
22-
Write-Output 'Your JEnv name cannot be "remove". Checkout "jenv remove"'
23-
return
24-
}
20+
# Name cannot be remove due to the local remove
21+
if ($name -eq "remove") {
22+
Write-Output 'Your JEnv name cannot be "remove". Checkout "jenv remove"'
23+
return
24+
}
2525

26-
# Check if name is already used
27-
foreach ($jenv in $config.jenvs) {
28-
if ($jenv.name -eq $name) {
29-
Write-Output 'Theres already a JEnv with that name. Consider using "jenv list"'
30-
return
31-
}
32-
}
33-
# Check if the path is a valid java home
34-
if (!(Test-Path -Path $path/bin/java.exe -PathType Leaf)) {
35-
Write-Output ($path + "/bin/java.exe not found. Your Path is not a valid JAVA_HOME")
26+
# Check if name is already used
27+
foreach ($jenv in $config.jenvs) {
28+
if ($jenv.name -eq $name) {
29+
Write-Output 'Theres already a JEnv with that name. Consider using "jenv list"'
3630
return
3731
}
32+
}
33+
# Check if the path is a valid java home
34+
if (!(Test-Path -Path $path/bin/java.exe -PathType Leaf)) {
35+
Write-Output ($path + "/bin/java.exe not found. Your Path is not a valid JAVA_HOME")
36+
return
37+
}
3838

39-
# Add new JEnv
40-
$config.jenvs += [PSCustomObject]@{
41-
name = $name
42-
path = $path
43-
}
44-
Write-Output ("Successfully added the new JEnv: " + $name)
39+
# Add new JEnv
40+
$config.jenvs += [PSCustomObject]@{
41+
name = $name
42+
path = $path
4543
}
44+
Write-Output ("Successfully added the new JEnv: " + $name)
4645
}

src/jenv-change.psm1

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@ function Invoke-Change {
1010
Write-Host '"jenv change <name>"'
1111
Write-Host 'With this command you set your JAVA_HOME and the version of java to be used globally. This is overwriten by both "jenv local" and "jenv use"'
1212
Write-Host '<name> is the alias you asigned to the path with "jenv add <name> <path>"'
13+
return
1314
}
14-
else {
1515

16-
# Check if specified JEnv is avaible
17-
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
18-
if ($null -eq $jenv) {
19-
Write-Host ('Theres no JEnv with name {0} Consider using "jenv list"' -f $name)
20-
return
21-
}
22-
else {
23-
Write-Host "Setting JAVA_HOME globally. This could take some time"
24-
$config.global = $jenv.path
25-
$Env:JAVA_HOME = $jenv.path # Set for powershell users
26-
if ($output) {
27-
Set-Content -path "jenv.home.tmp" -value $jenv.path # Create temp file so no restart of the active shell is required
28-
}
29-
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $jenv.path, [System.EnvironmentVariableTarget]::User) # Set globally}
30-
Write-Host "JEnv changed globally"
16+
# Check if specified JEnv is avaible
17+
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
18+
if ($null -eq $jenv) {
19+
Write-Host ('Theres no JEnv with name {0} Consider using "jenv list"' -f $name)
20+
return
21+
}
22+
else {
23+
Write-Host "Setting JAVA_HOME globally. This could take some time"
24+
$config.global = $jenv.path
25+
$Env:JAVA_HOME = $jenv.path # Set for powershell users
26+
if ($output) {
27+
Set-Content -path "jenv.home.tmp" -value $jenv.path # Create temp file so no restart of the active shell is required
3128
}
29+
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $jenv.path, [System.EnvironmentVariableTarget]::User) # Set globally}
30+
Write-Host "JEnv changed globally"
3231
}
3332
}

src/jenv-link.psm1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ function Invoke-Link {
1010
Write-Host '<executable> is the name of the binary file for example "javac" or "javaw"'
1111
Write-Host 'For example enable javac with: "jenv link javac"'
1212
Write-Host 'List all registered java versions with "jenv list"'
13+
return
1314
}
14-
else {
1515

16-
$payload = @'
16+
$payload = @'
1717
@echo off
1818
for /f "delims=" %%i in ('jenv getjava') do set "var=%%i"
1919
@@ -25,7 +25,6 @@ function Invoke-Link {
2525
)
2626
'@ -f $executable
2727

28-
Set-Content ((get-item $PSScriptRoot).parent.fullname + "/$executable.bat") $payload
28+
Set-Content ((get-item $PSScriptRoot).parent.fullname + "/$executable.bat") $payload
2929

30-
}
3130
}

src/jenv-list.psm1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ function Invoke-List {
1111
Write-Host 'Then you can set them with various commands like "jenv use" or "jenv change"'
1212
Write-Host "This command will also tell you every local JEnv that you specified"
1313
Write-Host 'You can tell JEnv that it should always use jdk8 on the desktop with "jenv local"'
14+
return
1415
}
15-
else {
1616

17-
Write-Host "All avaible versions of java"
18-
Write-Host ($config.jenvs | Format-Table | Out-String)
19-
Write-Host "All locally specified versions"
20-
Write-Host ($config.locals | Format-Table | Out-String)
21-
}
17+
Write-Host "All avaible versions of java"
18+
Write-Host ($config.jenvs | Format-Table | Out-String)
19+
Write-Host "All locally specified versions"
20+
Write-Host ($config.locals | Format-Table | Out-String)
2221

2322
}

src/jenv-local.psm1

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,38 @@ function Invoke-Local {
1111
Write-Host 'This is overwriten by "jenv use"'
1212
Write-Host '<name> is the alias of the JEnv you want to specify'
1313
Write-Host "Attention! You might have to call jenv first before it changes your JAVA_HOME to the local environment. The java command will work out of the box"
14+
return
1415
}
15-
else {
1616

17-
# Remove the local JEnv
18-
if ($name -eq "remove") {
19-
$config.locals = @($config.locals | Where-Object { $_.path -ne (Get-Location) })
20-
Write-Output "Your local JEnv was unset"
21-
return
22-
}
23-
24-
# Check if specified JEnv is avaible
25-
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
26-
if ($null -eq $jenv) {
27-
Write-Output "Theres no JEnv with name $name Consider using `"jenv list`""
28-
return
29-
}
17+
# Remove the local JEnv
18+
if ($name -eq "remove") {
19+
$config.locals = @($config.locals | Where-Object { $_.path -ne (Get-Location) })
20+
Write-Output "Your local JEnv was unset"
21+
return
22+
}
3023

31-
# Check if path is already used
32-
foreach ($jenv in $config.locals) {
33-
if ($jenv.path -eq (Get-Location)) {
34-
# if path is used replace with new version
35-
$jenv.name = $name
36-
Write-Output ("Your replaced your java version for {0} {1}" -f (Get-Location), $name)
37-
return
38-
}
39-
}
24+
# Check if specified JEnv is avaible
25+
$jenv = $config.jenvs | Where-Object { $_.name -eq $name }
26+
if ($null -eq $jenv) {
27+
Write-Output "Theres no JEnv with name $name Consider using `"jenv list`""
28+
return
29+
}
4030

41-
# Add new JEnv
42-
$config.locals += [PSCustomObject]@{
43-
path = (Get-Location).path
44-
name = $name
31+
# Check if path is already used
32+
foreach ($jenv in $config.locals) {
33+
if ($jenv.path -eq (Get-Location)) {
34+
# if path is used replace with new version
35+
$jenv.name = $name
36+
Write-Output ("Your replaced your java version for {0} {1}" -f (Get-Location), $name)
37+
return
4538
}
39+
}
4640

47-
Write-Output ("{0} is now your local java version for {1}" -f (Get-Location), $name)
41+
# Add new JEnv
42+
$config.locals += [PSCustomObject]@{
43+
path = (Get-Location).path
44+
name = $name
4845
}
46+
47+
Write-Output ("{0} is now your local java version for {1}" -f (Get-Location), $name)
4948
}

src/jenv-remove.psm1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ function Invoke-Remove {
99
Write-Host '"jenv remove" <name>'
1010
Write-Host 'With this command you can remove any java version you registered with "jenv add"'
1111
Write-Host '<name> is the alias you asigned to the path with "jenv add <name> <path>"'
12+
return
1213
}
13-
else {
14-
# Remove the JEnv
15-
$config.jenvs = @($config.jenvs | Where-Object { $_.name -ne $name })
16-
# Remove any jenv local with that name
17-
$config.locals = @($config.locals | Where-Object { $_.name -ne $name })
18-
Write-Output 'Your JEnv was removed successfully'
19-
}
14+
15+
# Remove the JEnv
16+
$config.jenvs = @($config.jenvs | Where-Object { $_.name -ne $name })
17+
# Remove any jenv local with that name
18+
$config.locals = @($config.locals | Where-Object { $_.name -ne $name })
19+
Write-Output 'Your JEnv was removed successfully'
2020
}

src/jenv-uninstall.psm1

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,67 @@ function Invoke-Uninstall {
99
Write-Host '"jenv uninstall" <name>'
1010
Write-Host 'This command deletes jenv and restores the specified jenv as java'
1111
Write-Host '<name> is the alias you asigned to the path with "jenv add <name> <path>"'
12+
return
1213
}
13-
else {
1414

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-
}
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+
}
2121

22-
# Abort Uninstall
23-
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) {
24-
Write-Host "Aborted uninstallation"
25-
return
26-
}
22+
# Abort Uninstall
23+
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) {
24+
Write-Host "Aborted uninstallation"
25+
return
26+
}
2727

28-
#region Restore the specified java version
28+
#region Restore the specified java version
2929

30-
# Restore PATH
31-
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
32-
$systemPath = [System.Environment]::GetEnvironmentVariable("PATH", "MACHINE").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
30+
# Restore PATH
31+
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
32+
$systemPath = [System.Environment]::GetEnvironmentVariable("PATH", "MACHINE").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
3333

34-
# Filter out the jenv path
35-
$root = (get-item $PSScriptRoot).parent.fullname
36-
$userPath = ($userPath | Where-Object { $_ -ne $root } ) -join ";"
37-
$systemPath = ($systemPath | Where-Object { $_ -ne $root } ) -join ";"
34+
# Filter out the jenv path
35+
$root = (get-item $PSScriptRoot).parent.fullname
36+
$userPath = ($userPath | Where-Object { $_ -ne $root } ) -join ";"
37+
$systemPath = ($systemPath | Where-Object { $_ -ne $root } ) -join ";"
3838

39-
#Update user path
40-
$userPath = $userPath + ";" + $jenv.path + "\bin"
39+
#Update user path
40+
$userPath = $userPath + ";" + $jenv.path + "\bin"
4141

42-
# Set the new PATH
43-
$path = $userPath + ";" + $systemPath
44-
$Env:PATH = $path # Set for powershell users
45-
if ($output) {
46-
Set-Content -path "jenv.path.tmp" -value $path # Create temp file so no restart of the active shell is required
47-
}
42+
# Set the new PATH
43+
$path = $userPath + ";" + $systemPath
44+
$Env:PATH = $path # Set for powershell users
45+
if ($output) {
46+
Set-Content -path "jenv.path.tmp" -value $path # Create temp file so no restart of the active shell is required
47+
}
4848

49-
# Restore JAVA_HOME
50-
$Env:JAVA_HOME = $jenv.path # Set for powershell users
51-
if ($output) {
52-
Set-Content -path "jenv.home.tmp" -value $jenv.path # Create temp file so no restart of the active shell is required
53-
}
49+
# Restore JAVA_HOME
50+
$Env:JAVA_HOME = $jenv.path # Set for powershell users
51+
if ($output) {
52+
Set-Content -path "jenv.home.tmp" -value $jenv.path # Create temp file so no restart of the active shell is required
53+
}
5454

55-
# Set globally
56-
Write-Host "JEnv is changing your environment variables. This process could take longer"
57-
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $javahome, [System.EnvironmentVariableTarget]::User)
58-
[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) # Set globally
55+
# Set globally
56+
Write-Host "JEnv is changing your environment variables. This process could take longer"
57+
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $javahome, [System.EnvironmentVariableTarget]::User)
58+
[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) # Set globally
5959

60-
# Either delete %appdata%/jenv or keep config
61-
$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
62-
if ($uninstall -eq 1) {
63-
Remove-Item $env:appdata/jenv -recurse -force
64-
}
65-
#endregion
60+
# Either delete %appdata%/jenv or keep config
61+
$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
62+
if ($uninstall -eq 1) {
63+
Remove-Item $env:appdata/jenv -recurse -force
64+
}
65+
#endregion
6666

67-
# Delete jenv folder
68-
Remove-Item (get-item $PSScriptRoot).Parent.FullName -Recurse -Force
67+
# Delete jenv folder
68+
Remove-Item (get-item $PSScriptRoot).Parent.FullName -Recurse -Force
6969

70-
# Exit the script so jenv.ps1 wont continue to run
71-
Write-Host "Successfully uninstalled JEnv"
72-
Exit 0
70+
# Exit the script so jenv.ps1 wont continue to run
71+
Write-Host "Successfully uninstalled JEnv"
72+
Exit 0
7373

7474

75-
}
7675
}

0 commit comments

Comments
 (0)