1- import de.undercouch.gradle.tasks.download.Download
21import org.jetbrains.intellij.tasks.PrepareSandboxTask
32import org.jetbrains.kotlin.gradle.dsl.JvmTarget
4- import java.net.URI
53import java.security.MessageDigest
64import java.util.zip.ZipFile
7- import kotlin.io.path.deleteExisting
85
96plugins {
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
5654val 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+
5860dependencies {
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
7082configurations {
@@ -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}
0 commit comments