diff --git a/gradle-plugin/README.md b/gradle-plugin/README.md index 63499ccc..671629e3 100644 --- a/gradle-plugin/README.md +++ b/gradle-plugin/README.md @@ -203,6 +203,9 @@ emerge { // Include private previews in snapshot generation - defaults to true includePrivatePreviews.set(false) + // Snapshot `@Preview` functions with `@PreviewParameter` annotated params - defaults to true + includePreviewParamPreviews.set(false) + // Tag to use for grouping builds in the Emerge dashboard tag.set("snapshots") // Alternatively, use `setFromVariant()` to set the tag from the Android build variant @@ -219,13 +222,14 @@ versions to releases & names, see [Android API levels](https://apilevels.com/). ##### Fields -| Field | Type | Default | Description | -|-----------------------------|-----------|-----------------------------------|---------------------------------------------------------------------------------------------------| -| `includePrivatePreviews` | `Boolean` | `true` | If Emerge should snapshot `private` annotated `@Preview` functions. | -| `apiVersion` | `Int` | `34` | The Android API version to use for snapshot generation. Must be an int value in 29, 31, 33 or 34. | -| `snapshotsStorageDirectory` | `String` | `/build/emerge/snapshots/outputs` | The path to local snapshot storage. Only used for local snapshot generation. | -| `tag` | `String` | `release` | The build tag to use for grouping builds in the Emerge dashboard. | -| `enabled` | `Boolean` | `true` | If snapshot tasks/project configuration are enabled. | +| Field | Type | Default | Description | +|-------------------------------|-----------|-----------------------------------|---------------------------------------------------------------------------------------------------| +| `includePrivatePreviews` | `Boolean` | `true` | If Emerge should snapshot `private` annotated `@Preview` functions. | +| `includePreviewParamPreviews` | `Boolean` | `true` | If Emerge should snapshot `@Preview` functions with `@PreviewParameter` annotated params. | +| `apiVersion` | `Int` | `34` | The Android API version to use for snapshot generation. Must be an int value in 29, 31, 33 or 34. | +| `snapshotsStorageDirectory` | `String` | `/build/emerge/snapshots/outputs` | The path to local snapshot storage. Only used for local snapshot generation. | +| `tag` | `String` | `release` | The build tag to use for grouping builds in the Emerge dashboard. | +| `enabled` | `Boolean` | `true` | If snapshot tasks/project configuration are enabled. | ### Reaper @@ -375,8 +379,9 @@ emerge { snapshotsStorageDirectory.set("/src/main/snapshots") // Android API version to run snapshots on, must be 29, 31, 33 or 34. apiVersion.set(33) - // Include private previews in snapshot generation - defaults to true - includePrivatePreviews.set(false) + + // Snapshot `@Preview` functions with `@PreviewParameter` annotated params - defaults to true + includePreviewParamPreviews.set(false) // Optional, snapshots use debug builds, we recommend using a separate tag. tag.set("snapshots") diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePlugin.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePlugin.kt index bcb74b7f..066a2c7c 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePlugin.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePlugin.kt @@ -276,6 +276,10 @@ class EmergePlugin : Plugin { "includePrivatePreviews: ${extension.snapshotOptions.includePrivatePreviews.orEmpty()}", snapshotsHeading ) + addItem( + "includePreviewParamPreviews: ${extension.snapshotOptions.includePreviewParamPreviews.orEmpty()}", + snapshotsHeading + ) addItem("tag (optional): ${extension.snapshotOptions.tag.orEmpty()}", snapshotsHeading) addItem("enabled: ${extension.snapshotOptions.enabled.getOrElse(true)}", snapshotsHeading) diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePluginExtension.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePluginExtension.kt index 87a0f7a6..2079b702 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePluginExtension.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/EmergePluginExtension.kt @@ -195,6 +195,12 @@ abstract class SnapshotOptions : ProductOptions() { * Include private previews in snapshot generation, defaults to true */ abstract val includePrivatePreviews: Property + + /** + * Include previews with PreviewParameter annotated Previews in snapshot generation, + * defaults to true + */ + abstract val includePreviewParamPreviews: Property } abstract class ReaperOptions : ProductOptions() { diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/LocalSnapshots.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/LocalSnapshots.kt index d7a3dfd0..79073ed4 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/LocalSnapshots.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/LocalSnapshots.kt @@ -64,6 +64,10 @@ abstract class LocalSnapshots : DefaultTask() { @get:Optional abstract val includePrivatePreviews: Property + @get:Input + @get:Optional + abstract val includePreviewParamPreviews: Property + @TaskAction fun execute() { val artifactMetadataFiles = packageDir.asFileTree.matching { @@ -105,6 +109,7 @@ abstract class LocalSnapshots : DefaultTask() { val composeSnapshots = PreviewUtils.findPreviews( extractedApkDir, includePrivatePreviews.getOrElse(true), + includePreviewParamPreviews.getOrElse(true), previews, logger ) diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/Register.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/Register.kt index f540a53d..b4a60a6e 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/Register.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/Register.kt @@ -126,6 +126,7 @@ private fun registerSnapshotLocalTask( it.testAppId.set(testAppId) it.testInstrumentationRunner.set(testInstrumentationRunner) it.includePrivatePreviews.set(extension.snapshotOptions.includePrivatePreviews) + it.includePreviewParamPreviews.set(extension.snapshotOptions.includePreviewParamPreviews) it.dependsOn(packageTask) } } @@ -153,6 +154,7 @@ private fun registerSnapshotUploadTask( packageTask.flatMap { packageTask -> packageTask.artifactMetadataPath }) it.apiVersion.set(extension.snapshotOptions.apiVersion) it.includePrivatePreviews.set(extension.snapshotOptions.includePrivatePreviews) + it.includePreviewParamPreviews.set(extension.snapshotOptions.includePreviewParamPreviews) it.setUploadTaskInputs(extension, appProject, variant) it.setTagFromProductOptions(extension.snapshotOptions, variant) it.dependsOn(packageTask) diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/UploadSnapshotBundle.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/UploadSnapshotBundle.kt index 95177916..6e6e5b2a 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/UploadSnapshotBundle.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/UploadSnapshotBundle.kt @@ -33,6 +33,10 @@ abstract class UploadSnapshotBundle : BaseUploadTask() { @get:Optional abstract val includePrivatePreviews: Property + @get:Input + @get:Optional + abstract val includePreviewParamPreviews: Property + @get:InputFile abstract val artifactMetadataPath: RegularFileProperty @@ -70,6 +74,7 @@ abstract class UploadSnapshotBundle : BaseUploadTask() { androidSnapshotsEnabled = true, androidSnapshotsApiVersion = apiVersion.orNull, androidSnapshotsPrivateEnabled = includePrivatePreviews.getOrElse(true), + androidSnapshotsPreviewParamsEnabled = includePreviewParamPreviews.getOrElse(true), ) } diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/utils/PreviewUtils.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/utils/PreviewUtils.kt index f565d48f..f5c01340 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/utils/PreviewUtils.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/snapshots/utils/PreviewUtils.kt @@ -33,6 +33,7 @@ object PreviewUtils { fun findPreviews( extractedApkDirectory: File, includePrivatePreviews: Boolean, + includePreviewParamPreviews: Boolean, previewFunctions: List, logger: Logger, ): ComposeSnapshots { @@ -76,7 +77,7 @@ object PreviewUtils { return@forEach } - if (!hasSupportedMethodParameters(method, logger)) { + if (!hasSupportedMethodParameters(method, includePreviewParamPreviews, logger)) { logger.info("Ignoring snapshot for method: $methodKey as params are not supported.") return@forEach } @@ -102,7 +103,11 @@ object PreviewUtils { return signature.replace('/', '.').substring(1, signature.length - 1) } - private fun hasSupportedMethodParameters(method: DexBackedMethod, logger: Logger): Boolean { + private fun hasSupportedMethodParameters( + method: DexBackedMethod, + includePreviewParamPreviews: Boolean, + logger: Logger, + ): Boolean { // Only supporting 0-1 preview params val methodParamCount = method.parameters.size if (methodParamCount < 2 || methodParamCount > 3) { @@ -118,6 +123,7 @@ object PreviewUtils { } if (methodParamCount == 3) { + // Check first param annotated with @PreviewParameter val firstParam = method.parameters[0] val hasPreviewParameterAnnotation = firstParam.annotations.any { annotation -> @@ -129,7 +135,12 @@ object PreviewUtils { return false } - logger.info("Method ${method.name} has 3 parameters, and the first one is annotated with @PreviewParameter!") + if (!includePreviewParamPreviews) { + logger.info("Method ${method.name} has 3 parameters and the first one is annotated with @PreviewParameter but includePreviewParamPreviews is set to false") + return false + } + + logger.info("Method ${method.name} has 3 parameters and the first one is annotated with @PreviewParameter!") } logger.info("Method ${method.name} has supported parameters!") diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/network/EmergeUploadRequest.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/network/EmergeUploadRequest.kt index d7e62dfa..ecbab905 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/network/EmergeUploadRequest.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/network/EmergeUploadRequest.kt @@ -32,6 +32,7 @@ data class EmergeUploadRequestData( val androidSnapshotsEnabled: Boolean, val androidSnapshotsApiVersion: Int? = null, val androidSnapshotsPrivateEnabled: Boolean = true, + val androidSnapshotsPreviewParamsEnabled: Boolean = true, ) @Serializable