Skip to content

Commit

Permalink
Add additional logging (#93)
Browse files Browse the repository at this point in the history
* Add additional logging

* Add additional logging

* Detekt
  • Loading branch information
rbro112 authored Oct 20, 2023
1 parent 768e8d4 commit 44cc186
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ class PreviewProcessor(
val symbolsWithPreviewAnnotations = resolver
.getSymbolsWithAnnotation(COMPOSE_PREVIEW_ANNOTATION_NAME)
.toList()
val symbolsWithMultiPreviewAnnotations = resolver.getSymbolsWithMultiPreviewAnnotations()
val symbolsWithMultiPreviewAnnotations = resolver.getSymbolsWithMultiPreviewAnnotations(logger)

val previewAnnotatedFunctions = symbolsWithPreviewAnnotations
.functionsWithPreviewAnnotation()
val multiPreviewAnnotatedFunctions = symbolsWithMultiPreviewAnnotations
.functionsWithMultiPreviewAnnotations(resolver)
.functionsWithMultiPreviewAnnotations(resolver, logger)

val previewFunctionMap = buildMap {
putOrAppend(previewAnnotatedFunctions)
Expand All @@ -64,6 +64,8 @@ class PreviewProcessor(
val codeGenerator = environment.codeGenerator

val generatedPreviews = previewFunctionMap.entries.flatMap { previewFunction ->
logger.info("Generating snapshot tests for ${previewFunction.key.simpleName.asString()}")

val function = previewFunction.key
val previewConfigs = previewFunction.value
// Intentionally skipping functions with parameters for now.
Expand Down Expand Up @@ -156,6 +158,8 @@ class PreviewProcessor(
}.build()

fileSpec.writeTo(codeGenerator, Dependencies(false))
val filename = fileSpec.name
logger.info("Generated snapshot test: $filename")
fileSpec.name
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.emergetools.snapshots.processor.utils
import com.emergetools.snapshots.processor.preview.ComposePreviewUtils.getUniqueSnapshotConfigsFromMultiPreviewAnnotation
import com.emergetools.snapshots.processor.preview.ComposePreviewUtils.getUniqueSnapshotConfigsFromPreviewAnnotations
import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSAnnotation
Expand All @@ -19,12 +20,14 @@ fun List<KSAnnotated>.functionsWithPreviewAnnotation(): Map<KSFunctionDeclaratio

fun List<KSAnnotated>.functionsWithMultiPreviewAnnotations(
resolver: Resolver,
logger: KSPLogger,
): Map<KSFunctionDeclaration, List<ComposePreviewSnapshotConfig>> {
val uniqueSnapshotConfigs = filterIsInstance<KSFunctionDeclaration>()
.map { function ->
val allPreviewAnnotations = function.annotations.flatMap {
resolver.findAllDirectOrTransitivePreviewAnnotations(
it
annotation = it,
logger = logger,
)
}.toList()
function to getUniqueSnapshotConfigsFromMultiPreviewAnnotation(
Expand All @@ -36,32 +39,41 @@ fun List<KSAnnotated>.functionsWithMultiPreviewAnnotations(
// The same function declaration can show up multiple times, so ensure the values are merged together
val mergedConfigs = mutableMapOf<KSFunctionDeclaration, List<ComposePreviewSnapshotConfig>>()
uniqueSnapshotConfigs.forEach {
logger.info(
"Found multipreview function: ${it.first.simpleName.asString()} with configs: ${it.second.joinToString()}"
)
mergedConfigs.putOrAppend(it.first, it.second)
}

return mergedConfigs
}

fun Resolver.getSymbolsWithMultiPreviewAnnotations(): List<KSAnnotated> {
return getMultiPreviewAnnotations()
fun Resolver.getSymbolsWithMultiPreviewAnnotations(logger: KSPLogger): List<KSAnnotated> {
return getMultiPreviewAnnotations(logger)
.mapNotNull { annotation ->
val annotationQN = annotation.annotationType.resolve().declaration.qualifiedName
annotationQN?.let { getSymbolsWithAnnotation(it.asString()) }
}
.flatMap { it }
}

fun Resolver.getMultiPreviewAnnotations(): List<KSAnnotation> {
fun Resolver.getMultiPreviewAnnotations(logger: KSPLogger): List<KSAnnotation> {
return getAllFiles()
.flatMap { it.declarations }
.flatMap { it.annotations }
.toSet()
.filter {
val annotationQN = it.annotationType.resolve().declaration.qualifiedName
val annotationClassDecl =
annotationQN?.let { qualifiedName -> getClassDeclarationByName(qualifiedName) }
annotationClassDecl?.let { classDecl -> hasDirectOrTransitivePreviewAnnotation(classDecl) }
?: false
val annotationClassDecl = annotationQN?.let { qualifiedName ->
getClassDeclarationByName(qualifiedName)
}
annotationClassDecl?.let { classDecl ->
val hasPreviewAnnotation = hasDirectOrTransitivePreviewAnnotation(classDecl)
logger.info(
"Checking annotation for preview annotation: ${classDecl.qualifiedName?.asString()}, $hasPreviewAnnotation"
)
hasPreviewAnnotation
} ?: false
}
.toList()
.sortedBy { it.shortName.asString() }
Expand Down Expand Up @@ -97,11 +109,10 @@ fun Resolver.hasDirectOrTransitivePreviewAnnotation(
fun Resolver.findAllDirectOrTransitivePreviewAnnotations(
annotation: KSAnnotation,
seenAnnotations: MutableSet<KSClassDeclaration> = mutableSetOf(),
logger: KSPLogger,
): List<KSAnnotation> {
val classDeclaration = annotation.annotationType.resolve().declaration.qualifiedName?.let {
getClassDeclarationByName(
it
)
getClassDeclarationByName(it)
}
val isPreviewAnnotation =
classDeclaration?.qualifiedName?.asString() == COMPOSE_PREVIEW_ANNOTATION_NAME
Expand All @@ -112,6 +123,9 @@ fun Resolver.findAllDirectOrTransitivePreviewAnnotations(
return emptyList()
}

logger.info(
"Found preview or multipreview annotation: ${classDeclaration.qualifiedName?.asString()}"
)
seenAnnotations.add(classDeclaration)

val currentPreviewAnnotations =
Expand All @@ -122,7 +136,7 @@ fun Resolver.findAllDirectOrTransitivePreviewAnnotations(
}

val nestedPreviewAnnotations = classDeclaration.annotations.flatMap {
findAllDirectOrTransitivePreviewAnnotations(it, seenAnnotations)
findAllDirectOrTransitivePreviewAnnotations(it, seenAnnotations, logger)
}

return currentPreviewAnnotations + nestedPreviewAnnotations
Expand Down

0 comments on commit 44cc186

Please sign in to comment.