Skip to content

Commit

Permalink
Implemented #32 A discovery search feature for java.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixSelter committed Aug 19, 2022
1 parent b7b123b commit 4cf53f0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
61 changes: 61 additions & 0 deletions src/jenv-autoscan.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function Invoke-AutoScan {
param (
[Parameter(Mandatory = $true)][object]$config,
[Parameter(Mandatory = $true)][boolean]$help,
[string]$path
)

if ($help) {
Write-Host '"jenv autoscan <path>"'
Write-Host 'This will search for any java.exe files in the given path and prompt the user to add them to JEnv'
Write-Host '<path> is the path to search like "C:\Program Files\Java"'
Write-Host 'If <path> is not provided, JEnv will search the entire system'
return
}

$paths = @($path)
if ( $path -eq "") {
# Get Drives including Temp folders
$drives = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Root
# Only keep the physical drive letter
$drives = $drives | ForEach-Object { $_.Substring(0, 3) }
# Only keep unique
$paths = $drives | Select-Object -Unique
}
# Check if the provided path exists
elseif (!(Test-Path -Path $path -PathType Container)) {
Write-Host "The provided path does not exist"
return
}

# Iterate over paths and find java.exe
Write-Host "JEnv is now searching for java.exe on your Computer. This could take some time..."
$javaExecutables = @()
foreach ($path in $paths) {
$path = $path + "\\"
$files = Get-ChildItem -Path $path -Recurse -File -ErrorAction "SilentlyContinue" | Where-Object { $_.FullName.EndsWith("\bin\java.exe") }
if ($null -ne $files) {
$files | ForEach-Object {
$javaExecutables += $_.FullName
}
}
}

# Filter out jenv tests java.exe
$root = (get-item $PSScriptRoot).parent.fullname
$javaExecutables = $javaExecutables | Where-Object { $_.Contains($root) -eq $false }

# Ask user if java.exe should be added to the list
foreach ($java in $javaExecutables) {
$version = Get-JavaVersion $java
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) {
0 {
Invoke-Add $config $false $version ($java -replace "\\bin\\java\.exe$", "")
}
2 {
Invoke-Add $config $false (Read-Host ("Enter the new name for {0}" -f $java)) ($java -replace "\\bin\\java\.exe$", "")
}
}
}

}
7 changes: 5 additions & 2 deletions src/jenv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ param (
"jenv use <name>" Applys the given Java-Version locally for the current shell
"jenv local <name>" Will use the given Java-Version whenever in this folder. Will set the Java-version for all subfolders as well
#>
[Parameter(Position = 0)][validateset("list", "add", "change", "use", "remove", "local", "getjava", "link", "uninstall")] [string]$action,
[Parameter(Position = 0)][validateset("list", "add", "change", "use", "remove", "local", "getjava", "link", "uninstall", "autoscan")] [string]$action,

# Displays this helpful message
[Alias("h")]
Expand All @@ -42,6 +42,7 @@ Import-Module $PSScriptRoot\jenv-local.psm1 -Force
Import-Module $PSScriptRoot\jenv-getjava.psm1 -Force
Import-Module $PSScriptRoot\jenv-link.psm1 -Force
Import-Module $PSScriptRoot\jenv-uninstall.psm1 -Force
Import-Module $PSScriptRoot\jenv-autoscan.psm1 -Force
#endregion

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

#region Save the config
Expand Down
9 changes: 9 additions & 0 deletions src/util.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ function Open-Prompt {
}

return $Host.UI.PromptForChoice($title, $question, $options, $default_choice)
}

function Get-JavaVersion {
param (
[Parameter(Mandatory = $true)][string]$javaexe
)
$version = (Get-Command $javaexe | Select-Object -ExpandProperty Version).toString()
$version = $version -replace "(?>\.0)*(?!.+)", "" # Remove trailing zeros
return $version
}

0 comments on commit 4cf53f0

Please sign in to comment.