diff --git a/CHANGELOG.md b/CHANGELOG.md index 39d8d45..5356a8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/DownloadTask.kt b/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/DownloadTask.kt index 3d2bc9c..373204f 100644 --- a/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/DownloadTask.kt +++ b/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/DownloadTask.kt @@ -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 @@ -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 @@ -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.") } @@ -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 - } } diff --git a/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/ExtractTask.kt b/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/ExtractTask.kt index e3b51d5..a939f1b 100644 --- a/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/ExtractTask.kt +++ b/plugin/src/main/kotlin/de/infolektuell/gradle/jextract/tasks/ExtractTask.kt @@ -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 @@ -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