Skip to content

Commit

Permalink
Expose Enum properties. Bump version 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Lureau committed Jan 26, 2022
1 parent 2f7bf50 commit 23c0198
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 31 deletions.
13 changes: 9 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ buildscript {
}

val localProperties = java.util.Properties().apply {
val propPath = "local.properties"
if (File(propPath).exists()) {
load(java.io.FileInputStream(File(rootProject.rootDir, propPath)))
val file = File(rootProject.rootDir, "local.properties")
if (file.exists()) {
load(java.io.FileInputStream(file))
}
}

Expand All @@ -24,7 +24,7 @@ plugins {

allprojects {
group = "deezer.kustomexport"
version = "0.1.0"
version = "0.1.1"

repositories {
mavenLocal()
Expand Down Expand Up @@ -61,6 +61,10 @@ if (gitUser != null && gitPassword != null) {
System.setProperty("org.ajoberstar.grgit.auth.password", gitPassword)
}

tasks.create<Delete>("cleanMavenLocalArtifacts") {
delete = setOf("$buildDir/mvn-repo/")
}

tasks.create<Sync>("copyMavenLocalArtifacts") {
group = "publishing"
dependsOn(":compiler:publishToMavenLocal", ":lib:publishToMavenLocal")
Expand All @@ -84,4 +88,5 @@ gitPublish {
val head = grgit.head()
commitMessage.set("${head.abbreviatedId}: ${project.version} : ${head.fullMessage}")
}
tasks["copyMavenLocalArtifacts"].dependsOn("cleanMavenLocalArtifacts")
tasks["gitPublishCopy"].dependsOn("copyMavenLocalArtifacts")
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ data class ClassDescriptor(
data class EnumDescriptor(
val packageName: String,
val classSimpleName: String,
val entries: List<Entry>
val properties: List<PropertyDescriptor>,
val entries: List<Entry>,
) : Descriptor() {
data class Entry(val name: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package deezer.kustomexport.compiler.js.pattern

import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.getConstructors
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.getDeclaredProperties
Expand Down Expand Up @@ -80,6 +81,7 @@ fun parseClass(

//val superTypes = classDeclaration.getAllSuperTypes()

Logger.warn("PARSING - ${classSimpleName} ${classDeclaration.superTypes.count()} ${classDeclaration.getAllSuperTypes().count()}")
val superTypes = classDeclaration.superTypes
.map { superType ->
val superTypeName = superType.toTypeNamePatch(typeParamResolver).cached(concreteTypeParameters)
Expand Down Expand Up @@ -159,6 +161,7 @@ fun parseClass(
classKind == ClassKind.ENUM_CLASS -> EnumDescriptor(
packageName = packageName,
classSimpleName = classSimpleName,
properties = properties,
entries = classDeclaration.declarations
.filterIsInstance<KSClassDeclaration>()
.map { EnumDescriptor.Entry(it.simpleName.asString()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import deezer.kustomexport.compiler.js.EnumDescriptor
import deezer.kustomexport.compiler.js.jsExport
import deezer.kustomexport.compiler.js.jsPackage
import deezer.kustomexport.compiler.js.mapping.INDENTATION
import deezer.kustomexport.compiler.js.pattern.overrideGetterSetter

fun EnumDescriptor.transform() = transformEnum(this)

Expand All @@ -54,30 +55,26 @@ fun transformEnum(origin: EnumDescriptor): FileSpec {
)
.addProperty(PropertySpec.builder(delegateName, originalClass).initializer(delegateName).build())
.addProperty(PropertySpec.builder("name", STRING).initializer("$delegateName.name").build())
/*.addType(TypeSpec.companionObjectBuilder()
.addFunction(
FunSpec.builder("values")
.returns(ARRAY.parameterizedBy(jsExportedClass))
.addCode(
"return·arrayOf(" +
origin.entries.joinToString { origin.generatedName(it) } +
")"
)
.build()
)
.addFunction(
FunSpec.builder("valueOf")
.returns(jsExportedClass.copy(nullable = true))
.addParameter("name", STRING)
.addCode(
origin.entries.joinToString("\n") {
"if (name == ${origin.generatedName(it)}.name) return ${origin.generatedName(it)}"
} + "\nreturn null"
)
.build()
)
.build()
)*/
.also { builder ->
origin.properties
// Don't export fields only present in super implementation
// .filterNot { p -> origin.supers.any { s -> s.parameters?.any { it.name == p.name } ?: false } }
.forEach {
if (it.name != "name" && it.name != "ordinal") { // Kotlin keywords
builder.addProperty(
overrideGetterSetter(
prop = it,
target = delegateName,
import = false,
forceOverride = false,
isClassOpen = false,
isClassThrowable = false
)
)
}
}

}
.build()
)
.addFunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ import deezer.kustomexport.compiler.js.PropertyDescriptor
import deezer.kustomexport.compiler.js.jsExport
import deezer.kustomexport.compiler.js.jsPackage
import deezer.kustomexport.compiler.js.mapping.INDENTATION
import deezer.kustomexport.compiler.js.pattern.asClassName
import deezer.kustomexport.compiler.js.pattern.buildWrappingFunction
import deezer.kustomexport.compiler.js.pattern.overrideGetterSetter
import deezer.kustomexport.compiler.js.pattern.packageName
import deezer.kustomexport.compiler.js.pattern.simpleName
import java.util.Locale

fun InterfaceDescriptor.transform() = transformInterface(this)
Expand Down
18 changes: 18 additions & 0 deletions samples/src/commonMain/kotlin/sample/_enum/Enum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ enum class Direction {
NORTH, SOUTH, WEST, EAST
}

val d = Direction.NORTH

@KustomExport
enum class DirectionWithData(val enName: String, val frName: String) {
NORTH("North", "Nord"),
SOUTH("South", "Sud"),
WEST("West", "Ouest"),
EAST("East", "Est")
}

@KustomExport
class Engine {
fun goTo(direction: Direction): String {
Expand All @@ -34,4 +44,12 @@ class Engine {
Direction.EAST -> "➡️"
}
}

fun translateEnName(d: DirectionWithData): String {
return d.frName
}

fun translateFrName(d: DirectionWithData): String {
return d.enName
}
}
3 changes: 3 additions & 0 deletions samples/src/commonMain/kotlin/sample/_enum/Enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ runTest("Enum", () : void => {
assertEqualsQuiet(sample._enum.js.Direction_WEST, sample._enum.js.Direction_valueOf("WEST"), "valueOf(WEST) -> WEST")
assertEqualsQuiet(sample._enum.js.Direction_EAST, sample._enum.js.Direction_valueOf("EAST"), "valueOf(EAST) -> EAST")
assertEquals(null, sample._enum.js.Direction_valueOf("UP"), "valueOf(UP) is null")

assertEqualsQuiet("South", engine.translateFrName(sample._enum.js.DirectionWithData_SOUTH), "check additional field 1")
assertEqualsQuiet("Sud", engine.translateEnName(sample._enum.js.DirectionWithData_SOUTH), "check additional field 2")
})
4 changes: 2 additions & 2 deletions samples/src/commonMain/kotlin/sample/generics/Generics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
KustomGenerics(GenericsStuff::class, arrayOf(Float::class), "GenericsStuffFloat"),
KustomGenerics(GenericsInterface::class, arrayOf(Long::class), "GenericsInterface"),
KustomGenerics(GenericsInterface::class, arrayOf(Float::class), "GenericsInterfaceFloat"),
KustomGenerics(SuperGenericsInterface::class, arrayOf(Float::class), "SuperGenericsInterfaceFloat"),
KustomGenerics(SuperGenericsInterface::class, arrayOf(Float::class, Int::class), "SuperGenericsInterfaceFloat"),
KustomGenerics(GenericsImpl::class, arrayOf(Long::class), "GenericsImpl"),
KustomGenerics(GenericsImpl::class, arrayOf(Float::class), "GenericsImplFloat"),
]
Expand Down Expand Up @@ -49,7 +49,7 @@ interface GenericsInterface<Template> {
fun baz() = DataClass("baz data")
}

interface SuperGenericsInterface<Template> : GenericsInterface<Template> {
interface SuperGenericsInterface<Template, SomethingElse> : GenericsInterface<Template> {
val superFoo: Template
}

Expand Down

0 comments on commit 23c0198

Please sign in to comment.