-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
390 lines (347 loc) · 16.2 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
alias(libs.plugins.kotlin.ksp)
alias(libs.plugins.paparazzi)
}
plugins.apply(ExampleGradleSubplugin::class)
android {
namespace = "com.isaacudy.codegen.examples"
compileSdk = 35
defaultConfig {
applicationId = "com.isaacudy.codegen.examples"
minSdk = 24
targetSdk = 35
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
kotlinOptions {
jvmTarget = "21"
}
buildFeatures {
compose = true
}
sourceSets {
getByName("main").java.srcDirs("build/generated/gradle/src/main/java")
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(projects.compilerPluginCompanion)
ksp(projects.kspProcessor)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
// =================================================================================================
// Generate code from Gradle task
// =================================================================================================
/**
* This is a simple example of a Gradle task that writes some code to a file.
*
* It's important to note line 46 in this file (inside the "android" block), where we're adding
* the generated source set "build/generated/gradle/src/main/java" as a source directory for
* the "main" source set. This is necessary so that the generated code will be compiled as
* part of the main source of the project.
*
* It's also important to note the "afterEvaluate" block underneath this task's definition,
* where we're configuring the "compileDebugKotlin" and "compileReleaseKotlin" tasks to depend on
* this task. This is necessary so that this task is able to write the generated code before
* the main source code is compiled, which means that the generated code will be compiled.
*/
tasks.register("generateGreeting") {
doLast {
// Ensure that the directory we want to write our generated code to exists
val generatedDirectory =
project.file("build/generated/gradle/src/main/java/com/isaacudy/codegen/examples")
.apply {
// If we're already generated code, delete it
if (exists()) deleteRecursively()
mkdirs()
}
// Ensure that the file we want to write our generated code to exists
val file = File(generatedDirectory, "GeneratedGreeting.kt").apply {
createNewFile()
}
// This is a very simple example of code generation. We don't need to do anything fancy,
// we're just going to output some valid code into a file. If this doesn't compile properly,
// the build will fail, and we'll see the error in the build output, and we can adjust this
// code accordingly.
file.writeText(
"""
package com.isaacudy.codegen.examples
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@Composable
fun GeneratedGreeting() {
Text(
text = "Hello from Gradle code generation!",
)
}
""".trimIndent()
)
}
}
/**
* This block configures the "compileDebugKotlin" and "compileReleaseKotlin" tasks to depend on
* the "generateGreeting" task. This is necessary so that the generated code is written before
* the main source code is compiled, which means that the generated code will be compiled.
* We need to use "afterEvaluate" here because these tasks are not available until after the
* build script has been evaluated for the first time. These are added through the
* android plugin (in this case, the application plugin, but it's the same for the library plugin).
*/
afterEvaluate {
tasks.named("compileDebugKotlin").configure {
dependsOn("generateGreeting")
}
tasks.named("compileReleaseKotlin").configure {
dependsOn("generateGreeting")
}
}
// =================================================================================================
// =================================================================================================
// Generate tests for Compose previews from Gradle task
// =================================================================================================
/**
* The generateComposePreviewTests task generates a test class for each @Composable @Preview function,
* and that test class defines a paparazzi snapshot test for that preview function. Unlike the
* generateGreeting task, this task is used to generate test classes, so it's configured to run
* before the compileDebugUnitTestKotlin and compileReleaseUnitTestKotlin tasks. This means that
* this task will not run if you're running the app, but it will run if you're running the tests.
*
* This is a more complex example of using Gradle to perform code generation. For this example,
* we're doing the boilerplate task registration and afterEvaluate bits first, and then we're
* defining the actual task implementation in a separate function (generateComposePreviewTests).
*
* If you're not sure about the task registration or afterEvaluate bits, you can refer to the
* "generateGreeting" task above, which is a simpler example and describes what these do.
*/
tasks.register("generateComposePreviewTests") {
doLast {
generateComposePreviewTests(project)
}
}
afterEvaluate {
tasks.named("compileDebugUnitTestKotlin").configure {
dependsOn("generateComposePreviewTests")
}
tasks.named("compileReleaseUnitTestKotlin").configure {
dependsOn("generateComposePreviewTests")
}
}
/**
* This function generates and writes test classes for each Composable preview function
* in the project. This is where we're going to start to see the limitations of using Gradle
* to perform code generation, as we're going to need to scan through all the files in the
* project, and use basic string manipulation to find the Composable preview functions,
* which is not particularly robust.
*/
fun generateComposePreviewTests(project: Project) {
// Recursively list all files in the "src/main/java" directory,
// and filter these so we're only getting the Kotlin files
val projectFiles = project.file("src/main/java")
.listFilesRecursively()
.filter { it.extension == "kt" }
// For each file, find all the Composable preview functions, and store these in a list.
// The main interesting part of this function is the getComposablePreviews function,
// which scans through a file and finds all the Composable preview functions, and also
// clearly shows the limitations of using Gradle for code generation.
val composablePreviews = projectFiles.flatMap { file ->
getComposablePreviews(file)
}
// Get the directory where we want to write the generated test classes, and ensure that
// it exists. If it already exists, we delete it and recreate it (because we don't want
// to keep the old test classes around if the Composable preview functions have changed).
val generatedDirectory = project.file("src/test/java/com/isaacudy/codegen/examples/generated")
.apply {
if (exists()) deleteRecursively()
mkdirs()
}
// Just like the generateGreeting task, we're going to take a simple approach here, and just
// write the generated code as a string. In a more complex project, you'd likely use something
// like KotlinPoet to generate the code.
composablePreviews.forEach {
val file = File(generatedDirectory, "${it.previewFunction}Test.kt").apply {
createNewFile()
}
// We consider a preview function to be a screen if it contains "Screen" and does not
// start with "Screen", because there are some functions like "ScreenTitle" which are
// not screens, but contain "Screen" in their name.
val isScreen = it.previewFunction.contains("Screen")
&& !it.previewFunction.startsWith("Screen")
val paddingAmount = when {
isScreen -> "0.dp"
else -> "16.dp"
}
file.writeText(
"""
package com.isaacudy.codegen.examples.generated
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import app.cash.paparazzi.DeviceConfig.Companion.PIXEL_5
import app.cash.paparazzi.Paparazzi
import com.android.ide.common.rendering.api.SessionParams
import org.junit.Test
import org.junit.Rule
import ${it.packageName}.${it.previewFunction}
/**
* This test class is generated by the generateComposePreviewTests Gradle task.
*/
class ${it.previewFunction}Test {
@get:Rule
val paparazzi = Paparazzi(
deviceConfig = PIXEL_5,
theme = "android:Theme.Material.Light.NoActionBar",
renderingMode = SessionParams.RenderingMode.SHRINK,
)
@Test
fun snapshot() {
paparazzi.snapshot {
Box(
modifier = Modifier.padding($paddingAmount),
) {
${it.previewFunction}()
}
}
}
}
""".trimIndent()
)
}
}
/**
* This function scans through a file and finds all the Composable preview functions in that file.
* This function works by reading the file line by line, and looking for lines that start with
* "@Preview". When it finds a line that starts with "@Preview", it looks for the first line that
* starts with "fun", and extracts the function name from that line. This is not a robust way to
* find Composable preview functions, but it's a simple way to demonstrate how you might do this
* using Gradle. This starts to show the limitations of using Gradle for code generation.
*/
fun getComposablePreviews(file: File): List<ComposablePreview> {
val fileLines = file.readLines()
// Find the first line that starts with "package ", and extract the package name from that line
val packageName = fileLines
.firstOrNull { it.startsWith("package ") }
?.substringAfter("package ")?.trim() ?: ""
val previews = mutableListOf<ComposablePreview>()
fileLines.forEachIndexed { index, line ->
if (line.startsWith("@Preview")) {
val previewFunction = fileLines.subList(index, fileLines.size)
.takeWhile {
it.startsWith("@Preview") ||
it.startsWith("@Composable") ||
it.startsWith("fun ") ||
it.isBlank()
}
.firstOrNull { it.trim().startsWith("fun") }
?.substringAfter("fun ")
?.substringBefore("(")
?.trim() ?: return@forEachIndexed
previews.add(
ComposablePreview(
packageName = packageName,
previewFunction = previewFunction
)
)
}
}
return previews
}
/**
* This is a simple helper extension function on File is used to recursively list all files
* in a directory, because the built-in listFiles() function only lists the files in the current
* directory, and not the files in subdirectories.
*/
fun File.listFilesRecursively(): List<File> {
return listFiles().orEmpty().flatMap {
if (it.isDirectory) {
it.listFilesRecursively()
} else {
listOf(it)
}
}
}
/**
* This data class represents a Composable preview function, and is used to store the package name\
* and preview function name for each Composable preview function in the project.
*/
data class ComposablePreview(
val packageName: String,
val previewFunction: String,
)
// =================================================================================================
// =================================================================================================
// Configure example compiler plugin
// =================================================================================================
class ExampleGradleSubplugin : KotlinCompilerPluginSupportPlugin {
override fun apply(target: Project) {}
override fun getCompilerPluginId(): String = "com.isaacudy.codegen.examples.plugin"
// When creating a compiler plugin, we need to provide a artifact for the plugin, which
// can be resolved by Gradle to download and apply the plugin. We're providing a made up
// groupId/artifactId/version here, because we actually want to use a local plugin project,
// and then down below we're going to configure dependency substitution to use the local plugin
// project instead of this made up plugin artifact.
override fun getPluginArtifact(): SubpluginArtifact =
SubpluginArtifact(
groupId = "com.isaacudy.codegen.examples",
artifactId = "example-compiler-plugin",
version = "0.0.1",
)
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true
// We don't need to provide any options to the plugin, so we just return an empty list.
// If we did have options, we would return a list of SubpluginOption instances here,
// which we could then read in the compiler plugin.
// If you're interested in how to provide options to a compiler plugin, you can check out
// https://github.com/ZacSweers/redacted-compiler-plugin, which is a good example, specifically:
// Setting options: https://github.com/ZacSweers/redacted-compiler-plugin/blob/a6d909fc00e4a9404013e10e1226a9344d882a4e/redacted-compiler-plugin-gradle/src/main/kotlin/dev/zacsweers/redacted/gradle/RedactedGradleSubplugin.kt#L61
// and
// Reading options: https://github.com/ZacSweers/redacted-compiler-plugin/blob/a6d909fc00e4a9404013e10e1226a9344d882a4e/redacted-compiler-plugin/src/main/kotlin/dev/zacsweers/redacted/compiler/RedactedPlugin.kt#L42
override fun applyToCompilation(
kotlinCompilation: KotlinCompilation<*>
): Provider<List<SubpluginOption>> {
val project = kotlinCompilation.target.project
return project.provider {
listOf()
}
}
}
// See the comment on ExampleGradleSubplugin.getPluginArtifact
configurations.configureEach {
resolutionStrategy.dependencySubstitution {
substitute(module("com.isaacudy.codegen.examples:example-compiler-plugin"))
.using(project(":compiler-plugin"))
}
}
// =================================================================================================