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

Properties file symbol processor #27

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lyricist-processor-properties/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
7 changes: 7 additions & 0 deletions lyricist-processor-properties/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apply from: "../kotlin-module.gradle"
apply plugin: "com.vanniktech.maven.publish"

dependencies {
implementation libs.ksp
implementation libs.caseFormat
}
2 changes: 2 additions & 0 deletions lyricist-processor-properties/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POM_NAME=Lyricist Properties Processor
POM_ARTIFACT_ID=lyricist-processor-properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cafe.adriel.lyricist.processor.properties

internal fun createTemplate(className: String, fields: String): String =
"""
|public data class $className(
| $fields
|)
""".trimMargin()
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cafe.adriel.lyricist.processor.properties

import java.util.Properties

public fun resolveProperties(properties: Properties): String = properties.map { (key, value) ->
val name = removeDots(key as String)
val (type, fieldValue) = resolveType(value as String)

"val $name: $type = $fieldValue"
}.joinToString(separator = ",\n")

private fun removeDots(input: String): String {
var replaced = input
while (true) {
val dot = replaced.indexOf(".")
if (dot == -1) break

val first = replaced[dot + 1]

replaced = replaced.replaceRange(dot..dot + 1, first.uppercase())
}
return replaced
}

private fun resolveType(value: String): Pair<String, String> = when {
value.toBooleanStrictOrNull() != null -> "Boolean" to value
value.toIntOrNull() != null -> "Int" to value
else -> resolveComplexExpressionType(value) ?: ("String" to "\"$value\"")
}

private val ValueWithArgumentsExpr = Regex("\\{(.*?)}")

private fun resolveComplexExpressionType(value: String): Pair<String, String>? {
val matches = ValueWithArgumentsExpr.findAll(value)
val textArgs = matches.map(MatchResult::value).toList()
if (textArgs.isEmpty())
return null

val names = textArgs.map { name -> name.substring(1 until name.lastIndex) }
val fnArgs = names.joinToString(separator = ", ") { name -> "$name: String" }
val returnArgs = names.joinToString(separator = ", ")
var replacedValue = value
for (match in matches)
replacedValue = replacedValue.replace(match.value, "$${match.value}")

return "($fnArgs) -> String" to "{ $returnArgs -> \"$replacedValue\" }"
}
6 changes: 6 additions & 0 deletions sample-properties/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apply plugin: "com.google.devtools.ksp"
apply from: "../kotlin-module.gradle"

dependencies {
ksp(project(":lyricist-processor-properties"))
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ include(
":lyricist-core",
":lyricist-processor-compose",
":lyricist-processor-xml",
":lyricist-processor-properties",
":sample",
":sample-xml",
":sample-multi-module",
":sample-multiplatform",
":sample-properties",
)

// There's an issue when a module has the same name of the project
Expand Down