Skip to content

Commit 4cf53f0

Browse files
committed
Implemented #32 A discovery search feature for java.exe
1 parent b7b123b commit 4cf53f0

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/jenv-autoscan.psm1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function Invoke-AutoScan {
2+
param (
3+
[Parameter(Mandatory = $true)][object]$config,
4+
[Parameter(Mandatory = $true)][boolean]$help,
5+
[string]$path
6+
)
7+
8+
if ($help) {
9+
Write-Host '"jenv autoscan <path>"'
10+
Write-Host 'This will search for any java.exe files in the given path and prompt the user to add them to JEnv'
11+
Write-Host '<path> is the path to search like "C:\Program Files\Java"'
12+
Write-Host 'If <path> is not provided, JEnv will search the entire system'
13+
return
14+
}
15+
16+
$paths = @($path)
17+
if ( $path -eq "") {
18+
# Get Drives including Temp folders
19+
$drives = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Root
20+
# Only keep the physical drive letter
21+
$drives = $drives | ForEach-Object { $_.Substring(0, 3) }
22+
# Only keep unique
23+
$paths = $drives | Select-Object -Unique
24+
}
25+
# Check if the provided path exists
26+
elseif (!(Test-Path -Path $path -PathType Container)) {
27+
Write-Host "The provided path does not exist"
28+
return
29+
}
30+
31+
# Iterate over paths and find java.exe
32+
Write-Host "JEnv is now searching for java.exe on your Computer. This could take some time..."
33+
$javaExecutables = @()
34+
foreach ($path in $paths) {
35+
$path = $path + "\\"
36+
$files = Get-ChildItem -Path $path -Recurse -File -ErrorAction "SilentlyContinue" | Where-Object { $_.FullName.EndsWith("\bin\java.exe") }
37+
if ($null -ne $files) {
38+
$files | ForEach-Object {
39+
$javaExecutables += $_.FullName
40+
}
41+
}
42+
}
43+
44+
# Filter out jenv tests java.exe
45+
$root = (get-item $PSScriptRoot).parent.fullname
46+
$javaExecutables = $javaExecutables | Where-Object { $_.Contains($root) -eq $false }
47+
48+
# Ask user if java.exe should be added to the list
49+
foreach ($java in $javaExecutables) {
50+
$version = Get-JavaVersion $java
51+
switch (Open-Prompt "JEnv autoscan" ("Found java.exe at {0}. Default name is: '{1}'. Do you want to add it to the list?" -f $java, $version) "Yes", "No", "Rename" ("This will add {0} with alias '{1}' to JEnv" -f $java, $version), ("Skip {0}" -f $java), "Change the default name" 1) {
52+
0 {
53+
Invoke-Add $config $false $version ($java -replace "\\bin\\java\.exe$", "")
54+
}
55+
2 {
56+
Invoke-Add $config $false (Read-Host ("Enter the new name for {0}" -f $java)) ($java -replace "\\bin\\java\.exe$", "")
57+
}
58+
}
59+
}
60+
61+
}

src/jenv.ps1

Lines changed: 5 additions & 2 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", "uninstall")] [string]$action,
21+
[Parameter(Position = 0)][validateset("list", "add", "change", "use", "remove", "local", "getjava", "link", "uninstall", "autoscan")] [string]$action,
2222

2323
# Displays this helpful message
2424
[Alias("h")]
@@ -42,6 +42,7 @@ Import-Module $PSScriptRoot\jenv-local.psm1 -Force
4242
Import-Module $PSScriptRoot\jenv-getjava.psm1 -Force
4343
Import-Module $PSScriptRoot\jenv-link.psm1 -Force
4444
Import-Module $PSScriptRoot\jenv-uninstall.psm1 -Force
45+
Import-Module $PSScriptRoot\jenv-autoscan.psm1 -Force
4546
#endregion
4647

4748
#region Installation
@@ -128,6 +129,7 @@ if ($help -and $action -eq "") {
128129
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'
129130
Write-Host '"jenv link <executable>" Creates shortcuts for executables inside JAVA_HOME. For example "javac"'
130131
Write-Host '"jenv uninstall <name>" Deletes JEnv and restores the specified java version to the system. You may keep your config file'
132+
Write-Host '"jenv autoscan ?<path>?" Will scan the given path for java installations and ask to add them to JEnv. Path is optional'
131133
Write-Host 'Get help for individual commands using "jenv <list/add/remove/change/use/local> --help"'
132134
}
133135
else {
@@ -143,7 +145,8 @@ else {
143145
local { Invoke-Local $config $help @arguments }
144146
getjava { Get-Java $config }
145147
link { Invoke-Link $help @arguments }
146-
uninstall { Invoke-Uninstall $help $config @arguments }
148+
uninstall { Invoke-Uninstall $config $help @arguments }
149+
autoscan { Invoke-AutoScan $config $help @arguments }
147150
}
148151

149152
#region Save the config

src/util.psm1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@ function Open-Prompt {
1313
}
1414

1515
return $Host.UI.PromptForChoice($title, $question, $options, $default_choice)
16+
}
17+
18+
function Get-JavaVersion {
19+
param (
20+
[Parameter(Mandatory = $true)][string]$javaexe
21+
)
22+
$version = (Get-Command $javaexe | Select-Object -ExpandProperty Version).toString()
23+
$version = $version -replace "(?>\.0)*(?!.+)", "" # Remove trailing zeros
24+
return $version
1625
}

0 commit comments

Comments
 (0)