Skip to content

Commit

Permalink
Update kotlin to 1.8.10, xprocessing to 1.6.0-alpha01 (#317)
Browse files Browse the repository at this point in the history
* Bump kotlin, ksp, xprocessing, agp

* Update kotlin metadata usage

---------

Co-authored-by: Vinay Gaba <vinaygaba@gmail.com>
  • Loading branch information
elihart and vinaygaba committed Aug 29, 2023
1 parent 9f49a32 commit 360259e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 61 deletions.
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.KSP_VERSION = '1.7.10-1.0.6'
ext.KSP_VERSION = '1.8.10-1.0.9'
ext.versions = [
'androidXTestCore' : '1.4.0',
'androidXTestRules' : '1.4.0',
'assertJ' : '3.16.1',
'compose' : '1.2.1',
'composeCompiler' : '1.3.1',
'composeCompiler' : '1.4.2',
'composeActivity' : '1.5.1',
'composeConstraintLayout': '1.0.1',
'composeNavigation' : '2.5.1',
'detekt' : '1.7.4',
'espresso' : '3.2.0',
'gradle' : '7.2.1',
'gradle' : '7.4.0',
'junit' : '4.13.2',
'junitImplementation' : '1.1.2',
'kotlin' : '1.7.10',
'kotlinCompilerVersion' : '1.7.0',
'kotlinCompileTesting' : '1.4.9',
'kotlin' : '1.8.10',
'kotlinCompilerVersion' : '1.8.10',
'kotlinCompileTesting' : '1.5.0',
'kotlinPoet' : '1.12.0',
'kotlinXMetadata' : '0.5.0',
'kotlinXMetadata' : '0.6.0',
'ksp' : "$KSP_VERSION",
'ktx' : '1.1.0',
'lifecycle' : '2.2.0',
Expand All @@ -33,7 +33,7 @@ buildscript {
'mavenPublish' : '0.22.0',
'mdcComposeThemeAdapter' : '1.0.2',
'strikt' : '0.33.0',
'xprocessing' : '2.4.3',
'xprocessing' : '2.6.0-alpha01',
'corektx' : '1.7.0',
'shot' : '5.13.0'
]
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.compiler.processing.XFiler
import androidx.room.compiler.processing.XMessager
import androidx.room.compiler.processing.XProcessingEnv
import androidx.room.compiler.processing.XRoundEnv
import com.airbnb.android.showkase.processor.exceptions.ShowkaseProcessorException
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down Expand Up @@ -73,10 +74,8 @@ abstract class BaseProcessor(
final override fun process(resolver: Resolver): List<KSAnnotated> {
val kspEnvironment = requireNotNull(kspEnvironment)
environment = XProcessingEnv.create(
kspEnvironment.options,
kspEnvironment,
resolver,
kspEnvironment.codeGenerator,
kspEnvironment.logger
)
internalProcess(environment, XRoundEnv.create(environment))
return emptyList()
Expand Down Expand Up @@ -107,7 +106,11 @@ abstract class BaseProcessor(
} catch (e: Throwable) {
// Errors thrown from within KSP can get lost, making the root cause of an issue hidden.
// This helps to surface all thrown errors.
messager.printMessage(Diagnostic.Kind.ERROR, e.stackTraceToString())
if (e is ShowkaseProcessorException && e.element != null) {
messager.printMessage(Diagnostic.Kind.ERROR, e.stackTraceToString(), e.element)
} else {
messager.printMessage(Diagnostic.Kind.ERROR, e.stackTraceToString())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ShowkaseProcessor @JvmOverloads constructor(
) : BaseProcessor(kspEnvironment) {

private val logger = ShowkaseExceptionLogger()
private val showkaseValidator = ShowkaseValidator()
private val showkaseValidator by lazy { ShowkaseValidator(environment) }

override fun getSupportedAnnotationTypes(): MutableSet<String> {
val supportedAnnotations = mutableSetOf(
Expand Down Expand Up @@ -204,7 +204,7 @@ class ShowkaseProcessor @JvmOverloads constructor(
val skipPrivatePreviews = environment.options["skipPrivatePreviews"] == "true"
// Supported annotations from classpath
val supportedCustomPreview = mutableSetOf<ShowkaseMultiPreviewCodegenMetadata>()
environment.getTypeElementsFromPackage(CODEGEN_PACKAGE_NAME)
environment.getTypeElementsFromPackage(CODEGEN_PACKAGE_NAME)
.flatMap { it.getEnclosedElements() }.mapNotNull {
return@mapNotNull when (
val annotation = it.getAnnotation(ShowkaseMultiPreviewCodegenMetadata::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import kotlinx.metadata.jvm.KotlinClassMetadata
import javax.lang.model.element.Element
import kotlin.contracts.contract

internal class ShowkaseValidator {
internal class ShowkaseValidator(private val environment: XProcessingEnv) {

private val colorType by lazy { environment.requireType("androidx.compose.ui.graphics.Color") }

@Suppress("ThrowsCount")
internal fun validateComponentElementOrSkip(
element: XElement,
Expand All @@ -46,6 +49,7 @@ internal class ShowkaseValidator {
element
)
}

skipPrivatePreviews && element.isPrivate() -> return true
// Only check simple name to avoid costly type resolution
element.findAnnotationBySimpleName(COMPOSABLE_SIMPLE_NAME) == null -> {
Expand All @@ -54,6 +58,7 @@ internal class ShowkaseValidator {
element
)
}

element.isPrivate() -> {
throw ShowkaseProcessorException(
"The methods annotated with " +
Expand All @@ -75,6 +80,7 @@ internal class ShowkaseValidator {
element
)
}

else -> {
return false
}
Expand Down Expand Up @@ -122,9 +128,11 @@ internal class ShowkaseValidator {
is KotlinClassMetadata.FileFacade -> metadata.toKmPackage().functions.validateKaptComposableParameter(
composableMethodElement
)

is KotlinClassMetadata.Class -> metadata.toKmClass().functions.validateKaptComposableParameter(
composableMethodElement
)

else -> false
}

Expand Down Expand Up @@ -180,25 +188,34 @@ internal class ShowkaseValidator {
contract {
returns() implies (element is XFieldElement)
}
when {
!element.isField() -> {
throw ShowkaseProcessorException(
"Only \"Color\" fields can be annotated with $annotationName",
element
)
}
!element.type.isLong() -> {
throw ShowkaseProcessorException(
"Only \"Color\" fields can be annotated with $annotationName",
element
)

if (!element.isField()) {
throw ShowkaseProcessorException(
"Only \"Color\" fields can be annotated with $annotationName",
element
)
}

when (environment.backend) {
XProcessingEnv.Backend.JAVAC -> {
// Kapt can't see that the original type is a value class, it just sees the raw
// type of the Color value class which is a Long
if (element.type.isLong()) return
}
// TODO(vinay.gaba) Also add the private modifier check. Unfortunately, the java code
// for this element adds a private modifier since it's a field. Potentially use
// kotlinMetadata to enforce this check.
else -> {

XProcessingEnv.Backend.KSP -> {
if (element.type.rawType == colorType.rawType) return
}
}

throw ShowkaseProcessorException(
"Only \"Color\" fields can be annotated with $annotationName",
element
)

// TODO(vinay.gaba) Also add the private modifier check. Unfortunately, the java code
// for this element adds a private modifier since it's a field. Potentially use
// kotlinMetadata to enforce this check.
}

internal fun validateTypographyElement(
Expand All @@ -216,6 +233,7 @@ internal class ShowkaseValidator {
element
)
}

!element.type.isSameType(textStyleType) -> {
throw ShowkaseProcessorException(
"Only \"TextStyle\" fields can be annotated with $annotationName",
Expand Down Expand Up @@ -245,6 +263,7 @@ internal class ShowkaseValidator {
elementSet.first()
)
}

else -> {
// Safe to do this as we've ensured that there's only one element in this set
val element = elementSet.first()
Expand Down Expand Up @@ -322,6 +341,7 @@ internal class ShowkaseValidator {
elements.first()
)
}

else -> {
// Safe to do this as we've ensured that there's only one element in this set
val element = elements.first()
Expand All @@ -338,17 +358,19 @@ internal class ShowkaseValidator {
showkaseScreenshotTestTypeMirror.isAssignableFrom(element.type)

return if (isShowkaseScreenshotTest) {
ScreenshotTestType.SHOWKASE
ScreenshotTestType.SHOWKASE
} else if (
environment.findType(PAPARAZZI_SHOWKASE_SCREENSHOT_TEST_CLASS_NAME)
?.isAssignableFrom(element.type) == true
) {
val paparazziShowkaseScreenshotTestTypeMirror = environment
.requireType(PAPARAZZI_SHOWKASE_SCREENSHOT_TEST_CLASS_NAME)
validatePaparazziShowkaseScreenshotTest(environment, element,
paparazziShowkaseScreenshotTestTypeMirror)
validatePaparazziShowkaseScreenshotTest(
environment, element,
paparazziShowkaseScreenshotTestTypeMirror
)

ScreenshotTestType.PAPARAZZI_SHOWKASE
ScreenshotTestType.PAPARAZZI_SHOWKASE
} else {
throw ShowkaseProcessorException(
"Only an implementation of com.airbnb.android.showkase.screenshot.testing" +
Expand Down Expand Up @@ -376,9 +398,10 @@ internal class ShowkaseValidator {
val companionObjectTypeElements = element.getEnclosedTypeElements().filter {
it.isCompanionObject()
}
val errorMessage = "Classes implementing the ${paparazziShowkaseScreenshotTestTypeMirror.typeName} " +
"interface should have a companion object that implements the " +
"${paparazziShowkaseScreenshotTestCompanionType.typeName} interface."
val errorMessage =
"Classes implementing the ${paparazziShowkaseScreenshotTestTypeMirror.typeName} " +
"interface should have a companion object that implements the " +
"${paparazziShowkaseScreenshotTestCompanionType.typeName} interface."
if (companionObjectTypeElements.isEmpty()) {
throw ShowkaseProcessorException(
errorMessage,
Expand All @@ -387,7 +410,8 @@ internal class ShowkaseValidator {
}

if (!paparazziShowkaseScreenshotTestCompanionType
.isAssignableFrom(companionObjectTypeElements[0].type)) {
.isAssignableFrom(companionObjectTypeElements[0].type)
) {
throw ShowkaseProcessorException(
errorMessage,
element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.javapoet.toKClassName
import com.squareup.kotlinpoet.javapoet.toKTypeName
import kotlinx.metadata.jvm.KotlinClassHeader.Companion.FILE_FACADE_KIND
import kotlinx.metadata.jvm.KotlinClassMetadata
import java.util.Locale

@Suppress("LongParameterList")
Expand Down Expand Up @@ -513,14 +511,8 @@ fun XElement.isTopLevel(enclosingElement: XMemberContainer): Boolean {
// class type. This is null though if the type doesn't have metadata, such as in the case
// of a top level function.
val kotlinMetadata = xTypeElement.getFieldWithReflection<Any?>("kotlinMetadata")
?: return true

val enclosingElementKind = kotlinMetadata
.getFieldWithReflection<KotlinClassMetadata.Class>("classMetadata")
.header
.kind

enclosingElementKind == FILE_FACADE_KIND
return kotlinMetadata == null
} else {
// Per enclosingElement kdoc:
// When running with KSP, if this function is in source, the value will NOT be an XTypeElement.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package com.airbnb.android.showkase.processor.utils

import kotlinx.metadata.jvm.KotlinClassHeader
import kotlinx.metadata.jvm.KotlinClassMetadata
import javax.lang.model.element.Element

internal fun Element.kotlinMetadata(): KotlinClassMetadata? {
// https://github.com/JetBrains/kotlin/tree/master/libraries/kotlinx-metadata/jvm
val kotlinMetadataAnnotation = getAnnotation(Metadata::class.java) ?: return null
val header = KotlinClassHeader(
kind = kotlinMetadataAnnotation.kind,
metadataVersion = kotlinMetadataAnnotation.metadataVersion,
data1 = kotlinMetadataAnnotation.data1,
data2 = kotlinMetadataAnnotation.data2,
extraString = kotlinMetadataAnnotation.extraString,
packageName = kotlinMetadataAnnotation.packageName,
extraInt = kotlinMetadataAnnotation.extraInt
)

return KotlinClassMetadata.read(header)
return KotlinClassMetadata.read(kotlinMetadataAnnotation)
}

0 comments on commit 360259e

Please sign in to comment.