150 lines
4.2 KiB
PowerShell
150 lines
4.2 KiB
PowerShell
param()
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
function Write-Log {
|
|
param([string]$Message)
|
|
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
Write-Host "[$ts] $Message"
|
|
}
|
|
|
|
function Add-PathIfMissing {
|
|
param([string]$Dir)
|
|
if ([string]::IsNullOrWhiteSpace($Dir)) {
|
|
return
|
|
}
|
|
if (-not (Test-Path $Dir)) {
|
|
return
|
|
}
|
|
$pathParts = $env:Path -split ';' | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
|
|
if ($pathParts -notcontains $Dir) {
|
|
$env:Path = ($pathParts + $Dir) -join ';'
|
|
}
|
|
}
|
|
|
|
function Find-7Zip {
|
|
$candidates = @()
|
|
$programFiles = $env:ProgramFiles
|
|
$programFilesX86 = ${env:ProgramFiles(x86)}
|
|
if (-not $programFilesX86) {
|
|
$programFilesX86 = [Environment]::GetFolderPath("ProgramFilesX86")
|
|
}
|
|
if ($programFiles) {
|
|
$candidates += (Join-Path -Path $programFiles -ChildPath "7-Zip\7z.exe")
|
|
}
|
|
if ($programFilesX86 -and $programFilesX86 -ne $programFiles) {
|
|
$candidates += (Join-Path -Path $programFilesX86 -ChildPath "7-Zip\7z.exe")
|
|
}
|
|
foreach ($candidate in $candidates) {
|
|
if ($candidate -and (Test-Path $candidate)) {
|
|
return $candidate
|
|
}
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Find-JavaBin {
|
|
$roots = @()
|
|
$programFiles = $env:ProgramFiles
|
|
if (-not $programFiles) {
|
|
$programFiles = [Environment]::GetFolderPath("ProgramFiles")
|
|
}
|
|
$programFilesX86 = ${env:ProgramFiles(x86)}
|
|
if (-not $programFilesX86) {
|
|
$programFilesX86 = [Environment]::GetFolderPath("ProgramFilesX86")
|
|
}
|
|
foreach ($base in @($programFiles, $programFilesX86)) {
|
|
if ($base) {
|
|
$roots += (Join-Path -Path $base -ChildPath "Eclipse Adoptium")
|
|
$roots += (Join-Path -Path $base -ChildPath "Java")
|
|
}
|
|
}
|
|
$roots = $roots | Where-Object { $_ -and (Test-Path $_) }
|
|
|
|
foreach ($root in $roots) {
|
|
$found = Get-ChildItem -Path $root -Recurse -Filter "java.exe" -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.FullName -match '\\bin\\java\.exe$' } |
|
|
Select-Object -ExpandProperty FullName -First 1
|
|
if ($found) {
|
|
return $found
|
|
}
|
|
}
|
|
return $null
|
|
}
|
|
|
|
$sevenZipPath = Find-7Zip
|
|
|
|
$javaBin = Find-JavaBin
|
|
|
|
$need7z = -not (Get-Command 7z -ErrorAction SilentlyContinue)
|
|
$needJava = -not (Get-Command java -ErrorAction SilentlyContinue)
|
|
if (-not $need7z -and -not $needJava) {
|
|
Write-Log "All dependencies already satisfied."
|
|
exit 0
|
|
}
|
|
|
|
$missing = @()
|
|
if ($need7z) { $missing += "7z" }
|
|
if ($needJava) { $missing += "java" }
|
|
Write-Log "Missing dependencies detected: $($missing -join ', ')"
|
|
|
|
$installed = $false
|
|
if (Get-Command winget -ErrorAction SilentlyContinue) {
|
|
if ($need7z) {
|
|
Write-Log "Installing 7-Zip via winget..."
|
|
winget install --id 7zip.7zip -e --source winget
|
|
}
|
|
if ($needJava) {
|
|
Write-Log "Installing Java 11 (Temurin) via winget..."
|
|
winget install --id EclipseAdoptium.Temurin.11.JRE -e --source winget
|
|
}
|
|
$installed = $true
|
|
} elseif (Get-Command choco -ErrorAction SilentlyContinue) {
|
|
if ($need7z) {
|
|
Write-Log "Installing 7-Zip via choco..."
|
|
choco install -y 7zip
|
|
}
|
|
if ($needJava) {
|
|
Write-Log "Installing Java 11 (Temurin) via choco..."
|
|
choco install -y temurin11jre
|
|
}
|
|
$installed = $true
|
|
} else {
|
|
Write-Error "Neither winget nor choco found. Please install 7-Zip and Java 11+ manually."
|
|
exit 1
|
|
}
|
|
|
|
if (-not $installed) {
|
|
Write-Error "Failed to install 7-Zip."
|
|
exit 1
|
|
}
|
|
|
|
if ($need7z) {
|
|
if (-not $sevenZipPath) {
|
|
$sevenZipPath = Find-7Zip
|
|
}
|
|
if ($sevenZipPath) {
|
|
Add-PathIfMissing -Dir (Split-Path -Parent $sevenZipPath)
|
|
}
|
|
}
|
|
|
|
if ($needJava) {
|
|
if (-not $javaBin) {
|
|
$javaBin = Find-JavaBin
|
|
}
|
|
if ($javaBin) {
|
|
Add-PathIfMissing -Dir (Split-Path -Parent $javaBin)
|
|
}
|
|
}
|
|
|
|
if (-not (Get-Command 7z -ErrorAction SilentlyContinue)) {
|
|
Write-Error "7z still not found after install. Please check your PATH."
|
|
exit 1
|
|
}
|
|
if (-not (Get-Command java -ErrorAction SilentlyContinue)) {
|
|
Write-Error "Java still not found after install. Please check your PATH."
|
|
exit 1
|
|
}
|
|
|
|
Write-Log "Dependencies installed successfully."
|