Skip to content

Commit 85e4555

Browse files
committed
feat: add Detect hook, so that vfox link --detect java finds existing JDKs
Implements #127, part of version-fox/vfox#737. Scans Gradle's jdks folder, IntelliJ IDEA's folder, system directories (/usr/lib/jvm, Program Files), version managers (SDKMAN, asdf, mise, jabba), Homebrew kegs (incl. Linuxbrew), Scoop apps, the CI runner tool cache and JAVA_HOME. winget/Chocolatey/vendor installers land in Program Files and are covered by the same roots; nvm manages Node.js only. Reads each candidate's release file and names the version like install does: runtime version (SEMANTIC_VERSION or leading release+build), GraalVM's own version for that family, then -short name from IMPLEMENTOR and -fx for JavaFX, no suffix for OpenJDK. Includes offline tests in the vfox.test style (test.load with os/arch/env, faked directory listings and release files): naming, bundles, kegs, deep scans, duplicates and .vfox filtering. Bumps the Test Plugin workflow to vfox 1.1.0, which brings the Detect hook.
1 parent 2a4e569 commit 85e4555

9 files changed

Lines changed: 1454 additions & 5 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ jobs:
3434
with:
3535
go-version: '1.24.0'
3636
cache: false
37-
# vfox plugin test came with vfox 1.0.13. The release is pinned and its
38-
# checksum checked, as the actions are pinned by their commits; change the
39-
# two together to move to another release.
37+
# vfox plugin test came with vfox 1.0.13; 1.1.0 brings the Detect
38+
# hook, which tests/detect_test.lua exercises. The release is pinned
39+
# and its checksum checked, as the actions are pinned by their commits;
40+
# change the two together to move to another release.
4041
- name: Install vfox
4142
env:
42-
VFOX_VERSION: 1.0.13
43-
VFOX_SHA256: 7ad0a4c82c8173f09393e09abecc3e27468253bab5fe53ffbf58f2b12b3fc768
43+
VFOX_VERSION: 1.1.0
44+
VFOX_SHA256: 3b33c3c468b27bc17aecf0d6d3af24fe49fe204972984904a136a47b402a0ee4
4445
run: |
4546
archive="$RUNNER_TEMP/vfox.tar.gz"
4647
curl --fail --silent --show-error --location --output "$archive" \

