Skip to content

Commit

Permalink
Improvement: Make download task cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaracha committed Oct 12, 2024
1 parent 845a5cd commit ad70f60
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Create changelog file for release notes
- The source set the generated sources are added to is now user-selectable via extension property, `main` is chosen by default.
- DownloadTask became cacheable, so the downloaded Jextract archives can be shared and re-used via build cache.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
Expand All @@ -24,6 +25,7 @@ import java.security.MessageDigest
import java.time.Duration
import javax.inject.Inject

@CacheableTask
abstract class DownloadTask : DefaultTask() {
abstract class Resource @Inject constructor(objects: ObjectFactory) {
@get:Input
Expand Down Expand Up @@ -52,15 +54,12 @@ abstract class DownloadTask : DefaultTask() {

private fun download(source: URI, checksum: String, algorithm: String, target: Path) {
val client: HttpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).version(HttpClient.Version.HTTP_1_1).build()
val downloaded = verify(target, checksum, algorithm)
if (downloaded) return
val request: HttpRequest = HttpRequest.newBuilder()
.uri(source)
.GET()
.build()
val response = client.send(request, ofInputStream())
if (response.statusCode() != 200) throw GradleException("Downloading from $source failed with status code ${response.statusCode()}.")
println(response.version())
val isValid = copyVerify(response.body(), target, checksum, algorithm)
if (!isValid) throw GradleException("Data integrity of downloaded file $source could not be verified, checksums do not match.")
}
Expand All @@ -73,16 +72,4 @@ abstract class DownloadTask : DefaultTask() {
if (calculatedChecksum != checksum) Files.deleteIfExists(file)
return calculatedChecksum == checksum
}
@OptIn(ExperimentalStdlibApi::class)
private fun verify(file: Path, checksum: String, algorithm: String): Boolean {
if (Files.notExists(file)) return false
val md = MessageDigest.getInstance(algorithm)
DigestInputStream(Files.newInputStream(file), md).use { input ->
input.readAllBytes()
}
val calculatedChecksum = md.digest().toHexString()
val isValid = calculatedChecksum == checksum
if (!isValid) Files.deleteIfExists(file)
return isValid
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package de.infolektuell.gradle.jextract.tasks

import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
Expand All @@ -7,8 +8,10 @@ import org.gradle.api.file.FileSystemOperations
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault
import javax.inject.Inject

@DisableCachingByDefault(because = "Extracting an archive is not worth caching")
abstract class ExtractTask @Inject constructor(private val fileSystem: FileSystemOperations, private val archives: ArchiveOperations) : DefaultTask() {
@get:InputFile
abstract val source: RegularFileProperty
Expand Down

0 comments on commit ad70f60

Please sign in to comment.