Skip to content

Commit 15acfdb

Browse files
committed
add PowerShell scripts for Windows
1 parent 5bb595b commit 15acfdb

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

install-jdks-windows.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Enable error handling
2+
Set-StrictMode -Version Latest
3+
$ErrorActionPreference = 'Stop'
4+
$ProgressPreference = 'SilentlyContinue'
5+
6+
# winget may not be available for a few minutes if you just signed into Windows for the first time
7+
if (-not (Get-Command 'winget.exe' -ErrorAction SilentlyContinue)) {
8+
# this command will ensure it is available.
9+
Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe -ErrorAction SilentlyContinue
10+
}
11+
12+
# if winget is still not available, it may not be pre-installed in your Windows version
13+
if (-not (Get-Command 'winget.exe' -ErrorAction SilentlyContinue)) {
14+
Write-Host 'Installing WinGet PowerShell module from PSGallery...'
15+
Install-PackageProvider -Name NuGet -Force | Out-Null
16+
Install-Module -Name Microsoft.WinGet.Client -Force -Repository PSGallery | Out-Null
17+
Write-Host 'Using Repair-WinGetPackageManager cmdlet to bootstrap WinGet...'
18+
Repair-WinGetPackageManager
19+
Write-Host 'WinGet.'
20+
}
21+
22+
# install the required JDKs
23+
$jdkVersions = @('8', '11', '17', '21')
24+
25+
foreach ($jdkVersion in $jdkVersions) {
26+
Write-Host "ℹ️ Installing EclipseAdoptium.Temurin.${jdkVersion}.JDK"
27+
winget install --silent --disable-interactivity --accept-package-agreements --source winget --exact --id "EclipseAdoptium.Temurin.${jdkVersion}.JDK"
28+
}
29+
30+
# find the JDK installation paths
31+
if (Test-Path 'C:\Program Files\Eclipse Adoptium') {
32+
$jdkDirs = Get-ChildItem -Path 'C:\Program Files\Eclipse Adoptium' -Directory
33+
} else {
34+
Write-Host '❌ Directory "C:\Program Files\Eclipse Adoptium" does not exist. Cannot set environment variables.'
35+
exit 1
36+
}
37+
38+
# set the required JDK environment variables
39+
foreach ($jdkDir in $jdkDirs) {
40+
if ($jdkDir.Name -match 'jdk-(\d+)\..*-hotspot') {
41+
$jdkDirFullName = $jdkDir.FullName
42+
$version = $matches[1]
43+
$envVarName = "JAVA_${version}_HOME"
44+
Write-Host "ℹ️ Setting $envVarName=$jdkDirFullName"
45+
[System.Environment]::SetEnvironmentVariable($envVarName, $jdkDirFullName, [System.EnvironmentVariableTarget]::User)
46+
}
47+
}
48+
49+
Write-Host 'ℹ️ Setting JAVA_HOME=%JAVA_8_HOME%'
50+
[System.Environment]::SetEnvironmentVariable('JAVA_HOME', '%JAVA_8_HOME%', [System.EnvironmentVariableTarget]::User)

setup.ps1

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Enable error handling
2+
Set-StrictMode -Version Latest
3+
$ErrorActionPreference = 'Stop'
4+
5+
function TestJvm {
6+
param ($JavaHomeName, $ExpectedJavaVersion)
7+
8+
if (-not (Test-Path "env:$JavaHomeName")) {
9+
Write-Host "$JavaHomeName is not set. Please set $JavaHomeName to refer to a JDK $ExpectedJavaVersion installation." -ForegroundColor Red
10+
exit 1
11+
}
12+
else {
13+
$javaHome = Get-Item "env:$JavaHomeName" | Select-Object -ExpandProperty Value
14+
15+
try {
16+
# try to handle differences between PowerShell 7 and Windows PowerShell 5
17+
$ErrorActionPreference = 'Continue'
18+
$javaVersionOutput = & "$javaHome\bin\java.exe" -version 2>&1
19+
}
20+
catch {
21+
Write-Host "❌ Error running `"$javaHome\bin\java.exe -version`". Please check that $JavaHomeName is set to a JDK $ExpectedJavaVersion installation."
22+
exit 1
23+
}
24+
finally {
25+
$ErrorActionPreference = 'Stop'
26+
}
27+
28+
if ($javaVersionOutput[0] -notmatch "version `"$ExpectedJavaVersion") {
29+
Write-Host "$JavaHomeName is set to $javaHome, but it does not refer to a JDK $ExpectedJavaVersion installation." -ForegroundColor Red
30+
exit 1
31+
}
32+
else {
33+
Write-Host "$JavaHomeName is set to $javaHome."
34+
}
35+
}
36+
}
37+
38+
Write-Host 'ℹ️ Checking required JVM:'
39+
if (Test-Path 'env:JAVA_HOME') {
40+
TestJvm 'JAVA_HOME' '1.8'
41+
}
42+
43+
TestJvm 'JAVA_8_HOME' '1.8'
44+
TestJvm 'JAVA_11_HOME' '11'
45+
TestJvm 'JAVA_17_HOME' '17'
46+
TestJvm 'JAVA_21_HOME' '21'
47+
TestJvm 'JAVA_GRAALVM17_HOME' '17'
48+
49+
# Check for required commands (e.g., git, docker)
50+
function TestCommand {
51+
param ($command)
52+
53+
if (Get-Command $command -ErrorAction SilentlyContinue) {
54+
Write-Host "✅ The $command command line is installed."
55+
}
56+
else {
57+
Write-Host "❌ The $command command line is missing. Please install $command." -ForegroundColor Red
58+
exit 1
59+
}
60+
}
61+
62+
function Get-FileHashMD5 {
63+
param ($file)
64+
return (Get-FileHash -Path $file -Algorithm MD5).Hash
65+
}
66+
67+
function TestHook {
68+
param ($hookName)
69+
70+
$hookChecksum = Get-FileHashMD5 ".githooks/$hookName"
71+
$hooksPath = (git config core.hooksPath) -or '.git/hooks'
72+
73+
if ((Test-Path ".git/hooks/$hookName") -and (Get-FileHashMD5 ".git/hooks/$hookName" -eq $hookChecksum)) {
74+
Write-Host "$hookName hook is installed in repository."
75+
}
76+
elseif ((Test-Path "$hooksPath/$hookName") -and (Get-FileHashMD5 "$hooksPath/$hookName" -eq $hookChecksum)) {
77+
Write-Host "$hookName hook is installed in git hooks path."
78+
}
79+
else {
80+
Write-Host "🟨 $hookName hook was not found (optional but recommended)."
81+
}
82+
}
83+
84+
function TestGitConfig {
85+
param ($configName, $expectedValue)
86+
87+
$actualValue = git config $configName
88+
89+
if ($actualValue -eq $expectedValue) {
90+
Write-Host "✅ git config $configName is set to $expectedValue."
91+
}
92+
elseif (-not $actualValue) {
93+
Write-Host "❌ git config $configName is not set. Please set it to $expectedValue." -ForegroundColor Red
94+
}
95+
else {
96+
Write-Host "🟨 git config $configName is set to $actualValue (expected: $expectedValue)."
97+
}
98+
}
99+
100+
Write-Host 'ℹ️ Checking git configuration:'
101+
TestCommand 'git'
102+
TestHook 'pre-commit'
103+
TestGitConfig 'submodule.recurse' 'true'
104+
105+
# Check Docker environment
106+
function TestDockerServer {
107+
try {
108+
# try to handle differences between PowerShell 7 and Windows PowerShell 5
109+
$ErrorActionPreference = 'Continue'
110+
docker info *> $null
111+
112+
if ($LASTEXITCODE -eq 0) {
113+
Write-Host "✅ The Docker server is running."
114+
}
115+
else {
116+
Write-Host "🟨 The Docker server is not running. Please start it to run all tests."
117+
}
118+
}
119+
catch {
120+
Write-Host "❌ Error running `"docker info *> $null`"."
121+
exit 1
122+
}
123+
finally {
124+
$ErrorActionPreference = 'Stop'
125+
}
126+
}
127+
128+
Write-Host 'ℹ️ Checking Docker environment:'
129+
TestCommand 'docker'
130+
TestDockerServer

0 commit comments

Comments
 (0)