1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
* Copyright (c) 2017-present Robert Jaros
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package pl.treksoft.kvision.plugin
import de.jensklingenberg.mpapt.common.canonicalFilePath
import de.jensklingenberg.mpapt.common.methods
import de.jensklingenberg.mpapt.model.AbstractProcessor
import de.jensklingenberg.mpapt.model.Element
import de.jensklingenberg.mpapt.model.RoundEnvironment
import de.jensklingenberg.mpapt.utils.KotlinPlatformValues
import org.jetbrains.kotlin.backend.common.descriptors.allParameters
import org.jetbrains.kotlin.cli.common.config.kotlinSourceRoots
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ParameterDescriptor
import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.types.KotlinType
import pl.treksoft.kvision.annotations.KVService
import java.io.File
class KVProcessor : AbstractProcessor() {
override fun isTargetPlatformSupported(platform: TargetPlatform): Boolean {
val targetName = platform.first().platformName
return when (targetName) {
KotlinPlatformValues.JS -> false
KotlinPlatformValues.JVM -> true
KotlinPlatformValues.NATIVE -> false
else -> {
log(targetName)
false
}
}
}
@Suppress("MaxLineLength", "ComplexMethod", "NestedBlockDepth")
override fun process(roundEnvironment: RoundEnvironment) {
val isCommon = this.configuration.kotlinSourceRoots.find { !it.isCommon } == null
if (isCommon) {
roundEnvironment.getElementsAnnotatedWith(KVService::class.java.name).forEach {
if (it is Element.ClassElement && it.classDescriptor.name.asString().startsWith("I")
&& it.classDescriptor.name.asString().endsWith("Service")
) {
tailrec fun findBuildFolder(path: String): String {
val preSrcDir = path.substringBeforeLast("/src")
return if (path == preSrcDir || File(preSrcDir, "build").isDirectory) {
"$preSrcDir/build"
} else {
findBuildFolder(preSrcDir)
}
}
val cl = it.classDescriptor
val buildFolder = cl.canonicalFilePath()?.let { path -> findBuildFolder(path) }
val genRootDir = File(buildFolder, "generated-src").apply {
mkdirs()
}
val packageName = cl.containingDeclaration.fqNameSafe.asString()
val iName = cl.name.asString()
val baseName = iName.drop(1)
val commonCode = StringBuilder().apply {
appendln("//")
appendln("// GENERATED by KVision")
appendln("//")
appendln("package $packageName")
appendln()
appendln("import kotlinx.coroutines.CoroutineStart")
appendln("import kotlinx.coroutines.GlobalScope")
appendln("import kotlinx.coroutines.launch")
appendln("import pl.treksoft.kvision.remote.HttpMethod")
appendln("import pl.treksoft.kvision.remote.KVServiceManager")
appendln()
appendln("expect class $baseName : $iName")
appendln()
appendln("object ${baseName}Manager : KVServiceManager<$baseName>($baseName::class) {")
appendln(" init {")
appendln(" GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {")
cl.methods().forEach {
val params = it.allParameters.drop(1)
val wsMethod =
if (params.size == 2)
params.first().type.toString().startsWith("ReceiveChannel")
else false
val kvBinding =
it.annotations.findAnnotation(FqName("pl.treksoft.kvision.annotations.KVBinding"))
val kvBindingMethod =
it.annotations.findAnnotation(FqName("pl.treksoft.kvision.annotations.KVBindingMethod"))
val kvBindingRoute =
it.annotations.findAnnotation(FqName("pl.treksoft.kvision.annotations.KVBindingRoute"))
val (method, route) = if (kvBinding != null) {
val method = kvBinding.allValueArguments[Name.identifier("method")].toString()
val route = kvBinding.allValueArguments[Name.identifier("route")].toString()
"Http$method" to route
} else if (kvBindingMethod != null) {
val method = kvBindingMethod.allValueArguments[Name.identifier("method")].toString()
"Http$method" to null
} else if (kvBindingRoute != null) {
val route = kvBindingRoute.allValueArguments[Name.identifier("route")].toString()
"HttpMethod.POST" to route
} else {
"HttpMethod.POST" to null
}
when {
it.returnType.toString().startsWith("RemoteData") ->
appendln(" bindTabulatorRemote($iName::${it.name}, $route)")
wsMethod -> if (route == null) {
appendln(" bind($iName::${it.name}, null as String?)")
} else {
appendln(" bind($iName::${it.name}, $route)")
}
else -> appendln(" bind($iName::${it.name}, $method, $route)")
}
}
appendln(" }")
appendln(" }")
appendln("}")
}.toString()
val commonDestinationDir = File(
genRootDir,
"common" + File.separator + packageName.replace('.', File.separatorChar)
).apply {
mkdirs()
}
val commonFile = File(commonDestinationDir, "${baseName}Manager.kt")
if (commonFile.exists()) {
val content = commonFile.readText()
if (content != commonCode) {
commonFile.writeText(commonCode)
}
} else {
commonFile.writeText(commonCode)
}
val frontendCode = StringBuilder().apply {
appendln("//")
appendln("// GENERATED by KVision")
appendln("//")
appendln("package $packageName")
appendln()
appendln("import pl.treksoft.jquery.JQueryAjaxSettings")
appendln("import pl.treksoft.jquery.JQueryXHR")
appendln("import pl.treksoft.kvision.remote.KVRemoteAgent")
getTypes(cl.methods()).sorted().forEach {
appendln("import $it")
}
appendln()
appendln("actual class $baseName(beforeSend: ((JQueryXHR, JQueryAjaxSettings) -> Boolean)? = null) : $iName, KVRemoteAgent<$baseName>(${baseName}Manager, beforeSend) {")
cl.methods().forEach {
val name = it.name
val params = it.allParameters.drop(1)
val wsMethod =
if (params.size == 2)
params.first().type.toString().startsWith("ReceiveChannel")
else false
if (!wsMethod) {
if (params.isNotEmpty()) {
when {
it.returnType.toString().startsWith("RemoteData") -> appendln(
" override suspend fun $name(${getParameterList(
params
)}) = ${it.returnType.toString()}()"
)
else -> appendln(
" override suspend fun $name(${getParameterList(params)}) = call($iName::$name, ${getParameterNames(
params
)})"
)
}
} else {
appendln(" override suspend fun $name() = call($iName::$name)")
}
} else {
appendln(" override suspend fun $name(${getParameterList(params)}) {}")
val type1 = params[0].type.toString().replace("ReceiveChannel", "SendChannel")
val type2 = params[1].type.toString().replace("SendChannel", "ReceiveChannel")
appendln(" suspend fun $name(handler: suspend ($type1, $type2) -> Unit) = webSocket($iName::$name, handler)")
}
}
appendln("}")
}.toString()
val frontendDestinationDir = File(
genRootDir,
"frontend" + File.separator + packageName.replace('.', File.separatorChar)
).apply {
mkdirs()
}
val frontendFile = File(frontendDestinationDir, "${baseName}.kt")
if (frontendFile.exists()) {
val content = frontendFile.readText()
if (content != frontendCode) {
frontendFile.writeText(frontendCode)
}
} else {
frontendFile.writeText(frontendCode)
}
}
}
}
}
private fun getParameterList(params: List<ParameterDescriptor>): String {
return params.joinToString(", ") {
"${it.name.asString()}: ${it.type}"
}
}
private fun getParameterNames(params: List<ParameterDescriptor>): String {
return params.joinToString(", ") {
it.name.asString()
}
}
private fun getTypes(type: KotlinType): Set<String> {
return if (type.arguments.isNotEmpty()) {
(type.arguments.flatMap { getTypes(it.type) } + type.getJetTypeFqName(false)).toSet()
} else {
setOf(type.getJetTypeFqName(false))
}
}
private fun getTypes(methods: Collection<CallableMemberDescriptor>): Set<String> {
return methods.flatMap { m ->
m.allParameters.drop(1).flatMap { p ->
getTypes(p.type)
}.toSet() + (m.returnType?.let { getTypes(it) } ?: setOf())
}.filterNot {
it.startsWith("kotlin.collections.") || it.startsWith("kotlin.")
}.toSet()
}
override fun getSupportedAnnotationTypes(): Set<String> = setOf(
KVService::class.java.name
)
}
|