Skip to content

Commit

Permalink
Add includePreviewParamPreviews extension for allowing users to opt-o…
Browse files Browse the repository at this point in the history
…ut of preview param support (#303)

* Add includePreviewParamPreviews extension for allowing users to opt-out of preview param support

* Doc
  • Loading branch information
rbro112 authored Nov 8, 2024
1 parent e0ad8c5 commit 4913246
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
23 changes: 14 additions & 9 deletions gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ class EmergePlugin : Plugin<Project> {
"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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ abstract class SnapshotOptions : ProductOptions() {
* Include private previews in snapshot generation, defaults to true
*/
abstract val includePrivatePreviews: Property<Boolean>

/**
* Include previews with PreviewParameter annotated Previews in snapshot generation,
* defaults to true
*/
abstract val includePreviewParamPreviews: Property<Boolean>
}

abstract class ReaperOptions : ProductOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ abstract class LocalSnapshots : DefaultTask() {
@get:Optional
abstract val includePrivatePreviews: Property<Boolean>

@get:Input
@get:Optional
abstract val includePreviewParamPreviews: Property<Boolean>

@TaskAction
fun execute() {
val artifactMetadataFiles = packageDir.asFileTree.matching {
Expand Down Expand Up @@ -105,6 +109,7 @@ abstract class LocalSnapshots : DefaultTask() {
val composeSnapshots = PreviewUtils.findPreviews(
extractedApkDir,
includePrivatePreviews.getOrElse(true),
includePreviewParamPreviews.getOrElse(true),
previews,
logger
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ abstract class UploadSnapshotBundle : BaseUploadTask() {
@get:Optional
abstract val includePrivatePreviews: Property<Boolean>

@get:Input
@get:Optional
abstract val includePreviewParamPreviews: Property<Boolean>

@get:InputFile
abstract val artifactMetadataPath: RegularFileProperty

Expand Down Expand Up @@ -70,6 +74,7 @@ abstract class UploadSnapshotBundle : BaseUploadTask() {
androidSnapshotsEnabled = true,
androidSnapshotsApiVersion = apiVersion.orNull,
androidSnapshotsPrivateEnabled = includePrivatePreviews.getOrElse(true),
androidSnapshotsPreviewParamsEnabled = includePreviewParamPreviews.getOrElse(true),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object PreviewUtils {
fun findPreviews(
extractedApkDirectory: File,
includePrivatePreviews: Boolean,
includePreviewParamPreviews: Boolean,
previewFunctions: List<String>,
logger: Logger,
): ComposeSnapshots {
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand All @@ -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 ->
Expand All @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ data class EmergeUploadRequestData(
val androidSnapshotsEnabled: Boolean,
val androidSnapshotsApiVersion: Int? = null,
val androidSnapshotsPrivateEnabled: Boolean = true,
val androidSnapshotsPreviewParamsEnabled: Boolean = true,
)

@Serializable
Expand Down

0 comments on commit 4913246

Please sign in to comment.