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. * 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 com.jakewharton.dex.DexParser.Companion.toDexParser
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property import org.gradle.api.provider.Property
import org.gradle.api.tasks.* import org.gradle.api.tasks.*
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import java.io.File import org.gradle.kotlin.dsl.property
import javax.inject.Inject
@CacheableTask @CacheableTask
abstract class DexMethodCount : DefaultTask() { abstract class DexMethodCount @Inject constructor(objectFactory: ObjectFactory, layout: ProjectLayout) : DefaultTask() {
data class Counts( data class Counts(
val total: Int, val total: Int,
@@ -25,39 +28,33 @@ abstract class DexMethodCount : DefaultTask() {
val byClass: Map<String, Int> val byClass: Map<String, Int>
) )
@Classpath @get:InputFile
lateinit var jarFile: File @get:Classpath
abstract val jarFile: RegularFileProperty
@get:Optional @get:Optional
@get:Input @get:Input
abstract val ownPackages: ListProperty<String> abstract val ownPackages: ListProperty<String>
@Internal private val projectName: String = project.name
var artifactName: String? = null
private val projectName = project.name
@get:Input @get:Input
val artifactOrArchiveName: String val artifactOrArchiveName: Property<String> = objectFactory.property<String>().convention(projectName)
get() = artifactName ?: projectName
fun from(jar: Jar) { fun from(jar: TaskProvider<Jar>) {
jarFile = jar.archiveFile.get().asFile jarFile.set(jar.flatMap { it.archiveFile })
artifactName = jar.archiveBaseName.orNull artifactOrArchiveName.set(jar.flatMap { it.archiveBaseName.orElse(projectName) })
dependsOn(jar)
} }
@Internal // plain output properties are not supported, mark as internal to suppress warning from validatePlugins @Internal // plain output properties are not supported, mark as internal to suppress warning from validatePlugins
lateinit var counts: Counts lateinit var counts: Counts
@get:OutputFile @get:OutputFile
val detailOutputFile: File by lazy { val detailOutputFile: RegularFileProperty = objectFactory.fileProperty().value(artifactOrArchiveName.flatMap { layout.buildDirectory.file("$it-method-count.txt") })
project.buildDir.resolve("$artifactOrArchiveName-method-count.txt")
}
@TaskAction @TaskAction
fun invoke() { fun invoke() {
val methods = jarFile.toDexParser().listMethods() val methods = jarFile.get().asFile.toDexParser().listMethods()
val counts = methods.getCounts().also { this.counts = it } val counts = methods.getCounts().also { this.counts = it }
outputDetails(counts) outputDetails(counts)
} }
@@ -83,7 +80,7 @@ abstract class DexMethodCount : DefaultTask() {
} }
private fun outputDetails(counts: Counts) { private fun outputDetails(counts: Counts) {
detailOutputFile.printWriter().use { writer -> detailOutputFile.get().asFile.printWriter().use { writer ->
writer.println("${counts.total.padRight()}\tTotal methods") writer.println("${counts.total.padRight()}\tTotal methods")
ownPackages.orNull?.let { packages -> ownPackages.orNull?.let { packages ->
writer.println("${counts.totalOwnPackages?.padRight()}\tTotal methods from packages ${packages.joinToString { "$it.*" }}") 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>) { fun Project.printStats(dexMethodCount: TaskProvider<DexMethodCount>) {
val dexMethodCountStats = tasks.register("dexMethodCountStats", DexMethodCountStats::class.java) { val dexMethodCountStats = tasks.register("dexMethodCountStats", DexMethodCountStats::class.java) {
dependsOn(dexMethodCount) inputFile.set(dexMethodCount.flatMap { it.detailOutputFile })
inputFile.set(dexMethodCount.flatMap { objects.fileProperty().apply { set(it.detailOutputFile) } }) artifactOrArchiveName.set(dexMethodCount.flatMap { it.artifactOrArchiveName })
artifactOrArchiveName.set(dexMethodCount.map { it.artifactOrArchiveName })
ownPackages.set(dexMethodCount.flatMap { it.ownPackages }) ownPackages.set(dexMethodCount.flatMap { it.ownPackages })
} }
+2 -5
View File
@@ -244,15 +244,12 @@ val result by task<Jar> {
javadocJar() javadocJar()
dexMethodCount { dexMethodCount {
dependsOn(result) jarFile.fileProvider(result.map { it.outputs.files.singleFile })
jarFile = result.get().outputs.files.single()
ownPackages.set(listOf("kotlin.reflect")) ownPackages.set(listOf("kotlin.reflect"))
} }
artifacts { artifacts {
listOf("archives", "runtimeElements").forEach { configurationName -> listOf("archives", "runtimeElements").forEach { configurationName ->
add(configurationName, provider { result.get().outputs.files.singleFile }) { add(configurationName, result.map { it.outputs.files.singleFile })
builtBy(result)
}
} }
} }
+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' archivesBaseName = 'kotlin-stdlib'
@@ -93,7 +95,7 @@ dependencies {
builtins project(':core:builtins') builtins project(':core:builtins')
} }
jar { tasks.named("jar", Jar) {
dependsOn(configurations.builtins) dependsOn(configurations.builtins)
manifestAttributes(manifest, project, 'Main', true) manifestAttributes(manifest, project, 'Main', true)
from { from {
@@ -104,7 +106,7 @@ jar {
from sourceSets.java9.output from sourceSets.java9.output
} }
sourcesJar { tasks.named("sourcesJar", Jar) {
from "${rootDir}/core/builtins/native" from "${rootDir}/core/builtins/native"
from(sourceSets.mainJdk7.allSource) { from(sourceSets.mainJdk7.allSource) {
into 'jdk7' into 'jdk7'
@@ -114,7 +116,7 @@ sourcesJar {
} }
} }
task distSourcesJar(type: Jar) { tasks.create("distSourcesJar", Jar) {
dependsOn(sourcesJar, configurations.commonSources) dependsOn(sourcesJar, configurations.commonSources)
destinationDirectory = file("$buildDir/lib/dist") destinationDirectory = file("$buildDir/lib/dist")
archiveClassifier.set('sources') archiveClassifier.set('sources')
@@ -136,7 +138,7 @@ artifacts {
} }
DexMethodCountKt.dexMethodCount(project) { task -> DexMethodCountKt.dexMethodCount(project) { task ->
task.from(jar) task.from(tasks.named("jar", Jar))
task.ownPackages = ['kotlin'] task.ownPackages = ['kotlin']
} }