Skip to content

Commit

Permalink
Handle private previews for Custom annotations (#313)
Browse files Browse the repository at this point in the history
* Handle private previews for Custom annotations

The validation was not quite handled for the custom annotation processing steps and it had not passed the flag since it had a default argument.

* Add comment to test and fix spelling misstake
  • Loading branch information
oas004 authored Apr 4, 2023
1 parent 7555419 commit 063b9de
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ fun CustomRoundedSquareWithText() {
Text("This is a rounded square!")
}
}

@FontPreview
@Composable
private fun PrivateCustomRoundedSquareWithText() {
Box(Modifier.size(100.dp).clip(RoundedCornerShape(8.dp))) {
Text("This is a private rounded square!")
}
}
4 changes: 4 additions & 0 deletions showkase-browser-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ apply plugin: 'kotlin-android'

if (project.hasProperty('useKsp')) {
apply plugin: 'com.google.devtools.ksp'
ksp {
arg("skipPrivatePreviews", "true")
}
} else {
apply plugin: 'kotlin-kapt'
kapt {
correctErrorTypes = true
arguments {
arg("multiPreviewType", "com.airbnb.android.submodule.showkasesample.LocalePreview")
arg("skipPrivatePreviews", "true")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,4 +816,39 @@ class ShowcaseBrowserTest {

}
}

// We have enabled private preview compiler flag so this test
// should test that we can have private methods annotated with custom annotations
// and that they will not show in the application.
@Test
fun customAnnotatedPrivateComposablesShouldCompileButNotShow() {

composeTestRule.apply {

verifyLandingScreen(
components = componentSize,
typography = 13,
colors = 4,
)
// Tap on the "Components" row
clickRowWithText("Components ($componentSize)")

waitForIdle()

onRoot().performTouchInput {
swipeUp()
}

waitForIdle()

val composables = if (BuildConfig.IS_RUNNING_KSP) 3 else 1

clickRowWithText("LocalePreview ($composables)")

onNodeWithText("Private Text Composable").assertDoesNotExist()

waitForIdle()

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,11 @@ fun PreviewEnglishText() {
BasicText(text = "Some text In locale")
}

@EnglishLocalePreview
@Composable
private fun PrivateTextComposable() {
BasicText(text = "Private Text Composable")
}

@ShowkaseRoot
class MyRootModule: ShowkaseRootModule
Original file line number Diff line number Diff line change
Expand Up @@ -611,5 +611,12 @@ class ShowkaseProcessorTest : BaseProcessorTest() {
fun `top level composable function with showkase and showkaseroot with tags and metadata`() {
compileInputsAndVerifyOutputs()
}

@Test
fun `composable function with private custom preview annotation compiles with flag`() {
val options = mutableMapOf<String, String>()
options["skipPrivatePreviews"] = "true"
compileInputsAndVerifyOutputs(options = options)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.airbnb.android.showkase_processor_testing

import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview


@Preview(
name = "large Screen",
group = "device",
)
public annotation class DevicePreviews

public class Composables {

@DevicePreviews
@Composable
private fun Component() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This is an auto-generated file. Please do not edit/modify this file.
package com.airbnb.android.showkase

import com.airbnb.android.showkase.`annotation`.ShowkaseMultiPreviewCodegenMetadata
import kotlin.Unit

public class ShowkaseMetadata_showkase_com_airbnb_android_showkase_processor_testing_devicepreviews
{
@ShowkaseMultiPreviewCodegenMetadata(
previewName = "large Screen",
previewGroup = "device",
supportTypeQualifiedName = "com.airbnb.android.showkase_processor_testing.DevicePreviews",
showkaseWidth = -1,
showkaseHeight = -1,
packageName = "com.airbnb.android.showkase_processor_testing",
)
public fun Preview_0(): Unit {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import com.airbnb.android.showkase.processor.models.getShowkaseMetadataFromPrevi
import com.airbnb.android.showkase.processor.models.getShowkaseTypographyMetadata
import com.airbnb.android.showkase.processor.writer.PaparazziShowkaseScreenshotTestWriter
import com.airbnb.android.showkase.processor.writer.ShowkaseBrowserProperties
import com.airbnb.android.showkase.processor.writer.ShowkaseBrowserPropertyWriter
import com.airbnb.android.showkase.processor.writer.ShowkaseBrowserWriter
import com.airbnb.android.showkase.processor.writer.ShowkaseBrowserWriter.Companion.CODEGEN_AUTOGEN_CLASS_NAME
import com.airbnb.android.showkase.processor.writer.ShowkaseCodegenMetadataWriter
import com.airbnb.android.showkase.processor.writer.ShowkaseExtensionFunctionsWriter
import com.airbnb.android.showkase.processor.writer.ShowkaseBrowserPropertyWriter
import com.airbnb.android.showkase.processor.writer.ShowkaseScreenshotTestWriter
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down Expand Up @@ -137,7 +137,11 @@ class ShowkaseProcessor @JvmOverloads constructor(
ShowkaseBrowserWriter(environment).writeCustomAnnotationElementToMetadata(
element
)
return@mapNotNull processCustomAnnotation(roundEnvironment, element)
return@mapNotNull processCustomAnnotation(
skipPrivatePreviews = skipPrivatePreviews,
roundEnvironment = roundEnvironment,
annotation = element
)
}
val skipElement = showkaseValidator.validateComponentElementOrSkip(
element,
Expand All @@ -154,6 +158,7 @@ class ShowkaseProcessor @JvmOverloads constructor(
}

private fun processCustomAnnotation(
skipPrivatePreviews: Boolean,
roundEnvironment: XRoundEnv,
annotation: XTypeElement? = null
): Set<ShowkaseMetadata.Component> {
Expand All @@ -166,17 +171,20 @@ class ShowkaseProcessor @JvmOverloads constructor(
annotatedElements.map { annotatedElement ->
if (!showkaseValidator.checkElementIsAnnotationClass(annotatedElement)) {

showkaseValidator.validateComponentElementOrSkip(
val skipable = showkaseValidator.validateComponentElementOrSkip(
element = annotatedElement,
annotationName = supportedType,
skipPrivatePreviews = skipPrivatePreviews
)
components.addAll(
getShowkaseMetadataFromCustomAnnotation(
element = annotatedElement,
showkaseValidator = showkaseValidator,
supportedType.getCustomAnnotationSimpleName(),
).toSet()
)
if (!skipable) {
components.addAll(
getShowkaseMetadataFromCustomAnnotation(
element = annotatedElement,
showkaseValidator = showkaseValidator,
supportedType.getCustomAnnotationSimpleName(),
).toSet()
)
}
}
}
}
Expand All @@ -193,6 +201,7 @@ class ShowkaseProcessor @JvmOverloads constructor(
// from the annotation from classpath. We use the fields from the classpath annotation to build
// common data for the ShowkaseMetadata.

val skipPrivatePreviews = environment.options["skipPrivatePreviews"] == "true"
// Supported annotations from classpath
val supportedCustomPreview = mutableSetOf<ShowkaseMultiPreviewCodegenMetadata>()
environment.getTypeElementsFromPackage(CODEGEN_PACKAGE_NAME)
Expand Down Expand Up @@ -223,19 +232,22 @@ class ShowkaseProcessor @JvmOverloads constructor(
roundEnvironment
.getElementsAnnotatedWith(customPreviewMetadata.supportTypeQualifiedName)
.mapIndexed elementRoot@{ elementIndex, xElement ->
showkaseValidator.validateComponentElementOrSkip(
val skippable = showkaseValidator.validateComponentElementOrSkip(
xElement,
customPreviewMetadata.supportTypeQualifiedName,
skipPrivatePreviews = skipPrivatePreviews
)
components.add(
getShowkaseMetadata(
xElement = xElement,
customPreviewMetadata = customPreviewMetadata,
elementIndex = elementIndex,
index = index,
showkaseValidator = showkaseValidator
if (!skippable) {
components.add(
getShowkaseMetadata(
xElement = xElement,
customPreviewMetadata = customPreviewMetadata,
elementIndex = elementIndex,
index = index,
showkaseValidator = showkaseValidator
)
)
)
}

}
}
Expand Down

0 comments on commit 063b9de

Please sign in to comment.