feat(server): JDK 탐색 우선순위 재정의 (.mc_custom → 환경변수 → Program Files)
요청 사양대로 탐색 순서 변경: ① .mc_custom/jdk(설치기 자동설치 위치)가 있으면 먼저 사용(권장이면 match, 아니면 경고) ② 환경변수(JAVA_HOME/JDK_HOME)가 권장 버전이면 사용 ③ 기본 폴더 C:\Program Files\Java 에 권장 버전 있으면 사용 ④ 없으면 환경변수 자바(없으면 Program Files 자바)로 폴백 + "권장과 다름" 경고 소스별 후보 수집 헬퍼(mcCustom/env/programFiles) 분리. 문서도 갱신. 0.4.8→0.4.9. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -334,42 +334,88 @@ function jdkHomesUnder(parent: string): string[] {
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* 컴퓨터에 설치된 JDK 홈 목록을 우선순위대로 모은다(중복 제거).
|
||||
* 순서: ① 기본 자바 설치 위치 C:\Program Files\Java → ② 환경변수(JAVA_HOME/JDK_HOME)
|
||||
* → ③ 설치기 자동 설치 위치(.mc_custom/jdk, 구버전 %APPDATA%/jdk 호환).
|
||||
*/
|
||||
function collectJdkHomes(): string[] {
|
||||
const homes: string[] = []
|
||||
const push = (h: string): void => {
|
||||
if (h && !homes.some((x) => x.toLowerCase() === h.toLowerCase())) homes.push(h)
|
||||
interface JdkEntry { home: string; major: number }
|
||||
|
||||
/** 홈 목록 → {home, major} 목록(중복/버전0 제거). */
|
||||
function homesToEntries(homes: string[]): JdkEntry[] {
|
||||
const javaName = process.platform === 'win32' ? 'java.exe' : 'java'
|
||||
const seen = new Set<string>()
|
||||
const out: JdkEntry[] = []
|
||||
for (const home of homes) {
|
||||
const key = home.toLowerCase()
|
||||
if (seen.has(key)) continue
|
||||
seen.add(key)
|
||||
const major = getJavaMajor(path.join(home, 'bin', javaName))
|
||||
if (major > 0) out.push({ home, major })
|
||||
}
|
||||
for (const h of jdkHomesUnder('C:\\Program Files\\Java')) push(h)
|
||||
for (const env of [process.env.JAVA_HOME, process.env.JDK_HOME]) {
|
||||
if (env) push(resolveJdkHome(env))
|
||||
}
|
||||
for (const base of [installerJdkRoot(), path.join(getAppDataDir(), 'jdk')]) {
|
||||
for (const h of jdkHomesUnder(base)) push(h)
|
||||
}
|
||||
return homes
|
||||
return out
|
||||
}
|
||||
|
||||
// 권장 JDK 를 우선 찾는다. C:\Program Files\Java → 환경변수 → 설치기 자동설치 위치를
|
||||
// 훑어, ① 권장 버전과 정확히 일치하는 JDK 가 있으면 그걸 쓰고(match=true),
|
||||
// ② 없으면 가장 높은 버전을 대신 고르되 "권장과 다름"(match=false)으로 표시해 렌더러가
|
||||
// 경고를 띄우게 한다. ③ 아무 JDK 도 없으면 not found → 자동 설치 유도.
|
||||
/** 설치기 자동 설치 위치(.mc_custom/jdk, 구버전 %APPDATA%/jdk 호환)의 JDK 들. */
|
||||
function mcCustomJdkEntries(): JdkEntry[] {
|
||||
const homes: string[] = []
|
||||
for (const base of [installerJdkRoot(), path.join(getAppDataDir(), 'jdk')]) {
|
||||
homes.push(...jdkHomesUnder(base))
|
||||
}
|
||||
return homesToEntries(homes)
|
||||
}
|
||||
|
||||
/** 환경변수(JAVA_HOME/JDK_HOME)에 등록된 JDK 들. */
|
||||
function envJdkEntries(): JdkEntry[] {
|
||||
const homes: string[] = []
|
||||
for (const env of [process.env.JAVA_HOME, process.env.JDK_HOME]) {
|
||||
if (env) {
|
||||
const h = resolveJdkHome(env)
|
||||
if (h) homes.push(h)
|
||||
}
|
||||
}
|
||||
return homesToEntries(homes)
|
||||
}
|
||||
|
||||
/** 기본 자바 설치 위치 C:\Program Files\Java 아래의 JDK 들. */
|
||||
function programFilesJdkEntries(): JdkEntry[] {
|
||||
return homesToEntries(jdkHomesUnder('C:\\Program Files\\Java'))
|
||||
}
|
||||
|
||||
// JDK 탐색 우선순위(요청 사양):
|
||||
// ① .mc_custom/jdk(설치기가 설치해둔 위치)가 있으면 먼저 사용 — 권장 버전이면 그대로,
|
||||
// 혹시 다른 버전이면 경고(match=false).
|
||||
// ② 환경변수(JAVA_HOME/JDK_HOME) 가 권장 버전이면 사용.
|
||||
// ③ 기본 폴더 C:\Program Files\Java 에 권장 버전이 있으면 사용.
|
||||
// ④ 없으면 환경변수 자바로 폴백하고 "권장과 다름" 경고(match=false).
|
||||
// (환경변수 자바도 없으면 Program Files 자바로 폴백, 그것도 없으면 not found → 자동 설치 유도.)
|
||||
ipcMain.handle('jdk:detect', async (_event, recommendedInput?: number) => {
|
||||
const required = normalizeRecommendedJdk(recommendedInput)
|
||||
const javaName = process.platform === 'win32' ? 'java.exe' : 'java'
|
||||
const found = collectJdkHomes()
|
||||
.map((home) => ({ home, major: getJavaMajor(path.join(home, 'bin', javaName)) }))
|
||||
.filter((x) => x.major > 0)
|
||||
const recommended = (entries: JdkEntry[]): JdkEntry | undefined => entries.find((e) => e.major === required)
|
||||
const best = (entries: JdkEntry[]): JdkEntry => entries.slice().sort((a, b) => b.major - a.major)[0]
|
||||
|
||||
const exact = found.find((x) => x.major === required)
|
||||
if (exact) return { found: true, path: exact.home, major: required, match: true }
|
||||
if (found.length > 0) {
|
||||
const best = found.slice().sort((a, b) => b.major - a.major)[0]
|
||||
return { found: true, path: best.home, major: best.major, match: false }
|
||||
// ① .mc_custom/jdk 우선.
|
||||
const mc = mcCustomJdkEntries()
|
||||
if (mc.length > 0) {
|
||||
const rec = recommended(mc)
|
||||
if (rec) return { found: true, path: rec.home, major: required, match: true }
|
||||
const b = best(mc)
|
||||
return { found: true, path: b.home, major: b.major, match: false }
|
||||
}
|
||||
|
||||
// ② 환경변수 JDK 가 권장 버전이면 사용.
|
||||
const env = envJdkEntries()
|
||||
const envRec = recommended(env)
|
||||
if (envRec) return { found: true, path: envRec.home, major: required, match: true }
|
||||
|
||||
// ③ 기본 폴더(Program Files\Java)에 권장 버전이 있으면 사용.
|
||||
const pf = programFilesJdkEntries()
|
||||
const pfRec = recommended(pf)
|
||||
if (pfRec) return { found: true, path: pfRec.home, major: required, match: true }
|
||||
|
||||
// ④ 폴백: 환경변수 자바 → (없으면) Program Files 자바 + 경고.
|
||||
if (env.length > 0) {
|
||||
const b = best(env)
|
||||
return { found: true, path: b.home, major: b.major, match: false }
|
||||
}
|
||||
if (pf.length > 0) {
|
||||
const b = best(pf)
|
||||
return { found: true, path: b.home, major: b.major, match: false }
|
||||
}
|
||||
return { found: false, path: '', major: required, match: false }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user