Skip to content

Commit

Permalink
Separate 'UnpackAar' action
Browse files Browse the repository at this point in the history
  • Loading branch information
turansky authored and qwwdfsad committed Aug 24, 2020
1 parent fd5bf6b commit a09df3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
31 changes: 31 additions & 0 deletions buildSrc/src/main/kotlin/UnpackAar.kt
Original file line number Diff line number Diff line change
@@ -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<TransformParameters.None> {
@get:InputArtifact
abstract val inputArtifact: Provider<FileSystemLocation>

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())
}
}
34 changes: 0 additions & 34 deletions integration/kotlinx-coroutines-play-services/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -49,31 +43,3 @@ tasks.withType(dokka.getClass()) {
packageListUrl = projectDir.toPath().resolve("package.list").toUri().toURL()
}
}

abstract class UnpackAar implements TransformAction<TransformParameters.None> {
@InputArtifact
abstract Provider<FileSystemLocation> 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()
}
}
}

0 comments on commit a09df3d

Please sign in to comment.