Forming the generator slowly
This commit is contained in:
parent
3c77cc0698
commit
53325cdba6
@ -28,6 +28,8 @@ object Versions {
|
|||||||
val lazySodium = "4.2.6"
|
val lazySodium = "4.2.6"
|
||||||
val jna = "5.5.0"
|
val jna = "5.5.0"
|
||||||
|
|
||||||
|
val kotlinPoet = "1.6.0"
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +77,8 @@ object Deps {
|
|||||||
val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinSerialization}"
|
val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinSerialization}"
|
||||||
val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}"
|
val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}"
|
||||||
|
|
||||||
|
val kotlinPoet = "com.squareup:kotlinpoet:${Versions.kotlinPoet}"
|
||||||
|
|
||||||
object Delegated {
|
object Delegated {
|
||||||
val lazysodium = "com.goterl.lazycode:lazysodium-java:${Versions.lazySodium}"
|
val lazysodium = "com.goterl.lazycode:lazysodium-java:${Versions.lazySodium}"
|
||||||
val jna = "net.java.dev.jna:jna:${Versions.jna}"
|
val jna = "net.java.dev.jna:jna:${Versions.jna}"
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
plugins {
|
|
||||||
id 'org.jetbrains.kotlin.jvm'
|
|
||||||
}
|
|
||||||
|
|
||||||
group 'com.ionspin.kotlin.crypto'
|
|
||||||
version '0.0.1'
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
|
|
||||||
}
|
|
||||||
|
|
||||||
compileKotlin {
|
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
|
||||||
}
|
|
||||||
compileTestKotlin {
|
|
||||||
kotlinOptions.jvmTarget = "1.8"
|
|
||||||
}
|
|
37
kotlin-multiplatform-libsodium-generator/build.gradle.kts
Normal file
37
kotlin-multiplatform-libsodium-generator/build.gradle.kts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
plugins {
|
||||||
|
kotlin
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "com.ionspin.kotlin.crypto"
|
||||||
|
version = "0.0.1"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
google()
|
||||||
|
maven ("https://kotlin.bintray.com/kotlinx")
|
||||||
|
maven ("https://dl.bintray.com/kotlin/kotlin-eap")
|
||||||
|
maven ("https://dl.bintray.com/kotlin/kotlin-dev")
|
||||||
|
jcenter()
|
||||||
|
maven {
|
||||||
|
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation (kotlin(Deps.Jvm.stdLib))
|
||||||
|
implementation("com.squareup:kotlinpoet:1.6.0")
|
||||||
|
testImplementation(kotlin(Deps.Jvm.test))
|
||||||
|
testImplementation(kotlin(Deps.Jvm.testJUnit))
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
|
||||||
|
kotlinOptions.freeCompilerArgs += listOf(
|
||||||
|
"-Xuse-experimental=kotlin.ExperimentalUnsignedTypes",
|
||||||
|
""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,129 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.generator.libsodium.definitions
|
||||||
|
|
||||||
|
import com.squareup.kotlinpoet.TypeName
|
||||||
|
import com.squareup.kotlinpoet.asTypeName
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 28-Jul-2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
class KotlinFileDefinition(
|
||||||
|
val name: String,
|
||||||
|
val commonClassList: MutableList<ClassDefinition> = mutableListOf()
|
||||||
|
) {
|
||||||
|
operator fun ClassDefinition.unaryPlus() {
|
||||||
|
commonClassList.add(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClassDefinition(
|
||||||
|
val name: String,
|
||||||
|
val innerClasses: MutableList<InnerClassDefinition> = mutableListOf(),
|
||||||
|
val methods: MutableList<FunctionDefinition> = mutableListOf()
|
||||||
|
) {
|
||||||
|
operator fun InnerClassDefinition.unaryPlus() {
|
||||||
|
innerClasses.add(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun FunctionDefinition.unaryPlus() {
|
||||||
|
methods.add(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class InnerClassDefinition (
|
||||||
|
val name: String,
|
||||||
|
val javaName: String,
|
||||||
|
val jsName: String,
|
||||||
|
val nativeName: String,
|
||||||
|
val functions: MutableList<FunctionDefinition> = mutableListOf()
|
||||||
|
)
|
||||||
|
|
||||||
|
class FunctionDefinition(
|
||||||
|
val name: String,
|
||||||
|
val javaName: String,
|
||||||
|
val jsName: String,
|
||||||
|
val nativeName: String,
|
||||||
|
val parameterList: MutableList<ParameterDefinition> = mutableListOf(),
|
||||||
|
val returnType: TypeDefinition
|
||||||
|
) {
|
||||||
|
operator fun ParameterDefinition.unaryPlus() {
|
||||||
|
parameterList.add(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ParameterDefinition(
|
||||||
|
val parameterName: String,
|
||||||
|
val parameterType: TypeDefinition
|
||||||
|
)
|
||||||
|
|
||||||
|
enum class TypeDefinition(val typeName: TypeName) {
|
||||||
|
ARRAY_OF_UBYTES(UByteArray::class.asTypeName()),
|
||||||
|
LONG(Long::class.asTypeName()),
|
||||||
|
INT(Int::class.asTypeName()),
|
||||||
|
STRING(String::class.asTypeName())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fileDef(name: String, body: KotlinFileDefinition.() -> Unit) : KotlinFileDefinition {
|
||||||
|
val commonKotlinFileInstance = KotlinFileDefinition(name)
|
||||||
|
commonKotlinFileInstance.body()
|
||||||
|
return commonKotlinFileInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun classDef(name: String, body: ClassDefinition.() -> Unit): ClassDefinition {
|
||||||
|
val commonClass = ClassDefinition(name)
|
||||||
|
commonClass.body()
|
||||||
|
return commonClass
|
||||||
|
}
|
||||||
|
|
||||||
|
fun innerClassDef(
|
||||||
|
name: String,
|
||||||
|
javaName: String,
|
||||||
|
jsName: String,
|
||||||
|
nativeName: String,
|
||||||
|
body: InnerClassDefinition.() -> Unit = {}
|
||||||
|
) : InnerClassDefinition {
|
||||||
|
val genClass = InnerClassDefinition(
|
||||||
|
name,
|
||||||
|
javaName,
|
||||||
|
jsName,
|
||||||
|
nativeName
|
||||||
|
)
|
||||||
|
genClass.body()
|
||||||
|
return genClass
|
||||||
|
}
|
||||||
|
|
||||||
|
fun funcDef(
|
||||||
|
name: String,
|
||||||
|
javaName: String,
|
||||||
|
jsName: String,
|
||||||
|
nativeName: String,
|
||||||
|
returnType: TypeDefinition, body: FunctionDefinition.() -> Unit
|
||||||
|
): FunctionDefinition {
|
||||||
|
val function = FunctionDefinition(name, javaName, jsName, nativeName, returnType = returnType)
|
||||||
|
function.body()
|
||||||
|
return function
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
object LibSodiumDefinitions {
|
||||||
|
val testKotlinFile = fileDef("Test.kt") {
|
||||||
|
+classDef("Hashing") {
|
||||||
|
+innerClassDef(
|
||||||
|
"Sha256State",
|
||||||
|
"Hash.State256",
|
||||||
|
"Sha256State",
|
||||||
|
"crypto_hash_sha256_state"
|
||||||
|
)
|
||||||
|
+funcDef(
|
||||||
|
"test", "test", "test", "test", TypeDefinition.ARRAY_OF_UBYTES
|
||||||
|
) {
|
||||||
|
+ParameterDefinition("first", TypeDefinition.ARRAY_OF_UBYTES)
|
||||||
|
+ParameterDefinition("second", TypeDefinition.LONG)
|
||||||
|
+ParameterDefinition("third", TypeDefinition.STRING)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ClassDefinition
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.KotlinFileDefinition
|
||||||
|
import com.squareup.kotlinpoet.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 31-Jul-2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
enum class MultiplatformModifier(val modifierList : List<KModifier>) {
|
||||||
|
EXPECT(listOf(KModifier.EXPECT)),
|
||||||
|
ACTUAL(listOf(KModifier.ACTUAL)),
|
||||||
|
NONE(listOf())
|
||||||
|
}
|
||||||
|
|
||||||
|
object CommonLibsodiumGenerator {
|
||||||
|
|
||||||
|
fun createCommonFile(packageName: String, fileDefinition: KotlinFileDefinition): FileSpec {
|
||||||
|
val fileBuilder = FileSpec.builder(packageName, fileDefinition.name)
|
||||||
|
for (commonClassDefinition in fileDefinition.commonClassList) {
|
||||||
|
val commonClassSpec =
|
||||||
|
createClass(commonClassDefinition, MultiplatformModifier.EXPECT, ::createCommonMethodSpec)
|
||||||
|
fileBuilder.addType(commonClassSpec)
|
||||||
|
}
|
||||||
|
val file = fileBuilder.build()
|
||||||
|
file.writeTo(System.out)
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createCommonMethodSpec(methodDefinition: FunctionDefinition): FunSpec {
|
||||||
|
val methodBuilder = FunSpec.builder(methodDefinition.name)
|
||||||
|
for (paramDefinition in methodDefinition.parameterList) {
|
||||||
|
val parameterSpec =
|
||||||
|
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
|
||||||
|
methodBuilder.addParameter(parameterSpec.build())
|
||||||
|
}
|
||||||
|
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||||
|
return methodBuilder.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.LibSodiumDefinitions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 31-Jul-2020
|
||||||
|
*/
|
||||||
|
object Coordinator {
|
||||||
|
|
||||||
|
fun run(packageName: String) {
|
||||||
|
CommonLibsodiumGenerator.createCommonFile(packageName, LibSodiumDefinitions.testKotlinFile)
|
||||||
|
JvmLibsodiumGenerator.createJvmFile(packageName, LibSodiumDefinitions.testKotlinFile)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.KotlinFileDefinition
|
||||||
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
|
import com.squareup.kotlinpoet.FunSpec
|
||||||
|
import com.squareup.kotlinpoet.ParameterSpec
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 31-Jul-2020
|
||||||
|
*/
|
||||||
|
object JvmLibsodiumGenerator {
|
||||||
|
|
||||||
|
|
||||||
|
fun createJvmFile(packageName: String, fileDefinition: KotlinFileDefinition) : FileSpec {
|
||||||
|
val fileBuilder = FileSpec.builder(packageName, fileDefinition.name)
|
||||||
|
for (commonClassDefinition in fileDefinition.commonClassList) {
|
||||||
|
val commonClassSpec = createClass(commonClassDefinition, MultiplatformModifier.ACTUAL, ::createJvmFunctionImplementation)
|
||||||
|
fileBuilder.addType(commonClassSpec)
|
||||||
|
}
|
||||||
|
val file = fileBuilder.build()
|
||||||
|
file.writeTo(System.out)
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createJvmFunctionImplementation(methodDefinition: FunctionDefinition) : FunSpec {
|
||||||
|
val methodBuilder = FunSpec.builder(methodDefinition.name)
|
||||||
|
for (paramDefinition in methodDefinition.parameterList) {
|
||||||
|
val parameterSpec =
|
||||||
|
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
|
||||||
|
methodBuilder.addParameter(parameterSpec.build())
|
||||||
|
}
|
||||||
|
methodBuilder.addStatement("val test1 = ${methodDefinition.javaName}")
|
||||||
|
methodBuilder.returns(methodDefinition.returnType.typeName)
|
||||||
|
return methodBuilder.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.generator.libsodium.generator
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.ClassDefinition
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.KotlinFileDefinition
|
||||||
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
|
import com.squareup.kotlinpoet.FunSpec
|
||||||
|
import com.squareup.kotlinpoet.ParameterSpec
|
||||||
|
import com.squareup.kotlinpoet.TypeSpec
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 31-Jul-2020
|
||||||
|
*/
|
||||||
|
fun createClass(classDefinition: ClassDefinition, multiplatformModifier: MultiplatformModifier, methodCreator : (FunctionDefinition) -> FunSpec) : TypeSpec {
|
||||||
|
val commonClassBuilder = TypeSpec.classBuilder(classDefinition.name)
|
||||||
|
commonClassBuilder.modifiers += multiplatformModifier.modifierList
|
||||||
|
for (innerClassDefinition in classDefinition.innerClasses) {
|
||||||
|
val innerClassBuilder = TypeSpec.classBuilder(innerClassDefinition.name)
|
||||||
|
innerClassBuilder.modifiers += multiplatformModifier.modifierList
|
||||||
|
commonClassBuilder.addType(innerClassBuilder.build())
|
||||||
|
}
|
||||||
|
for (methodDefinition in classDefinition.methods) {
|
||||||
|
commonClassBuilder.addFunction(methodCreator(methodDefinition))
|
||||||
|
}
|
||||||
|
return commonClassBuilder.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.ionspin.kotlin.crypto.generator
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.generator.CommonLibsodiumGenerator
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.LibSodiumDefinitions
|
||||||
|
import com.ionspin.kotlin.crypto.generator.libsodium.generator.Coordinator
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ugljesa Jovanovic
|
||||||
|
* ugljesa.jovanovic@ionspin.com
|
||||||
|
* on 31-Jul-2020
|
||||||
|
*/
|
||||||
|
class DebugTest {
|
||||||
|
val packageName = "debug.test"
|
||||||
|
@Test
|
||||||
|
fun debugTest() {
|
||||||
|
Coordinator.run(packageName)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user