flashjdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +209 -0
- package/bin/flashjdk.js +73 -0
- package/lib/jdk-service.js +409 -0
- package/package.json +46 -0
- package/scripts/flashjdk.ps1 +428 -0
- package/scripts/flashjdk.sh +497 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# JDK 路径一键切换脚本
|
|
3
|
+
# 支持:扫描已安装JDK、手动输入路径、自动更新系统PATH
|
|
4
|
+
# 支持:本地缓存自定义扫描根目录
|
|
5
|
+
# ============================================================
|
|
6
|
+
|
|
7
|
+
# 缓存目录固定使用 AppData\Roaming,不受脚本启动方式影响
|
|
8
|
+
$script:CacheDir = Join-Path ([Environment]::GetFolderPath('ApplicationData')) "flashjdk"
|
|
9
|
+
$script:CacheFile = Join-Path $script:CacheDir "jdk-roots-cache.json"
|
|
10
|
+
$script:DefaultRoots = @(
|
|
11
|
+
"C:\Program Files\Java",
|
|
12
|
+
"C:\Program Files (x86)\Java",
|
|
13
|
+
"D:\Java",
|
|
14
|
+
"D:\ProgramFiles\Java",
|
|
15
|
+
"E:\Java",
|
|
16
|
+
"$env:USERPROFILE\.jdks"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# 自动检测版本号(从 package.json 读取,兼容 npm 全局安装和开发环境)
|
|
20
|
+
function Get-ScriptVersion {
|
|
21
|
+
$scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Path }
|
|
22
|
+
$candidates = @(
|
|
23
|
+
(Join-Path $scriptDir "..\package.json"),
|
|
24
|
+
(Join-Path $scriptDir "package.json")
|
|
25
|
+
)
|
|
26
|
+
foreach ($candidate in $candidates) {
|
|
27
|
+
if (Test-Path $candidate) {
|
|
28
|
+
try { return (Get-Content $candidate -Raw | ConvertFrom-Json).version } catch {}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return "unknown"
|
|
32
|
+
}
|
|
33
|
+
$script:Version = Get-ScriptVersion
|
|
34
|
+
|
|
35
|
+
# ════ 工具函数 ════════════════════════════════════════════════
|
|
36
|
+
|
|
37
|
+
function Write-Log {
|
|
38
|
+
param([string]$Message, [string]$Level = "INFO")
|
|
39
|
+
$time = Get-Date -Format "HH:mm:ss"
|
|
40
|
+
switch ($Level) {
|
|
41
|
+
"INFO" { Write-Host "[$time] [INFO] $Message" -ForegroundColor Cyan }
|
|
42
|
+
"SUCCESS" { Write-Host "[$time] [SUCCESS] $Message" -ForegroundColor Green }
|
|
43
|
+
"WARN" { Write-Host "[$time] [WARN] $Message" -ForegroundColor Yellow }
|
|
44
|
+
"ERROR" { Write-Host "[$time] [ERROR] $Message" -ForegroundColor Red }
|
|
45
|
+
"TITLE" { Write-Host $Message -ForegroundColor Magenta }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function Write-Separator {
|
|
50
|
+
Write-Host ("=" * 60) -ForegroundColor DarkGray
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Test-Admin {
|
|
54
|
+
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
55
|
+
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
|
|
56
|
+
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function Get-CurrentJdkPaths {
|
|
60
|
+
$sysPaths = [Environment]::GetEnvironmentVariable("Path", "Machine") -split ";"
|
|
61
|
+
return $sysPaths | Where-Object { $_ -imatch 'jdk|jre' -and $_ -ne "" }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function Find-JdkInstallations {
|
|
65
|
+
param([string[]]$SearchRoots)
|
|
66
|
+
$found = @()
|
|
67
|
+
foreach ($root in $SearchRoots) {
|
|
68
|
+
$rootPath = Normalize-Path $root
|
|
69
|
+
if (Test-PathSafe $rootPath) {
|
|
70
|
+
$candidates = @($rootPath)
|
|
71
|
+
$candidates += Get-ChildItem -LiteralPath $rootPath -Directory -ErrorAction SilentlyContinue |
|
|
72
|
+
ForEach-Object { $_.FullName }
|
|
73
|
+
|
|
74
|
+
foreach ($candidate in $candidates) {
|
|
75
|
+
if (Test-PathSafe (Join-Path $candidate "bin\java.exe")) {
|
|
76
|
+
$found += (Normalize-Path $candidate)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return $found | Select-Object -Unique
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function Test-IsJdkEntry {
|
|
85
|
+
param([string]$Entry)
|
|
86
|
+
if ([string]::IsNullOrWhiteSpace($Entry)) { return $false }
|
|
87
|
+
|
|
88
|
+
$e = [System.Environment]::ExpandEnvironmentVariables($Entry.Trim().Trim('"').TrimEnd('\').TrimEnd('/'))
|
|
89
|
+
if (Test-Path -LiteralPath (Join-Path $e "java.exe")) { return $true }
|
|
90
|
+
|
|
91
|
+
$vendorPattern = '(jdk|jre|java|openjdk|temurin|zulu|corretto|graal|dragonwell|kona|semeru|liberica|oracle)'
|
|
92
|
+
return ($e -imatch "\\$vendorPattern[^\\]*\\bin$") -or
|
|
93
|
+
($e -imatch "\\$vendorPattern[^\\]*\\jre\\bin$")
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
# 规范化路径:去除首尾空白/引号/尾随反斜杠,解析为绝对路径
|
|
97
|
+
function Normalize-Path {
|
|
98
|
+
param([string]$RawPath)
|
|
99
|
+
if ([string]::IsNullOrWhiteSpace($RawPath)) { return "" }
|
|
100
|
+
# 去除首尾空白、引号、尾随反斜杠
|
|
101
|
+
$p = $RawPath.Trim().Trim('"').TrimEnd('\').TrimEnd('/')
|
|
102
|
+
if ($p -eq "") { return "" }
|
|
103
|
+
# 如果路径存在,解析为规范绝对路径;否则使用 GetFullPath
|
|
104
|
+
try {
|
|
105
|
+
if (Test-Path -LiteralPath $p) {
|
|
106
|
+
return (Resolve-Path -LiteralPath $p).Path
|
|
107
|
+
}
|
|
108
|
+
} catch { }
|
|
109
|
+
try {
|
|
110
|
+
return [System.IO.Path]::GetFullPath($p)
|
|
111
|
+
} catch {
|
|
112
|
+
return $p
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
# 检查路径是否存在(-LiteralPath 避免 [] 等被当作通配符;双重检查 Test-Path + .NET)
|
|
117
|
+
function Test-PathSafe {
|
|
118
|
+
param([string]$Path)
|
|
119
|
+
if ([string]::IsNullOrWhiteSpace($Path)) { return $false }
|
|
120
|
+
$p = [System.Environment]::ExpandEnvironmentVariables($Path.Trim())
|
|
121
|
+
if (Test-Path -LiteralPath $p) { return $true }
|
|
122
|
+
try { return [System.IO.Directory]::Exists($p) } catch { return $false }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function Update-SystemJdkPath {
|
|
126
|
+
param([string]$NewJdkRoot)
|
|
127
|
+
|
|
128
|
+
$newBin = Join-Path $NewJdkRoot "bin"
|
|
129
|
+
$newJre = Join-Path $NewJdkRoot "jre\bin"
|
|
130
|
+
|
|
131
|
+
$machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
|
|
132
|
+
$parts = $machinePath -split ";" | Where-Object { $_ -ne "" }
|
|
133
|
+
|
|
134
|
+
$toRemove = $parts | Where-Object { Test-IsJdkEntry $_ }
|
|
135
|
+
$cleaned = $parts | Where-Object { -not (Test-IsJdkEntry $_) }
|
|
136
|
+
|
|
137
|
+
if ($toRemove.Count -gt 0) {
|
|
138
|
+
Write-Log "将移除以下旧 JDK 条目:" "WARN"
|
|
139
|
+
$toRemove | ForEach-Object { Write-Log " 移除: $_" "WARN" }
|
|
140
|
+
} else {
|
|
141
|
+
Write-Log "PATH 中未发现旧 JDK 条目,直接追加新条目。" "INFO"
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
$newEntries = @($newBin)
|
|
145
|
+
if (Test-Path $newJre) { $newEntries += $newJre }
|
|
146
|
+
|
|
147
|
+
$finalParts = $newEntries + $cleaned
|
|
148
|
+
$newMachinePath = ($finalParts | Select-Object -Unique) -join ";"
|
|
149
|
+
|
|
150
|
+
[Environment]::SetEnvironmentVariable("Path", $newMachinePath, "Machine")
|
|
151
|
+
Write-Log "系统 PATH(Machine) 已更新,其他条目未改动。" "SUCCESS"
|
|
152
|
+
|
|
153
|
+
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
|
|
154
|
+
if ($userPath) {
|
|
155
|
+
$env:Path = "$newMachinePath;$userPath"
|
|
156
|
+
} else {
|
|
157
|
+
$env:Path = $newMachinePath
|
|
158
|
+
}
|
|
159
|
+
Write-Log "当前会话 PATH 已同步 (Machine + User)。" "SUCCESS"
|
|
160
|
+
|
|
161
|
+
return $newBin
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
# ════ 缓存读写函数 ════════════════════════════════════════════
|
|
165
|
+
|
|
166
|
+
function Read-CachedRoots {
|
|
167
|
+
if (Test-Path $script:CacheFile) {
|
|
168
|
+
try {
|
|
169
|
+
$json = [System.IO.File]::ReadAllText($script:CacheFile, [System.Text.Encoding]::UTF8)
|
|
170
|
+
$obj = $json | ConvertFrom-Json
|
|
171
|
+
$raw = $obj.customRoots
|
|
172
|
+
if ($null -eq $raw) { return @() }
|
|
173
|
+
# ConvertFrom-Json 仅一条时返回 string,多条时返回 array;@($raw) 统一为列表
|
|
174
|
+
# 注意:不要用 `return ,$arr` 这种“包一层”的写法,否则会把 string[] 变成 object[]{string[]},
|
|
175
|
+
# 显示时会出现 System.String[] 之类的异常。
|
|
176
|
+
$rawList = @($raw)
|
|
177
|
+
$paths = @(
|
|
178
|
+
$rawList |
|
|
179
|
+
ForEach-Object { Normalize-Path $_ } |
|
|
180
|
+
Where-Object { $_ -ne "" } |
|
|
181
|
+
# 过滤历史异常:曾被错误写入为 "System.String[]"(或其被 GetFullPath 拼成的绝对路径)
|
|
182
|
+
Where-Object { $_ -notmatch '(^|\\)System\.String\[\]$' }
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
# 若发现异常条目,自动清理缓存文件,避免 UI 再次显示脏数据
|
|
186
|
+
if ($paths.Count -ne $rawList.Count) {
|
|
187
|
+
Write-Log "检测到异常缓存项,已自动清理。" "WARN"
|
|
188
|
+
Save-CachedRoots -Roots ([string[]]$paths)
|
|
189
|
+
}
|
|
190
|
+
return [string[]]$paths
|
|
191
|
+
} catch {
|
|
192
|
+
Write-Log "读取缓存失败:$_" "WARN"
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return @()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function Save-CachedRoots {
|
|
199
|
+
param([string[]]$Roots)
|
|
200
|
+
try {
|
|
201
|
+
if (-not (Test-Path $script:CacheDir)) {
|
|
202
|
+
New-Item -ItemType Directory -Path $script:CacheDir -Force | Out-Null
|
|
203
|
+
}
|
|
204
|
+
# 规范化所有路径后再保存,确保缓存格式一致
|
|
205
|
+
$normalized = $Roots |
|
|
206
|
+
ForEach-Object { Normalize-Path $_ } |
|
|
207
|
+
Where-Object { $_ -ne "" } |
|
|
208
|
+
Where-Object { $_ -notmatch '(^|\\)System\.String\[\]$' } |
|
|
209
|
+
Select-Object -Unique
|
|
210
|
+
$obj = [ordered]@{ customRoots = @($normalized) }
|
|
211
|
+
$json = $obj | ConvertTo-Json -Depth 3
|
|
212
|
+
[System.IO.File]::WriteAllText($script:CacheFile, $json, [System.Text.Encoding]::UTF8)
|
|
213
|
+
Write-Log "已保存到:$script:CacheFile" "SUCCESS"
|
|
214
|
+
} catch {
|
|
215
|
+
Write-Log "保存缓存失败:$_" "ERROR"
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function Get-AllSearchRoots {
|
|
220
|
+
$cached = @(Read-CachedRoots)
|
|
221
|
+
$all = $script:DefaultRoots + $cached | Select-Object -Unique
|
|
222
|
+
return [string[]]$all
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
# ════ CLI 命令:-list(列出所有 JDK 环境)═════════════════════
|
|
226
|
+
function Invoke-ListCommand {
|
|
227
|
+
$allRoots = Get-AllSearchRoots
|
|
228
|
+
$jdkList = Find-JdkInstallations -SearchRoots $allRoots
|
|
229
|
+
|
|
230
|
+
Write-Host ""
|
|
231
|
+
Write-Log "当前 JAVA_HOME:$($env:JAVA_HOME)" "INFO"
|
|
232
|
+
Write-Host ""
|
|
233
|
+
|
|
234
|
+
if ($jdkList.Count -gt 0) {
|
|
235
|
+
Write-Log "扫描到 $($jdkList.Count) 个 JDK 环境:" "SUCCESS"
|
|
236
|
+
Write-Host ""
|
|
237
|
+
for ($i = 0; $i -lt $jdkList.Count; $i++) {
|
|
238
|
+
$marker = if ($jdkList[$i] -eq $env:JAVA_HOME) { "*" } else { " " }
|
|
239
|
+
Write-Host (" $marker [{0}] {1}" -f ($i + 1), $jdkList[$i]) -ForegroundColor White
|
|
240
|
+
}
|
|
241
|
+
Write-Host ""
|
|
242
|
+
Write-Host " (* 表示当前使用的 JDK)" -ForegroundColor DarkGray
|
|
243
|
+
} else {
|
|
244
|
+
Write-Log "未扫描到 JDK 环境。" "WARN"
|
|
245
|
+
Write-Host ""
|
|
246
|
+
Write-Host " 请使用 flashjdk -set <路径> 添加扫描根目录。" -ForegroundColor Green
|
|
247
|
+
}
|
|
248
|
+
Write-Host ""
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
# ════ CLI 命令:-set <路径>(添加扫描根目录)═══════════════════
|
|
252
|
+
function Invoke-SetCommand {
|
|
253
|
+
param([string]$NewPath)
|
|
254
|
+
|
|
255
|
+
if ([string]::IsNullOrWhiteSpace($NewPath)) {
|
|
256
|
+
Write-Log "缺少路径参数。用法:flashjdk -set <路径>" "ERROR"
|
|
257
|
+
Write-Host ""
|
|
258
|
+
exit 1
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
$normalized = Normalize-Path $NewPath
|
|
262
|
+
|
|
263
|
+
if (-not (Test-PathSafe $normalized)) {
|
|
264
|
+
Write-Log "路径不存在:$normalized" "ERROR"
|
|
265
|
+
exit 1
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
$cached = @(Read-CachedRoots)
|
|
269
|
+
|
|
270
|
+
if ($cached -contains $normalized -or $script:DefaultRoots -contains $normalized) {
|
|
271
|
+
Write-Log "该路径已在缓存中,无需重复添加。" "WARN"
|
|
272
|
+
exit 0
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
$newList = [System.Collections.Generic.List[string]]@($cached)
|
|
276
|
+
$newList.Add($normalized)
|
|
277
|
+
Save-CachedRoots -Roots $newList.ToArray()
|
|
278
|
+
Write-Log "已添加扫描根目录:$normalized" "SUCCESS"
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
# ════ CLI 命令:-change(选择并切换 JDK)════════════════════════
|
|
282
|
+
function Invoke-ChangeCommand {
|
|
283
|
+
if (-not (Test-Admin)) {
|
|
284
|
+
Write-Log "未检测到管理员权限!修改系统 PATH 需要管理员身份运行。" "ERROR"
|
|
285
|
+
Write-Log "请以管理员身份运行 PowerShell 后重试。" "WARN"
|
|
286
|
+
exit 1
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
$allRoots = Get-AllSearchRoots
|
|
290
|
+
|
|
291
|
+
Write-Host ""
|
|
292
|
+
Write-Log "正在扫描 JDK 环境..." "INFO"
|
|
293
|
+
|
|
294
|
+
$jdkList = Find-JdkInstallations -SearchRoots $allRoots
|
|
295
|
+
|
|
296
|
+
if ($jdkList.Count -eq 0) {
|
|
297
|
+
Write-Log "未扫描到 JDK 环境。" "WARN"
|
|
298
|
+
Write-Host ""
|
|
299
|
+
Write-Host " 请使用 flashjdk -set <路径> 添加扫描根目录后重试。" -ForegroundColor Green
|
|
300
|
+
exit 1
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
Write-Host ""
|
|
304
|
+
Write-Log "当前 JAVA_HOME:$($env:JAVA_HOME)" "INFO"
|
|
305
|
+
Write-Host ""
|
|
306
|
+
|
|
307
|
+
Write-Log "扫描到以下 JDK 版本:" "SUCCESS"
|
|
308
|
+
Write-Host ""
|
|
309
|
+
for ($i = 0; $i -lt $jdkList.Count; $i++) {
|
|
310
|
+
$marker = if ($jdkList[$i] -eq $env:JAVA_HOME) { "*" } else { " " }
|
|
311
|
+
Write-Host (" $marker [{0}] {1}" -f ($i + 1), $jdkList[$i]) -ForegroundColor White
|
|
312
|
+
}
|
|
313
|
+
Write-Host ""
|
|
314
|
+
Write-Host " (* 表示当前使用的 JDK)" -ForegroundColor DarkGray
|
|
315
|
+
Write-Host ""
|
|
316
|
+
|
|
317
|
+
Write-Host "请输入要切换的 JDK 序号(输入 Q 取消):" -ForegroundColor Yellow
|
|
318
|
+
|
|
319
|
+
while ($true) {
|
|
320
|
+
$userInput = (Read-Host ">>> ").Trim()
|
|
321
|
+
if ($userInput -eq "") {
|
|
322
|
+
Write-Log "输入为空,请重新输入。" "ERROR"
|
|
323
|
+
continue
|
|
324
|
+
}
|
|
325
|
+
if ($userInput.ToUpper() -eq "Q") {
|
|
326
|
+
Write-Log "已取消。" "INFO"
|
|
327
|
+
exit 0
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if ($userInput -match "^\d+$") {
|
|
331
|
+
$idx = [int]$userInput - 1
|
|
332
|
+
if ($idx -ge 0 -and $idx -lt $jdkList.Count) {
|
|
333
|
+
$selected = $jdkList[$idx]
|
|
334
|
+
|
|
335
|
+
if (-not (Test-PathSafe $selected)) {
|
|
336
|
+
Write-Log "路径不存在:$selected" "ERROR"
|
|
337
|
+
exit 1
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
$javaBin = Join-Path $selected "bin\java.exe"
|
|
341
|
+
if (-not (Test-Path -LiteralPath $javaBin)) {
|
|
342
|
+
Write-Log "bin\java.exe 不存在,请确认选择的是 JDK 根目录。" "ERROR"
|
|
343
|
+
exit 1
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
Write-Host ""
|
|
347
|
+
Write-Separator
|
|
348
|
+
Write-Log "正在切换 JDK 到:$selected" "INFO"
|
|
349
|
+
|
|
350
|
+
$newBinPath = Update-SystemJdkPath -NewJdkRoot $selected
|
|
351
|
+
|
|
352
|
+
$oldJavaHome = [Environment]::GetEnvironmentVariable("JAVA_HOME", "Machine")
|
|
353
|
+
[Environment]::SetEnvironmentVariable("JAVA_HOME", $selected, "Machine")
|
|
354
|
+
$env:JAVA_HOME = $selected
|
|
355
|
+
Write-Log "JAVA_HOME 已设置为:$selected" "SUCCESS"
|
|
356
|
+
|
|
357
|
+
Write-Host ""
|
|
358
|
+
Write-Separator
|
|
359
|
+
Write-Log "正在验证 java -version..." "INFO"
|
|
360
|
+
Write-Host ""
|
|
361
|
+
try {
|
|
362
|
+
$javaExe = Join-Path $selected "bin\java.exe"
|
|
363
|
+
& $javaExe -version 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor White }
|
|
364
|
+
Write-Host ""
|
|
365
|
+
Write-Log "java -version 执行成功!" "SUCCESS"
|
|
366
|
+
} catch {
|
|
367
|
+
Write-Log "执行 java -version 时出错:$_" "ERROR"
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
Write-Separator
|
|
371
|
+
Write-Log "JDK 切换完成!新开命令窗口将自动生效。" "SUCCESS"
|
|
372
|
+
Write-Log "当前窗口已立即生效,无需重启。" "INFO"
|
|
373
|
+
exit 0
|
|
374
|
+
} else {
|
|
375
|
+
Write-Log "序号无效,请重新输入。" "ERROR"
|
|
376
|
+
}
|
|
377
|
+
} else {
|
|
378
|
+
Write-Log "请输入数字序号。" "ERROR"
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
# ════ 帮助信息 ══════════════════════════════════════════════════
|
|
384
|
+
function Show-Help {
|
|
385
|
+
Write-Host ""
|
|
386
|
+
Write-Host " flashJDK v$script:Version — JDK 版本一键切换工具" -ForegroundColor Magenta
|
|
387
|
+
Write-Host ""
|
|
388
|
+
Write-Host " 用法:" -ForegroundColor Green
|
|
389
|
+
Write-Host ""
|
|
390
|
+
Write-Host " flashjdk -v 查看版本号"
|
|
391
|
+
Write-Host " flashjdk -list 列出所有扫描到的 JDK 环境"
|
|
392
|
+
Write-Host " flashjdk -set <路径> 添加自定义扫描根目录"
|
|
393
|
+
Write-Host " flashjdk -change 列出所有 JDK 版本并选择切换"
|
|
394
|
+
Write-Host ""
|
|
395
|
+
Write-Host " 缓存文件:$script:CacheFile" -ForegroundColor DarkGray
|
|
396
|
+
Write-Host ""
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
# ════ CLI 参数分发 ══════════════════════════════════════════════
|
|
400
|
+
$Command = if ($args.Count -gt 0) { $args[0] } else { "" }
|
|
401
|
+
$Value = if ($args.Count -gt 1) { $args[1] } else { "" }
|
|
402
|
+
|
|
403
|
+
switch ($Command) {
|
|
404
|
+
"-v" {
|
|
405
|
+
Write-Host "v$script:Version"
|
|
406
|
+
}
|
|
407
|
+
"--version" {
|
|
408
|
+
Write-Host "v$script:Version"
|
|
409
|
+
}
|
|
410
|
+
"-list" {
|
|
411
|
+
Invoke-ListCommand
|
|
412
|
+
}
|
|
413
|
+
"-set" {
|
|
414
|
+
Invoke-SetCommand -NewPath $Value
|
|
415
|
+
}
|
|
416
|
+
"-change" {
|
|
417
|
+
Invoke-ChangeCommand
|
|
418
|
+
}
|
|
419
|
+
"" {
|
|
420
|
+
Show-Help
|
|
421
|
+
}
|
|
422
|
+
default {
|
|
423
|
+
Write-Host "未知参数: $Command"
|
|
424
|
+
Write-Host ""
|
|
425
|
+
Show-Help
|
|
426
|
+
exit 1
|
|
427
|
+
}
|
|
428
|
+
}
|