From a09df3de5f12058b99350ced88c142d8f436f3bf Mon Sep 17 00:00:00 2001 From: Victor Turansky Date: Sat, 2 May 2020 21:41:26 +0300 Subject: [PATCH] Separate 'UnpackAar' action --- buildSrc/src/main/kotlin/UnpackAar.kt | 31 +++++++++++++++++ .../build.gradle | 34 ------------------- 2 files changed, 31 insertions(+), 34 deletions(-) create mode 100644 buildSrc/src/main/kotlin/UnpackAar.kt diff --git a/buildSrc/src/main/kotlin/UnpackAar.kt b/buildSrc/src/main/kotlin/UnpackAar.kt new file mode 100644 index 0000000000..2b96df3f86 --- /dev/null +++ b/buildSrc/src/main/kotlin/UnpackAar.kt @@ -0,0 +1,31 @@ +import org.gradle.api.artifacts.transform.InputArtifact +import org.gradle.api.artifacts.transform.TransformAction +import org.gradle.api.artifacts.transform.TransformOutputs +import org.gradle.api.artifacts.transform.TransformParameters +import org.gradle.api.file.FileSystemLocation +import org.gradle.api.provider.Provider +import java.io.File +import java.nio.file.Files +import java.util.zip.ZipEntry +import java.util.zip.ZipFile + +@Suppress("UnstableApiUsage") +abstract class UnpackAar : TransformAction { + @get:InputArtifact + abstract val inputArtifact: Provider + + override fun transform(outputs: TransformOutputs) { + ZipFile(inputArtifact.get().asFile).use { zip -> + zip.entries().asSequence() + .filter { !it.isDirectory } + .filter { it.name.endsWith(".jar") } + .forEach { zip.unzip(it, outputs.file(it.name)) } + } + } +} + +private fun ZipFile.unzip(entry: ZipEntry, output: File) { + getInputStream(entry).use { + Files.copy(it, output.toPath()) + } +} diff --git a/integration/kotlinx-coroutines-play-services/build.gradle b/integration/kotlinx-coroutines-play-services/build.gradle index eb554866ed..29ce3d606f 100644 --- a/integration/kotlinx-coroutines-play-services/build.gradle +++ b/integration/kotlinx-coroutines-play-services/build.gradle @@ -2,12 +2,6 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -import org.gradle.api.artifacts.transform.* - -import java.nio.file.Files -import java.util.zip.ZipEntry -import java.util.zip.ZipFile - ext.tasks_version = '16.0.1' def artifactType = Attribute.of("artifactType", String) @@ -49,31 +43,3 @@ tasks.withType(dokka.getClass()) { packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL() } } - -abstract class UnpackAar implements TransformAction { - @InputArtifact - abstract Provider getInputArtifact() - - @Override - void transform(TransformOutputs outputs) { - ZipFile zip = new ZipFile(inputArtifact.get().asFile) - try { - for (entry in zip.entries()) { - if (!entry.isDirectory() && entry.name.endsWith(".jar")) { - unzipEntryTo(zip, entry, outputs.file(entry.name)) - } - } - } finally { - zip.close() - } - } - - private static void unzipEntryTo(ZipFile zip, ZipEntry entry, File output) { - InputStream stream = zip.getInputStream(entry) - try { - Files.copy(stream, output.toPath()) - } finally { - stream.close() - } - } -}