1+ #
2+ # Licensed to the Apache Software Foundation (ASF) under one
3+ # or more contributor license agreements. See the NOTICE file
4+ # distributed with this work for additional information
5+ # regarding copyright ownership. The ASF licenses this file
6+ # to you under the Apache License, Version 2.0 (the
7+ # "License"); you may not use this file except in compliance
8+ # with the License. You may obtain a copy of the License at
9+ #
10+ # http://www.apache.org/licenses/LICENSE-2.0
11+ #
12+ # Unless required by applicable law or agreed to in writing,
13+ # software distributed under the License is distributed on an
14+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ # KIND, either express or implied. See the License for the
16+ # specific language governing permissions and limitations
17+ # under the License.
18+
19+ # Binary, especially executable binary files, shall not be in the source tree of any Apache project.
20+ # Gradle usually requires a `gradle-wrapper.jar` file to be present in the source tree.
21+ # This script, included from `gradlew` downloads the gradle-wrapper.jar if necessary and verifies its integrity.
22+ # The `gradle-wrapper.jar` and its checksum are downloaded from two well-known locations.
23+
24+ # This fixes an issue that Get-FileHash works fine "out of the box" on older Windows version, but fails on
25+ # newer ones. More info via https://github.com/actions/runner-images/issues/225
26+ Import-Module $PSHOME \Modules\Microsoft.PowerShell.Utility - Function Get-FileHash
27+
28+ # Extract the Gradle version from gradle-wrapper.properties using a regular expression.
29+ $GradlePropertiesPath = Join-Path - Path $env: APP_HOME - ChildPath " gradle\wrapper\gradle-wrapper.properties"
30+ if (-not (Test-Path $GradlePropertiesPath )) {
31+ Write-Error " Gradle properties file not found: $GradlePropertiesPath "
32+ exit 1
33+ }
34+
35+ # Read the content and use a regex match to capture the version number.
36+ # Bash regex: 's/^.*gradle-\([0-9.]*\)-[a-z]*.zip$/\1/'
37+ # PowerShell equivalent: Match the distributionUrl line, then capture the version group.
38+ $GRADLE_DIST_VERSION = (
39+ Select-String - Path $GradlePropertiesPath - CaseSensitive `
40+ - Pattern ' distributionUrl=' |
41+ ForEach-Object {
42+ if ($_ -match ' gradle-([\d.]+)-[a-z]+\.zip' ) {
43+ $Matches [1 ]
44+ }
45+ }
46+ )
47+
48+ # Define file paths
49+ $GRADLE_WRAPPER_SHA256 = Join-Path - Path $env: APP_HOME - ChildPath " gradle\wrapper\gradle-wrapper-$GRADLE_DIST_VERSION .jar.sha256"
50+ $GRADLE_WRAPPER_JAR = Join-Path - Path $env: APP_HOME - ChildPath " gradle\wrapper\gradle-wrapper.jar"
51+
52+ # Checksum Verification and Cleanup
53+ # If the checksum file does not exist, delete the wrapper jar.
54+ if (-not (Test-Path $GRADLE_WRAPPER_SHA256 )) {
55+ Remove-Item - Path $GRADLE_WRAPPER_JAR - Force - ErrorAction SilentlyContinue
56+ }
57+
58+ # If the wrapper jar exists, verify its checksum.
59+ if (Test-Path $GRADLE_WRAPPER_JAR ) {
60+ try {
61+ # Calculate the SHA256 hash of the existing wrapper JAR.
62+ # Get-FileHash is the native PowerShell equivalent for sha256sum.
63+ $JarHashObject = Get-FileHash - Path $GRADLE_WRAPPER_JAR - Algorithm SHA256
64+ $JAR_CHECKSUM = $JarHashObject.Hash.ToLower () # Hash is uppercase by default, convert to lowercase
65+
66+ # Read the expected checksum from the file.
67+ # Note: 'cat' is an alias for Get-Content in PowerShell.
68+ $EXPECTED = (Get-Content - Path $GRADLE_WRAPPER_SHA256 - Raw).Trim().ToLower()
69+
70+ # Compare checksums and delete files if they do not match.
71+ if ($JAR_CHECKSUM -ne $EXPECTED ) {
72+ Write-Warning " Checksum mismatch. Deleting $GRADLE_WRAPPER_JAR and $GRADLE_WRAPPER_SHA256 ."
73+ Remove-Item - Path $GRADLE_WRAPPER_JAR - Force - ErrorAction SilentlyContinue
74+ Remove-Item - Path $GRADLE_WRAPPER_SHA256 - Force - ErrorAction SilentlyContinue
75+ }
76+ } catch {
77+ # Handle cases where Get-Content or Get-FileHash might fail (e.g., file deleted during operation).
78+ Write-Warning " Error during checksum verification: $ ( $_.Exception.Message ) "
79+ Remove-Item - Path $GRADLE_WRAPPER_JAR - Force - ErrorAction SilentlyContinue
80+ Remove-Item - Path $GRADLE_WRAPPER_SHA256 - Force - ErrorAction SilentlyContinue
81+ }
82+ }
83+
84+ # Download Checksum File
85+ # If the checksum file is missing, download it.
86+ if (-not (Test-Path $GRADLE_WRAPPER_SHA256 )) {
87+ $Sha256DownloadUrl = " https://services.gradle.org/distributions/gradle-$GRADLE_DIST_VERSION -wrapper.jar.sha256"
88+ Write-Host " Downloading SHA256 checksum from $Sha256DownloadUrl "
89+ # Invoke-WebRequest is the native PowerShell equivalent for curl --location --output.
90+ try {
91+ Invoke-WebRequest - Uri $Sha256DownloadUrl - OutFile $GRADLE_WRAPPER_SHA256 - UseBasicParsing - ErrorAction Stop
92+ } catch {
93+ Write-Error " Failed to download SHA256 checksum: $ ( $_.Exception.Message ) "
94+ exit 1
95+ }
96+ }
97+
98+ # Download Wrapper JAR and Final Verification
99+ # If the wrapper jar is missing, download it.
100+ if (-not (Test-Path $GRADLE_WRAPPER_JAR )) {
101+ # The original script handles a case where the version might be like 'x.y' and needs 'x.y.0'.
102+ # Bash sed: 's/^\([0-9]*[.][0-9]*\)$/\1.0/'
103+ # PowerShell equivalent using regex replacement.
104+ $GRADLE_VERSION = $GRADLE_DIST_VERSION
105+ if ($GRADLE_DIST_VERSION -match ' ^\d+\.\d+$' ) {
106+ $GRADLE_VERSION = " $GRADLE_DIST_VERSION .0"
107+ }
108+
109+ $JarDownloadUrl = " https://raw.githubusercontent.com/gradle/gradle/v$GRADLE_VERSION /gradle/wrapper/gradle-wrapper.jar"
110+ Write-Host " Downloading wrapper JAR from $JarDownloadUrl "
111+
112+ try {
113+ Invoke-WebRequest - Uri $JarDownloadUrl - OutFile $GRADLE_WRAPPER_JAR - UseBasicParsing - ErrorAction Stop
114+ } catch {
115+ Write-Error " Failed to download wrapper JAR: $ ( $_.Exception.Message ) "
116+ exit 1
117+ }
118+
119+ # Verify the checksum of the newly downloaded JAR.
120+ try {
121+ $JarHashObject = Get-FileHash - Path $GRADLE_WRAPPER_JAR - Algorithm SHA256
122+ $JAR_CHECKSUM = $JarHashObject.Hash.ToLower ()
123+ $EXPECTED = (Get-Content - Path $GRADLE_WRAPPER_SHA256 - Raw).Trim().ToLower()
124+
125+ if ($JAR_CHECKSUM -ne $EXPECTED ) {
126+ # Critical failure: downloaded file does not match its expected checksum.
127+ Write-Error " Expected sha256 of the downloaded $GRADLE_WRAPPER_JAR does not match the downloaded sha256!"
128+ exit 1
129+ }
130+ } catch {
131+ Write-Error " Error during final checksum verification: $ ( $_.Exception.Message ) "
132+ exit 1
133+ }
134+ }
0 commit comments