Skip to content

Commit

Permalink
Cleanup processor to remove preview param support
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-powell committed Aug 26, 2024
1 parent 05f0d2b commit 892982e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 245 deletions.
9 changes: 0 additions & 9 deletions buildSrc/build.gradle

This file was deleted.

9 changes: 0 additions & 9 deletions buildSrc/settings.gradle

This file was deleted.

114 changes: 0 additions & 114 deletions gradle/aarAsJar.gradle

This file was deleted.

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bytebuddy = "1.14.19"
coroutines = "1.8.1"
javaTarget = "11"
kotlin = "2.0.10"
kotlinPoet = "1.16.0"
kotlinPoet = "1.18.1"
ksp = "2.0.10-1.0.24"
layoutlib = "14.0.9"
moshi = "1.15.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ internal object PaparazziPoet {
emptyList()
} else {
listOf(
buildAnnotationsFile("paparazziPreviews", functions, env)
buildAnnotationsFile(
fileName = "PaparazziPreviews",
propertyName = "paparazziPreviews",
functions = functions,
env = env
)
)
}

@Suppress("SameParameterValue")
private fun buildAnnotationsFile(
fileName: String,
propertyName: String,
functions: Sequence<KSFunctionDeclaration>,
env: EnvironmentOptions
) =
FileSpec.scriptBuilder(propertyName, env.namespace)
FileSpec.scriptBuilder(fileName, env.namespace)
.addCode(
buildCodeBlock {
addStatement("internal val %L = listOf<%L.PaparazziPreviewData>(", propertyName, PACKAGE_NAME)
Expand All @@ -38,28 +44,30 @@ internal object PaparazziPoet {
if (functions.count() == 0) {
addEmpty()
} else {
functions.process { func, preview, previewParam ->
val visibilityCheck = checkVisibility(func, previewParam)
functions.process { func, previewParam ->
val visibilityCheck = checkVisibility(func)
val snapshotName = func.snapshotName(env)

when {
visibilityCheck.isPrivate -> addError(
visibilityCheck = visibilityCheck,
function = func,
snapshotName = snapshotName,
preview = preview,
previewParam = previewParam
buildErrorMessage = {
"$it is private. Make it internal or public to generate a snapshot."
}
)
previewParam != null -> addProvider(
previewParam != null -> addError(
visibilityCheck = visibilityCheck,
function = func,
snapshotName = snapshotName,
preview = preview,
previewParam = previewParam
buildErrorMessage = {
"$it preview uses PreviewParameters which aren't currently supported."
}
)
else -> addDefault(
function = func,
snapshotName = snapshotName,
preview = preview
snapshotName = snapshotName
)
}
}
Expand All @@ -76,102 +84,56 @@ internal object PaparazziPoet {
}

private fun Sequence<KSFunctionDeclaration>.process(
block: (KSFunctionDeclaration, PreviewModel, KSValueParameter?) -> Unit
block: (KSFunctionDeclaration, KSValueParameter?) -> Unit
) =
flatMap { func ->
val previewParam = func.previewParam()
func.findDistinctPreviews()
.map { Triple(func, it, previewParam) }
}.forEach { (func, preview, previewParam) ->
block(func, preview, previewParam)
.map { Pair(func, previewParam) }
}.forEach { (func, previewParam) ->
block(func, previewParam)
}

private fun CodeBlock.Builder.addError(
visibilityCheck: VisibilityCheck,
function: KSFunctionDeclaration,
snapshotName: String,
preview: PreviewModel,
previewParam: KSValueParameter?
buildErrorMessage: (String?) -> String
) {
val qualifiedName = if (visibilityCheck.isFunctionPrivate) {
function.qualifiedName?.asString()
} else {
previewParam?.previewParamProvider()?.qualifiedName?.asString()
null
}

addStatement("%L.PaparazziPreviewData.Error(", PACKAGE_NAME)
indent()
addStatement("snapshotName = %S,", snapshotName)
addStatement("message = %S,", "$qualifiedName is private. Make it internal or public to generate a snapshot.")
addPreviewData(preview)
addStatement("message = %S,", buildErrorMessage(qualifiedName))
unindent()
addStatement("),")
}

private fun CodeBlock.Builder.addProvider(
function: KSFunctionDeclaration,
snapshotName: String,
preview: PreviewModel,
previewParam: KSValueParameter
snapshotName: String
) {
addStatement("%L.PaparazziPreviewData.Provider(", PACKAGE_NAME)
indent()
addStatement("snapshotName = %S,", snapshotName)
addStatement("composable = { %L(it) },", function.qualifiedName?.asString())
addPreviewParameterData(previewParam)
addPreviewData(preview)
unindent()
addStatement("),")
}

private fun CodeBlock.Builder.addDefault(
function: KSFunctionDeclaration,
snapshotName: String,
preview: PreviewModel
snapshotName: String
) {
addStatement("%L.PaparazziPreviewData.Default(", PACKAGE_NAME)
indent()
addStatement("snapshotName = %S,", snapshotName)
addStatement("composable = { %L() },", function.qualifiedName?.asString())
addPreviewData(preview)
unindent()
addStatement("),")
}

private fun CodeBlock.Builder.addPreviewData(preview: PreviewModel) {
addStatement("preview = %L.PreviewData(", PACKAGE_NAME)
indent()

preview.fontScale.takeIf { it != 1f }
?.let { addStatement("fontScale = %Lf,", it) }

preview.device.takeIf { it.isNotEmpty() }
?.let { addStatement("device = %S,", it) }

preview.widthDp.takeIf { it > -1 }
?.let { addStatement("widthDp = %L,", it) }

preview.heightDp.takeIf { it > -1 }
?.let { addStatement("heightDp = %L,", it) }

preview.uiMode.takeIf { it != 0 }
?.let { addStatement("uiMode = %L,", it) }

preview.locale.takeIf { it.isNotEmpty() }
?.let { addStatement("locale = %S,", it) }

preview.backgroundColor.takeIf { it != 0L && preview.showBackground }
?.let { addStatement("backgroundColor = %S", it.toString(16)) }

unindent()
addStatement("),")
}

private fun CodeBlock.Builder.addPreviewParameterData(previewParam: KSValueParameter) {
addStatement("previewParameter = %L.PreviewParameterData(", PACKAGE_NAME)
indent()
addStatement("name = %S,", previewParam.name?.asString())
addStatement("values = %L().values,", previewParam.previewParamProvider().qualifiedName?.asString())
unindent()
addStatement("),")
}
Expand All @@ -187,17 +149,14 @@ internal object PaparazziPoet {
}.joinToString("_")

private fun checkVisibility(
function: KSFunctionDeclaration,
previewParam: KSValueParameter?
function: KSFunctionDeclaration
) = VisibilityCheck(
isFunctionPrivate = function.getVisibility() == Visibility.PRIVATE,
isPreviewParamProviderPrivate = previewParam?.previewParamProvider()?.getVisibility() == Visibility.PRIVATE
isFunctionPrivate = function.getVisibility() == Visibility.PRIVATE
)
}

internal data class VisibilityCheck(
val isFunctionPrivate: Boolean,
val isPreviewParamProviderPrivate: Boolean
val isFunctionPrivate: Boolean
) {
val isPrivate = isFunctionPrivate || isPreviewParamProviderPrivate
val isPrivate = isFunctionPrivate
}
Loading

0 comments on commit 892982e

Please sign in to comment.