|
1 | 1 | import de.undercouch.gradle.tasks.download.Download
|
| 2 | +import org.intellij.markdown.html.URI |
2 | 3 | import org.jetbrains.intellij.tasks.PrepareSandboxTask
|
3 | 4 | import java.security.MessageDigest
|
4 | 5 | import java.util.zip.ZipFile
|
@@ -99,62 +100,107 @@ tasks {
|
99 | 100 | kotlinOptions.jvmTarget = "17"
|
100 | 101 | }
|
101 | 102 |
|
102 |
| - val downloads = layout.buildDirectory.get().dir("download") |
103 |
| - |
104 |
| - val psScriptAnalyzerVersion: String by project |
105 |
| - val psScriptAnalyzerSha256Hash: String by project |
106 |
| - |
107 |
| - val psScriptAnalyzerFileName = "PSScriptAnalyzer.$psScriptAnalyzerVersion.nupkg" |
108 |
| - val psScriptAnalyzerOutFile = downloads.file(psScriptAnalyzerFileName) |
109 |
| - |
110 |
| - val downloadPsScriptAnalyzer by register<Download>("downloadPsScriptAnalyzer") { |
111 |
| - group = "dependencies" |
112 |
| - |
113 |
| - inputs.property("version", psScriptAnalyzerVersion) |
114 |
| - inputs.property("hash", psScriptAnalyzerSha256Hash) |
115 |
| - |
116 |
| - // NOTE: Do not overwrite: the verification step should delete an incorrect file. |
117 |
| - // NOTE: Notably, this property allows us to skip the task completely if no inputs change. |
118 |
| - overwrite(false) |
| 103 | + fun getDependencyTask( |
| 104 | + dependencyName: String, |
| 105 | + version: String, |
| 106 | + expectedHash: String, |
| 107 | + uri: URI, |
| 108 | + destination: RegularFile, |
| 109 | + customizeZip: Action<CopySpec> |
| 110 | + ): TaskProvider<Copy> { |
| 111 | + val download by register<Download>("download$dependencyName") { |
| 112 | + group = "dependencies" |
| 113 | + |
| 114 | + inputs.property("version", version) |
| 115 | + inputs.property("hash", expectedHash) |
| 116 | + |
| 117 | + // NOTE: Do not overwrite: the verification step should delete an incorrect file. |
| 118 | + // NOTE: Notably, this property allows us to skip the task completely if no inputs change. |
| 119 | + overwrite(false) |
| 120 | + |
| 121 | + src(uri) |
| 122 | + dest(destination) |
| 123 | + |
| 124 | + doLast { |
| 125 | + val data = destination.asFile.readBytes() |
| 126 | + val hash = MessageDigest.getInstance("SHA-256").let { sha256 -> |
| 127 | + sha256.update(data) |
| 128 | + sha256.digest().joinToString("") { "%02x".format(it) } |
| 129 | + } |
| 130 | + if (!hash.equals(expectedHash, ignoreCase = true)) { |
| 131 | + destination.asFile.toPath().deleteExisting() |
| 132 | + error("$dependencyName hash check failed. Expected $expectedHash, but got $hash\n" + |
| 133 | + "The downloaded file has been deleted.\n" + |
| 134 | + "Please try running the task again, or update the expected hash in the gradle.properties file.") |
| 135 | + } |
| 136 | + } |
| 137 | + } |
119 | 138 |
|
120 |
| - src("https://github.com/PowerShell/PSScriptAnalyzer/releases/download/" + |
121 |
| - "$psScriptAnalyzerVersion/$psScriptAnalyzerFileName") |
122 |
| - dest(psScriptAnalyzerOutFile) |
| 139 | + return register<Copy>("get$dependencyName") { |
| 140 | + group = "dependencies" |
123 | 141 |
|
124 |
| - doLast { |
125 |
| - val data = psScriptAnalyzerOutFile.asFile.readBytes() |
126 |
| - val hash = MessageDigest.getInstance("SHA-256").let { sha256 -> |
127 |
| - sha256.update(data) |
128 |
| - sha256.digest().joinToString("") { "%02x".format(it) } |
| 142 | + val outDir = projectDir.resolve("language_host/current/LanguageHost/modules/$dependencyName") |
| 143 | + doFirst { |
| 144 | + if (!outDir.deleteRecursively()) error("Cannot delete \"$outDir\".") |
129 | 145 | }
|
130 |
| - if (!hash.equals(psScriptAnalyzerSha256Hash, ignoreCase = true)) { |
131 |
| - psScriptAnalyzerOutFile.asFile.toPath().deleteExisting() |
132 |
| - error("PSScriptAnalyzer hash check failed. Expected ${psScriptAnalyzerSha256Hash}, but got $hash\n" + |
133 |
| - "Please try running the task again, or update the expected hash in the gradle.properties file.") |
| 146 | + |
| 147 | + dependsOn(download) |
| 148 | + from(zipTree(destination)) { |
| 149 | + customizeZip(this) |
134 | 150 | }
|
| 151 | + into(outDir) |
135 | 152 | }
|
136 | 153 | }
|
137 | 154 |
|
138 |
| - val getPsScriptAnalyzer by registering(Copy::class) { |
139 |
| - group = "dependencies" |
| 155 | + val downloads = layout.buildDirectory.get().dir("download") |
140 | 156 |
|
141 |
| - val outDir = projectDir.resolve("language_host/current/LanguageHost/modules/PSScriptAnalyzer") |
142 |
| - doFirst { |
143 |
| - if (!outDir.deleteRecursively()) error("Cannot delete \"$outDir\".") |
144 |
| - } |
| 157 | + val psScriptAnalyzerVersion: String by project |
| 158 | + val psScriptAnalyzerSha256Hash: String by project |
| 159 | + val psScriptAnalyzerFileName = "PSScriptAnalyzer.$psScriptAnalyzerVersion.nupkg" |
| 160 | + val psScriptAnalyzerOutFile = downloads.file(psScriptAnalyzerFileName) |
145 | 161 |
|
146 |
| - dependsOn(downloadPsScriptAnalyzer) |
147 |
| - from(zipTree(psScriptAnalyzerOutFile)) |
| 162 | + val psesVersion: String by project |
| 163 | + val psesSha256Hash: String by project |
| 164 | + |
| 165 | + val getPsScriptAnalyzer = getDependencyTask( |
| 166 | + "PSScriptAnalyzer", |
| 167 | + psScriptAnalyzerVersion, |
| 168 | + psScriptAnalyzerSha256Hash, |
| 169 | + URI( |
| 170 | + "https://github.com/PowerShell/PSScriptAnalyzer/releases/download/" + |
| 171 | + "$psScriptAnalyzerVersion/$psScriptAnalyzerFileName" |
| 172 | + ), |
| 173 | + psScriptAnalyzerOutFile |
| 174 | + ) { |
148 | 175 | // NuGet stuff:
|
149 | 176 | exclude("_manifest/**", "_rels/**", "package/**", "[Content_Types].xml", "*.nuspec")
|
150 | 177 |
|
151 | 178 | // Compatibility profiles, see https://github.com/PowerShell/PSScriptAnalyzer/issues/1148
|
152 | 179 | exclude("compatibility_profiles/**")
|
153 |
| - into(outDir) |
| 180 | + } |
| 181 | + |
| 182 | + val getPowerShellEditorServices = getDependencyTask( |
| 183 | + "PowerShellEditorServices", |
| 184 | + psesVersion, |
| 185 | + psesSha256Hash, |
| 186 | + URI( |
| 187 | + "https://github.com/PowerShell/PowerShellEditorServices/releases/download/" + |
| 188 | + "v$psesVersion/PowerShellEditorServices.zip" |
| 189 | + ), |
| 190 | + downloads.file("PowerShellEditorServices.zip") |
| 191 | + ) { |
| 192 | + include("PowerShellEditorServices/PowerShellEditorServices/**") |
| 193 | + eachFile { |
| 194 | + relativePath = RelativePath(true, *relativePath.segments.drop(2).toTypedArray()) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + val getAllDependencies by registering { |
| 199 | + dependsOn(getPsScriptAnalyzer, getPowerShellEditorServices) |
154 | 200 | }
|
155 | 201 |
|
156 | 202 | withType<PrepareSandboxTask> {
|
157 |
| - dependsOn(getPsScriptAnalyzer) |
| 203 | + dependsOn(getAllDependencies) |
158 | 204 | from("${project.rootDir}/language_host/current") {
|
159 | 205 | into("${intellij.pluginName.get()}/lib/")
|
160 | 206 | }
|
|
0 commit comments