‎docs/development.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ hooks/post_install.lua macOS: move the Java home to the install root
1212
hooks/env_keys.lua JAVA_HOME and PATH
1313
hooks/pre_use.lua vfox use: map what was typed to an installed name
1414
hooks/parse_legacy_file.lua .sdkmanrc
15+
hooks/detect.lua vfox link --detect: find JDKs already on the machine
1516
lib/checksum.lua the checksum of a download, from Foojay's record of the package
1617
lib/distribution_version.lua distribution names, parsing of specs, GraalVM feature releases
1718
lib/foojay.lua the Foojay request, and fixes to what it answers
1819
lib/github_mirror.lua the mirror of the downloads from GitHub
1920
lib/installed_version.lua matching against installed names
2021
lib/loongnix.lua the Loongnix archive
22+
lib/detect_jdk.lua finding installed JDKs by their release file
2123
tests/*_test.lua offline hook tests, run by vfox plugin test
2224
tests/post_install_layouts.lua the macOS layouts PostInstall handles, with its real shell script
2325
tests/fixtures/loongnix/ a saved copy of the Loongnix archive listing
@@ -37,6 +39,7 @@ vfox loads `metadata.lua`, calls a hook when a command needs it, and puts `PLUGI
3739
| `vfox install java@21-tem` | `PreInstall` returns the URL and the name. vfox downloads and unpacks, then calls `PostInstall` |
3840
| `vfox use java@21-tem` | `PreUse` returns the installed name to select. `EnvKeys` provides the variables |
3941
| a shell hook, `cd` into a directory with `.sdkmanrc` | `ParseLegacyFile` |
42+
| `vfox link --detect java` | `Detect` returns the JDKs already on the machine (`lib/detect_jdk.lua`) |
4043
| `vfox uninstall java@21.0.8+9-tem` | none, the name has to be exact |
4144

4245
A few contracts that cost time to find:
@@ -103,6 +106,14 @@ vfox plugin run . ParseLegacyFile --input '{"filename":".sdkmanrc","filepath":"/
103106

104107
`vfox --debug <command>` shows what vfox does with a hook, which is how to find an error it swallows.
105108

109+
## Detecting installed JDKs
110+
111+
`hooks/detect.lua` implements `Detect` for `vfox link --detect java`: it scans Gradle's provisioning folder (`$GRADLE_USER_HOME/jdks`), IntelliJ IDEA's folder (`~/.jdks`, `~/Library/Java/JavaVirtualMachines` on macOS), the system directories (`/usr/lib/jvm`, `/Library/Java/JavaVirtualMachines` on macOS, `C:\Program Files\*` on Windows), the version managers SDKMAN, asdf, mise and jabba, Homebrew kegs (`/opt/homebrew/opt`, `/usr/local/opt`, Linuxbrew), Scoop apps (`~/scoop/apps`), the CI runner tool cache and `JAVA_HOME`, and reports every JDK home it finds as `{ version, path, note }`. winget, Chocolatey and the vendor installers all land in `Program Files`, so the same Windows roots cover them; nvm manages Node.js only, so there is nothing Java-related to find in it.
112+
113+
`lib/detect_jdk.lua` reads each candidate's `release` file. The version is the runtime version (`SEMANTIC_VERSION`, else the leading `release+build` of `JAVA_RUNTIME_VERSION`), GraalVM's own version (`GRAALVM_VERSION`) for that family, then `-<short name>` from `IMPLEMENTOR` and `-fx` where `MODULES` mentions JavaFX, with no suffix for OpenJDK. System OpenJDKs (Ubuntu, Debian, `N/A`) map to no suffix; Oracle Corporation without `oracle` in the path stays OpenJDK, otherwise it is `oracle`. Mandrel's release file is the Temurin JDK it was built from, with no Mandrel version of its own, so it reports as Temurin (`tem`), and Liberica NIK without its own `GRAALVM_VERSION` reports as Liberica (`librca`); both stay usable JDKs under those names. Unknown vendors report with no suffix instead of a made-up one.
114+
115+
`tests/detect_test.lua` (plus `detect_env_test.lua`, `detect_darwin_test.lua` and `detect_windows_test.lua` for the environment, macOS and Windows roots) takes the platform and the environment through `test.load` (`os`, `arch`, `env`), fakes the directory listings and `release` files (`io.popen`, `io.open`) instead of touching real JDKs, and covers the naming, the macOS bundles and Homebrew kegs (`Contents/Home`, `libexec/openjdk.jdk/Contents/Home`), asdf/mise/jabba/Linuxbrew homes, Scoop's nested apps and mise on Windows, duplicate versions (the first path wins) and the `.vfox`-managed homes that must never be reported.
116+
106117
## Adding a distribution
107118

108119
1. Find its id in Foojay ([foojay.md](foojay.md)), and check that it has packages for the platforms you care about.

‎docs/zh-CN/development.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ hooks/post_install.lua macOS:把 Java home 移到安装根目录
1212
hooks/env_keys.lua JAVA_HOME 和 PATH
1313
hooks/pre_use.lua vfox use:把输入映射到已安装的版本名
1414
hooks/parse_legacy_file.lua .sdkmanrc
15+
hooks/detect.lua vfox link --detect:找出本机已有的 JDK
1516
lib/checksum.lua 下载文件的校验和,取自 Foojay 里这个包的记录
1617
lib/distribution_version.lua 发行版名称、版本写法的解析、GraalVM 特性版本的识别
1718
lib/foojay.lua 向 Foojay 发出的请求,以及对返回结果的修正
1819
lib/github_mirror.lua 从 GitHub 下载时用的镜像
1920
lib/installed_version.lua 与已安装的版本名做匹配
2021
lib/loongnix.lua Loongnix 归档
22+
lib/detect_jdk.lua 按 release 文件找出已安装的 JDK
2123
tests/*_test.lua 离线的钩子测试,由 vfox plugin test 运行
2224
tests/post_install_layouts.lua PostInstall 处理的 macOS 目录结构,会真实执行它的 shell 脚本
2325
tests/fixtures/loongnix/ Loongnix 归档目录页的一份快照
@@ -37,6 +39,7 @@ vfox 加载 `metadata.lua`,命令需要时调用相应的钩子,并向钩子
3739
| `vfox install java@21-tem` | `PreInstall` 返回 URL 和版本名。vfox 下载并解压,然后调用 `PostInstall` |
3840
| `vfox use java@21-tem` | `PreUse` 返回要选中的已安装版本名,`EnvKeys` 提供环境变量 |
3941
| 已挂载到 Shell,`cd` 进有 `.sdkmanrc` 的目录 | `ParseLegacyFile` |
42+
| `vfox link --detect java` | `Detect` 返回本机已有的 JDK(`lib/detect_jdk.lua`) |
4043
| `vfox uninstall java@21.0.8+9-tem` | 无,名字必须精确 |
4144

4245
几条容易踩坑的约定:
@@ -103,6 +106,14 @@ vfox plugin run . ParseLegacyFile --input '{"filename":".sdkmanrc","filepath":"/
103106

104107
`vfox --debug <命令>` 会显示 vfox 对钩子做了什么,vfox 吞掉的错误可以靠它找出来。
105108

109+
## 检测已安装的 JDK
110+
111+
`hooks/detect.lua` 为 `vfox link --detect java` 实现 `Detect`:扫描 Gradle 的目录、IntelliJ IDEA 的目录、系统目录(`/usr/lib/jvm`、macOS 上是 `/Library/Java/JavaVirtualMachines`、Windows 上是 `C:\Program Files\*`)、版本管理器 SDKMAN、asdf、mise 和 jabba、Homebrew keg(`/opt/homebrew/opt`、`/usr/local/opt`、Linuxbrew)、Scoop 应用(`~/scoop/apps`)、CI runner 的工具缓存和 `JAVA_HOME`,把找到的每个 JDK 目录以版本、路径和说明返回。winget、Chocolatey 和厂商安装包最终都装到 `Program Files`,所以同一组 Windows 目录就能覆盖;nvm 只管 Node.js,里面没有 Java 可找。
112+
113+
`lib/detect_jdk.lua` 读每个候选目录的 `release` 文件。版本号取运行时的版本,没有则取 `JAVA_RUNTIME_VERSION` 开头的版本加构建号,GraalVM 系取自己的版本号,再加上 `IMPLEMENTOR` 对应的简称,有 JavaFX 的再加 `-fx`,OpenJDK 不加后缀。系统自带的 OpenJDK 映射到无后缀;路径里没有 oracle 的 Oracle Corporation 仍视为 OpenJDK,否则为 `oracle`。Mandrel 的文件就是它基于的 Temurin JDK,没有 Mandrel 自己的版本号,所以会报成 Temurin;没有自己版本号的 Liberica NIK 会报成 Liberica;两种情况下 JDK 本身都能正常使用。认不出的厂商不编造后缀,直接用无后缀的版本号。
114+
115+
`tests/detect_test.lua`(另有 `detect_env_test.lua`、`detect_darwin_test.lua` 和 `detect_windows_test.lua` 分别覆盖环境变量、macOS 和 Windows 的目录)通过 `test.load` 传入平台和环境(`os`、`arch`、`env`),伪造目录列表和 `release` 文件(`io.popen`、`io.open`)而不碰真实的 JDK,覆盖命名规则、macOS 的 bundle 和 Homebrew keg、asdf/mise/jabba/Linuxbrew、Windows 上的 Scoop 嵌套目录和 mise、重复版本和绝不能上报的 `.vfox` 目录。
116+
106117
## 添加发行版
107118

108119
1. 在 Foojay 里找到它的 id(见 [foojay.md](foojay.md)),并确认它在你关心的平台上有包。

‎hooks/detect.lua‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Copyright 2026 Han Li and contributors
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
15+
local detect = require("detect_jdk")
16+
17+
--- Finds JDKs that are already on the machine and that vfox did not install,
18+
--- for `vfox link --detect java`: Gradle's provisioning folder, IntelliJ
19+
--- IDEA's folder, the system directories, SDKMAN and Homebrew, among others.
20+
--- Each entry names the version the way `vfox install` names it.
21+
function PLUGIN:Detect(ctx)
22+
return detect.find_all()
23+
end

0 commit comments

Comments
 (0)