Skip to content

Commit a026338

Browse files
committed
Build: download PSES using the Gradle dependency engine
1 parent 6c20e19 commit a026338

File tree

5 files changed

+62
-98
lines changed

5 files changed

+62
-98
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/language_host/
21
/src/main/gen-parser
32
/src/main/gen-lexer
43

build.gradle.kts

Lines changed: 60 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import de.undercouch.gradle.tasks.download.Download
21
import org.jetbrains.intellij.tasks.PrepareSandboxTask
32
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
4-
import java.net.URI
53
import java.security.MessageDigest
64
import java.util.zip.ZipFile
7-
import kotlin.io.path.deleteExisting
85

96
plugins {
107
id("java")
118
alias(libs.plugins.changelog)
12-
alias(libs.plugins.download)
139
alias(libs.plugins.gradleJvmWrapper)
1410
alias(libs.plugins.grammarkit)
1511
alias(libs.plugins.intellij)
@@ -41,30 +37,46 @@ repositories {
4137
mavenCentral()
4238
ivy {
4339
url = uri("https://github.com/PowerShell/PSScriptAnalyzer/releases/download/")
44-
patternLayout {
45-
artifact("[revision]/[module].[revision].[ext]")
46-
}
47-
content {
48-
includeGroup("PSScriptAnalyzer")
49-
}
50-
metadataSources {
51-
artifact()
52-
}
40+
patternLayout { artifact("[revision]/[module].[revision].[ext]") }
41+
content { includeGroup("PSScriptAnalyzer") }
42+
metadataSources { artifact() }
43+
}
44+
ivy {
45+
url = uri("https://github.com/PowerShell/PowerShellEditorServices/releases/download/")
46+
patternLayout { artifact("v[revision]/[module].[ext]") }
47+
content { includeGroup("PowerShellEditorServices") }
48+
metadataSources { artifact() }
5349
}
5450
}
5551

52+
val psScriptAnalyzerVersion: String by project
53+
val psScriptAnalyzerSha256Hash: String by project
5654
val psScriptAnalyzer: Configuration by configurations.creating
5755

56+
val psesVersion: String by project
57+
val psesSha256Hash: String by project
58+
val powerShellEditorServices: Configuration by configurations.creating
59+
5860
dependencies {
5961
implementation(libs.bundles.junixsocket)
6062

6163
implementation(libs.lsp4j)
6264
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
6365
testImplementation(libs.junit)
6466

65-
libs.psScriptAnalyzer.get().apply {
66-
psScriptAnalyzer(group = this.group!!, name = this.name, version = this.version, ext = "nupkg")
67-
}
67+
psScriptAnalyzer(
68+
group = "PSScriptAnalyzer",
69+
name = "PSScriptAnalyzer",
70+
version = psScriptAnalyzerVersion,
71+
ext = "nupkg"
72+
)
73+
74+
powerShellEditorServices(
75+
group = "PowerShellEditorServices",
76+
name = "PowerShellEditorServices",
77+
version = psesVersion,
78+
ext = "zip"
79+
)
6880
}
6981

7082
configurations {
@@ -119,68 +131,31 @@ tasks {
119131
}
120132
}
121133

122-
fun getDependencyTask(
123-
dependencyName: String,
124-
version: String,
125-
expectedHash: String,
126-
uri: URI,
127-
destination: RegularFile,
128-
customizeZip: Action<CopySpec>
129-
): TaskProvider<Copy> {
130-
val download by register<Download>("download$dependencyName") {
131-
group = "dependencies"
132-
133-
inputs.property("version", version)
134-
inputs.property("hash", expectedHash)
135-
136-
// NOTE: Do not overwrite: the verification step should delete an incorrect file.
137-
// NOTE: Notably, this property allows us to skip the task completely if no inputs change.
138-
overwrite(false)
139-
140-
src(uri)
141-
dest(destination)
142-
143-
doLast {
144-
println("Calculating hash for $dependencyName")
145-
val data = destination.asFile.readBytes()
146-
val hash = MessageDigest.getInstance("SHA-256").let { sha256 ->
147-
sha256.update(data)
148-
sha256.digest().joinToString("") { "%02x".format(it) }
149-
}
150-
println("Expected hash for $dependencyName = $expectedHash")
151-
println("Calculated hash for $dependencyName = $hash")
152-
if (!hash.equals(expectedHash, ignoreCase = true)) {
153-
destination.asFile.toPath().deleteExisting()
154-
error("$dependencyName hash check failed.\n" +
155-
"The downloaded file has been deleted.\n" +
156-
"Please try running the task again, or update the expected hash in the gradle.properties file.")
157-
}
158-
}
134+
fun File.verifyHash(expectedHash: String) {
135+
println("Calculating hash for $name...")
136+
val data = readBytes()
137+
val hash = MessageDigest.getInstance("SHA-256").let { sha256 ->
138+
sha256.update(data)
139+
sha256.digest().joinToString("") { "%02x".format(it) }
159140
}
160-
161-
return register<Copy>("get$dependencyName") {
162-
group = "dependencies"
163-
164-
val outDir = projectDir.resolve("language_host/LanguageHost/modules/$dependencyName")
165-
doFirst {
166-
if (!outDir.deleteRecursively()) error("Cannot delete \"$outDir\".")
167-
}
168-
169-
dependsOn(download)
170-
from(zipTree(destination)) {
171-
customizeZip(this)
172-
}
173-
into(outDir)
141+
println("Expected hash for $name = $expectedHash")
142+
println("Calculated hash for $name = $hash")
143+
if (!hash.equals(expectedHash, ignoreCase = true)) {
144+
error("$name hash check failed.\n" +
145+
"Please try re-downloading the dependency, or update the expected hash in the gradle.properties file.")
174146
}
175147
}
176148

177-
val downloads = layout.buildDirectory.get().dir("download")
178-
179-
val psesVersion: String by project
180-
val psesSha256Hash: String by project
149+
val verifyPsScriptAnalyzer by registering {
150+
dependsOn(psScriptAnalyzer)
151+
inputs.property("hash", psScriptAnalyzerSha256Hash)
152+
doFirst {
153+
psScriptAnalyzer.singleFile.verifyHash(psScriptAnalyzerSha256Hash)
154+
}
155+
}
181156

182157
fun PrepareSandboxTask.unpackPsScriptAnalyzer(outDir: String) {
183-
dependsOn(psScriptAnalyzer)
158+
dependsOn(psScriptAnalyzer, verifyPsScriptAnalyzer)
184159

185160
from(zipTree(psScriptAnalyzer.singleFile)) {
186161
into("$outDir/PSScriptAnalyzer")
@@ -193,34 +168,27 @@ tasks {
193168
}
194169
}
195170

196-
val getPowerShellEditorServices = getDependencyTask(
197-
"PowerShellEditorServices",
198-
psesVersion,
199-
psesSha256Hash,
200-
URI(
201-
"https://github.com/PowerShell/PowerShellEditorServices/releases/download/" +
202-
"v$psesVersion/PowerShellEditorServices.zip"
203-
),
204-
downloads.file("PowerShellEditorServices.zip")
205-
) {
206-
include("PowerShellEditorServices/**")
207-
eachFile {
208-
relativePath = RelativePath(true, *relativePath.segments.drop(1).toTypedArray())
171+
val verifyPowerShellEditorServices by registering {
172+
dependsOn(powerShellEditorServices)
173+
inputs.property("hash", psesSha256Hash)
174+
doFirst {
175+
powerShellEditorServices.singleFile.verifyHash(psesSha256Hash)
209176
}
210177
}
211178

212-
val getAllDependencies by registering {
213-
dependsOn(getPowerShellEditorServices)
179+
fun PrepareSandboxTask.unpackPowerShellEditorServices(outDir: String) {
180+
dependsOn(powerShellEditorServices, verifyPowerShellEditorServices)
181+
182+
from(zipTree(powerShellEditorServices.singleFile)) {
183+
into("$outDir/")
184+
include("PowerShellEditorServices/**")
185+
}
214186
}
215187

216188
withType<PrepareSandboxTask> {
217-
dependsOn(getAllDependencies)
218189
val outDir = "${intellij.pluginName.get()}/lib/LanguageHost/modules"
219190
unpackPsScriptAnalyzer(outDir)
220-
221-
from("${project.rootDir}/language_host") {
222-
into("${intellij.pluginName.get()}/lib/")
223-
}
191+
unpackPowerShellEditorServices(outDir)
224192
}
225193

226194
val maxUnpackedPluginBytes: String by project
@@ -265,5 +233,4 @@ tasks {
265233
)
266234
})
267235
}
268-
269236
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ kotlin.stdlib.default.dependency=false
33
# 30 MiB:
44
maxUnpackedPluginBytes=31457280
55

6-
# TODO: Move to Gradle verification file
6+
psScriptAnalyzerVersion=1.22.0
77
psScriptAnalyzerSha256Hash=71BFB9EB58E19D4B662F4494A7D572A724B60E0588848DCFF34195A0E08AE1BE
88

99
psesVersion=3.20.1

gradle/libs.versions.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ junixsocket-common = { module = "com.kohlschutter.junixsocket:junixsocket-common
77
junixsocket-native-common = { module = "com.kohlschutter.junixsocket:junixsocket-native-common", version.ref = "junixsocket" }
88
lsp4j = "org.eclipse.lsp4j:org.eclipse.lsp4j:0.23.1"
99
junit = "junit:junit:4.13.2"
10-
psScriptAnalyzer = { group = "PSScriptAnalyzer", name = "PSScriptAnalyzer", version = "1.22.0" }
1110

1211
[bundles]
1312
junixsocket = ["junixsocket-common", "junixsocket-native-common"]
1413

1514
[plugins]
1615
changelog = { id = "org.jetbrains.changelog", version = "2.2.0" }
17-
download = { id = "de.undercouch.download", version = "5.6.0" }
1816
gradleJvmWrapper = "me.filippov.gradle.jvm.wrapper:0.14.0"
1917
grammarkit = { id = "org.jetbrains.grammarkit", version = "2022.3.2.2" }
2018
intellij = { id = "org.jetbrains.intellij", version = "1.17.3" }

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = "PowerShell"
1+
rootProject.name = "PowerShell"

0 commit comments

Comments
 (0)