Skip to content

Commit

Permalink
Add DSL helpers to enable metrics and reports
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Oct 24, 2023
1 parent 5ab058a commit 8bcad23
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
*/
package app.cash.redwood.gradle

import javax.inject.Inject
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property

public abstract class RedwoodComposeExtension
@Inject constructor(objectFactory: ObjectFactory) {
public interface RedwoodComposeExtension {
/**
* The version of the JetBrains Compose compiler to use, or a Maven coordinate triple of
* the custom Compose compiler to use.
Expand All @@ -39,7 +36,35 @@ public abstract class RedwoodComposeExtension
* }
* ```
*/
public val kotlinCompilerPlugin: Property<String> =
objectFactory.property(String::class.java)
.convention(composeCompilerVersion)
public val kotlinCompilerPlugin: Property<String>

/**
* Enable the output of metrics from the Compose compiler.
*
* Text files will be written to `generated/redwood/compose-metrics/` in the project's build
* directory. See
* [the compiler documentation](https://github.com/androidx/androidx/blob/androidx-main/compose/compiler/design/compiler-metrics.md#reports-breakdown)
* for more information about the contents.
*
* **NOTE:** This should only be enabled during investigation as it breaks the use of
* Gradle's build cache for this project's Kotlin compilation tasks.
*
* @see enableReports
*/
public fun enableMetrics()

/**
* Enable the output of reports from the Compose compiler.
*
* Text files will be written to `generated/redwood/compose-reports/` in the project's build
* directory. See
* [the compiler documentation](https://github.com/androidx/androidx/blob/androidx-main/compose/compiler/design/compiler-metrics.md#reports-breakdown)
* for more information about the contents.
*
* **NOTE:** This should only be enabled during investigation as it breaks the use of
* Gradle's build cache for this project's Kotlin compilation tasks.
*
* @see enableMetrics
*/
public fun enableReports()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.gradle

import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal class RedwoodComposeExtensionImpl(private val project: Project) : RedwoodComposeExtension {
private var metricsEnabled = false
private var reportsEnabled = false

// Explicit backing property avoids Gradle attempting to reflect on an implicit backing field.
override val kotlinCompilerPlugin: Property<String> get() = _kotlinCompilerPlugin
private val _kotlinCompilerPlugin = project.objects.property(String::class.java)
.convention(composeCompilerVersion)

override fun enableMetrics() {
if (metricsEnabled) return
metricsEnabled = true

project.tasks.withType(KotlinCompile::class.java) {
val dir = project.redwoodGeneratedDir("compose-metrics/${it.name}").get().asFile.absolutePath
it.compilerOptions.freeCompilerArgs.addAll(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$dir",
)
}
}

override fun enableReports() {
if (reportsEnabled) return
reportsEnabled = true

project.tasks.withType(KotlinCompile::class.java) {
val dir = project.redwoodGeneratedDir("compose-reports/${it.name}").get().asFile.absolutePath
it.compilerOptions.freeCompilerArgs.addAll(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$dir",
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public class RedwoodComposePlugin : KotlinCompilerPluginSupportPlugin {
override fun apply(target: Project) {
super.apply(target)

extension = target.extensions.create(extensionName, RedwoodComposeExtension::class.java)
extension = RedwoodComposeExtensionImpl(target)
target.extensions.add(
RedwoodComposeExtension::class.java,
extensionName,
extension,
)

target.plugins.withId("org.jetbrains.compose") {
throw IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public abstract class RedwoodGeneratorPlugin(
it.description = "Generate Redwood Kotlin sources"

it.toolClasspath.from(toolingConfiguration)
it.outputDir.set(project.layout.buildDirectory.dir("generated/redwood"))
it.outputDir.set(project.redwoodGeneratedDir("sources"))
it.generatorFlag.set(strategy.generatorFlag)
it.schemaType.set(extension.type)
it.classpath.from(schemaConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class RedwoodSchemaPlugin : Plugin<Project> {
it.description = "Generate parsed schema JSON"

it.toolClasspath.from(toolingConfiguration)
it.outputDir.set(project.layout.buildDirectory.dir("generated/redwood"))
it.outputDir.set(project.redwoodGeneratedDir("schema-json"))
it.schemaType.set(extension.type)
it.classpath.from(classpath, compilation.output.classesDirs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package app.cash.redwood.gradle

import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.provider.Provider

internal fun Project.redwoodDependency(artifactId: String): Any {
// Indicates when the plugin is applied inside the Redwood repo to Redwood's own modules. This
Expand All @@ -28,3 +30,7 @@ internal fun Project.redwoodDependency(artifactId: String): Any {
"app.cash.redwood:$artifactId:$redwoodVersion"
}
}

internal fun Project.redwoodGeneratedDir(name: String): Provider<Directory> {
return layout.buildDirectory.dir("generated/redwood/$name")
}

0 comments on commit 8bcad23

Please sign in to comment.