Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading