Improve DexMethodCount task

Use lazy api to set the file and avoid configuring Jar tasks
This commit is contained in:
cristiangarcia
2023-06-20 00:55:41 +02:00
committed by Space Team
parent d17c7d4729
commit 650b00e1b2
3 changed files with 29 additions and 34 deletions
@@ -3,19 +3,22 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import com.jakewharton.dex.*
import com.jakewharton.dex.DexMethod
import com.jakewharton.dex.DexParser.Companion.toDexParser
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.jvm.tasks.Jar
import java.io.File
import org.gradle.kotlin.dsl.property
import javax.inject.Inject
@CacheableTask
abstract class DexMethodCount : DefaultTask() {
abstract class DexMethodCount @Inject constructor(objectFactory: ObjectFactory, layout: ProjectLayout) : DefaultTask() {
data class Counts(
val total: Int,
@@ -25,39 +28,33 @@ abstract class DexMethodCount : DefaultTask() {
val byClass: Map<String, Int>
)
@Classpath
lateinit var jarFile: File
@get:InputFile
@get:Classpath
abstract val jarFile: RegularFileProperty
@get:Optional
@get:Input
abstract val ownPackages: ListProperty<String>
@Internal
var artifactName: String? = null
private val projectName = project.name
private val projectName: String = project.name
@get:Input
val artifactOrArchiveName: String
get() = artifactName ?: projectName
val artifactOrArchiveName: Property<String> = objectFactory.property<String>().convention(projectName)
fun from(jar: Jar) {
jarFile = jar.archiveFile.get().asFile
artifactName = jar.archiveBaseName.orNull
dependsOn(jar)
fun from(jar: TaskProvider<Jar>) {
jarFile.set(jar.flatMap { it.archiveFile })
artifactOrArchiveName.set(jar.flatMap { it.archiveBaseName.orElse(projectName) })
}
@Internal // plain output properties are not supported, mark as internal to suppress warning from validatePlugins
lateinit var counts: Counts
@get:OutputFile
val detailOutputFile: File by lazy {
project.buildDir.resolve("$artifactOrArchiveName-method-count.txt")
}
val detailOutputFile: RegularFileProperty = objectFactory.fileProperty().value(artifactOrArchiveName.flatMap { layout.buildDirectory.file("$it-method-count.txt") })
@TaskAction
fun invoke() {
val methods = jarFile.toDexParser().listMethods()
val methods = jarFile.get().asFile.toDexParser().listMethods()
val counts = methods.getCounts().also { this.counts = it }
outputDetails(counts)
}
@@ -83,7 +80,7 @@ abstract class DexMethodCount : DefaultTask() {
}
private fun outputDetails(counts: Counts) {
detailOutputFile.printWriter().use { writer ->
detailOutputFile.get().asFile.printWriter().use { writer ->
writer.println("${counts.total.padRight()}\tTotal methods")
ownPackages.orNull?.let { packages ->
writer.println("${counts.totalOwnPackages?.padRight()}\tTotal methods from packages ${packages.joinToString { "$it.*" }}")
@@ -150,9 +147,8 @@ abstract class DexMethodCountStats : DefaultTask() {
fun Project.printStats(dexMethodCount: TaskProvider<DexMethodCount>) {
val dexMethodCountStats = tasks.register("dexMethodCountStats", DexMethodCountStats::class.java) {
dependsOn(dexMethodCount)
inputFile.set(dexMethodCount.flatMap { objects.fileProperty().apply { set(it.detailOutputFile) } })
artifactOrArchiveName.set(dexMethodCount.map { it.artifactOrArchiveName })
inputFile.set(dexMethodCount.flatMap { it.detailOutputFile })
artifactOrArchiveName.set(dexMethodCount.flatMap { it.artifactOrArchiveName })
ownPackages.set(dexMethodCount.flatMap { it.ownPackages })
}
+2 -5
View File
@@ -244,15 +244,12 @@ val result by task<Jar> {
javadocJar()
dexMethodCount {
dependsOn(result)
jarFile = result.get().outputs.files.single()
jarFile.fileProvider(result.map { it.outputs.files.singleFile })
ownPackages.set(listOf("kotlin.reflect"))
}
artifacts {
listOf("archives", "runtimeElements").forEach { configurationName ->
add(configurationName, provider { result.get().outputs.files.singleFile }) {
builtBy(result)
}
add(configurationName, result.map { it.outputs.files.singleFile })
}
}
+8 -6
View File
@@ -1,6 +1,8 @@
description = 'Kotlin Standard Library for JVM'
plugins {
id("kotlin-platform-jvm")
}
apply plugin: 'kotlin-platform-jvm'
description = 'Kotlin Standard Library for JVM'
archivesBaseName = 'kotlin-stdlib'
@@ -93,7 +95,7 @@ dependencies {
builtins project(':core:builtins')
}
jar {
tasks.named("jar", Jar) {
dependsOn(configurations.builtins)
manifestAttributes(manifest, project, 'Main', true)
from {
@@ -104,7 +106,7 @@ jar {
from sourceSets.java9.output
}
sourcesJar {
tasks.named("sourcesJar", Jar) {
from "${rootDir}/core/builtins/native"
from(sourceSets.mainJdk7.allSource) {
into 'jdk7'
@@ -114,7 +116,7 @@ sourcesJar {
}
}
task distSourcesJar(type: Jar) {
tasks.create("distSourcesJar", Jar) {
dependsOn(sourcesJar, configurations.commonSources)
destinationDirectory = file("$buildDir/lib/dist")
archiveClassifier.set('sources')
@@ -136,7 +138,7 @@ artifacts {
}
DexMethodCountKt.dexMethodCount(project) { task ->
task.from(jar)
task.from(tasks.named("jar", Jar))
task.ownPackages = ['kotlin']
}