From f72112bed5fea78be4e73b58fe8bea449d090140 Mon Sep 17 00:00:00 2001 From: jeffboutotte Date: Fri, 19 Jul 2019 13:21:02 -0400 Subject: [PATCH] Get Kotlin 1.2.x and 1.3.x to compile (#26) --- .gitignore | 1 + build.sbt | 2 +- src/main/scala/KotlinCompile.scala | 23 +++++++++++++++---- src/main/scala/KotlinPlugin.scala | 6 ++--- .../basic-android/project/build.properties | 1 - .../project/build.properties | 1 - .../kotlin/kotlin-1.1-compat/build.sbt | 10 ++++++++ .../kotlin-1.1-compat/project/plugins.sbt | 1 + .../src/main/kotlin/simple.kt | 10 ++++++++ src/sbt-test/kotlin/kotlin-1.1-compat/test | 3 +++ .../kotlin/kotlin-1.2-compat/build.sbt | 10 ++++++++ .../kotlin-1.2-compat/project/plugins.sbt | 1 + .../src/main/kotlin/simple.kt | 13 +++++++++++ src/sbt-test/kotlin/kotlin-1.2-compat/test | 3 +++ .../kotlin/kotlin-1.3-compat/build.sbt | 10 ++++++++ .../kotlin-1.3-compat/project/plugins.sbt | 1 + .../src/main/kotlin/simple.kt | 7 ++++++ src/sbt-test/kotlin/kotlin-1.3-compat/test | 3 +++ 18 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 .gitignore delete mode 100644 src/sbt-test/kotlin/basic-android/project/build.properties delete mode 100644 src/sbt-test/kotlin/extensions-android/project/build.properties create mode 100644 src/sbt-test/kotlin/kotlin-1.1-compat/build.sbt create mode 100644 src/sbt-test/kotlin/kotlin-1.1-compat/project/plugins.sbt create mode 100644 src/sbt-test/kotlin/kotlin-1.1-compat/src/main/kotlin/simple.kt create mode 100644 src/sbt-test/kotlin/kotlin-1.1-compat/test create mode 100644 src/sbt-test/kotlin/kotlin-1.2-compat/build.sbt create mode 100644 src/sbt-test/kotlin/kotlin-1.2-compat/project/plugins.sbt create mode 100644 src/sbt-test/kotlin/kotlin-1.2-compat/src/main/kotlin/simple.kt create mode 100644 src/sbt-test/kotlin/kotlin-1.2-compat/test create mode 100644 src/sbt-test/kotlin/kotlin-1.3-compat/build.sbt create mode 100644 src/sbt-test/kotlin/kotlin-1.3-compat/project/plugins.sbt create mode 100644 src/sbt-test/kotlin/kotlin-1.3-compat/src/main/kotlin/simple.kt create mode 100644 src/sbt-test/kotlin/kotlin-1.3-compat/test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/build.sbt b/build.sbt index a312242..86fe4ec 100644 --- a/build.sbt +++ b/build.sbt @@ -5,7 +5,7 @@ name := "kotlin-plugin" organization := "com.hanhuy.sbt" -version := "1.0.9" +version := "1.0.10-SNAPSHOT" scalacOptions ++= Seq("-deprecation","-Xlint","-feature") diff --git a/src/main/scala/KotlinCompile.scala b/src/main/scala/KotlinCompile.scala index 9cd4b4e..6046a9d 100644 --- a/src/main/scala/KotlinCompile.scala +++ b/src/main/scala/KotlinCompile.scala @@ -161,17 +161,32 @@ case class KotlinStub(s: TaskStreams, kref: KotlinReflection) { def compilerArgs = { import language.dynamics new Dynamic { + def withFirstUpper(string: String): String = string.head.toUpper + string.tail + def getterName(field: String) = s"get${withFirstUpper(field)}" + def setterName(field: String) = s"set${withFirstUpper(field)}" + def selectDynamic[A](field: String): A = { - val f = compilerArgsClass.getField(field) - f.get(instance).asInstanceOf[A] + val methodName = getterName(field) + val getterOpt = compilerArgsClass.getMethods.find(_.getName == methodName) + getterOpt match { + case Some(getter) => getter.invoke(instance).asInstanceOf[A] + case None => compilerArgsClass.getField(field).get(instance).asInstanceOf[A] + } } + def updateDynamic(field: String)(value: Any): Unit = { - val f = compilerArgsClass.getField(field) - f.set(instance, value) + val methodName = setterName(field) + val setterOpt = compilerArgsClass.getMethods.find(_.getName == methodName) + setterOpt match { + case Some(setter) => setter.invoke(instance, value.asInstanceOf[Object]) + case None => compilerArgsClass.getField(field).set(instance, value) + } } + val instance = compilerArgsClass.newInstance().asInstanceOf[AnyRef] } } + def compile(args: AnyRef): Unit = { val compiler = compilerClass.newInstance() val result = compilerExec.invoke(compiler, diff --git a/src/main/scala/KotlinPlugin.scala b/src/main/scala/KotlinPlugin.scala index 0554ac5..826f56c 100644 --- a/src/main/scala/KotlinPlugin.scala +++ b/src/main/scala/KotlinPlugin.scala @@ -42,7 +42,7 @@ object KotlinPlugin extends AutoPlugin { } } }, - kotlinVersion := "1.1.4-3", + kotlinVersion := "1.3.41", kotlincOptions := Nil, kotlincPluginOptions := Nil, watchSources ++= { @@ -59,8 +59,8 @@ object KotlinPlugin extends AutoPlugin { // public to allow kotlin compile in other configs beyond Compile and Test val kotlinCompileSettings = List( unmanagedSourceDirectories += kotlinSource.value, - kotlincOptions := (kotlincOptions in This).value, - kotlincPluginOptions := (kotlincPluginOptions in This).value, + kotlincOptions := kotlincOptions.value, + kotlincPluginOptions := kotlincPluginOptions.value, kotlinCompile := Def.task { KotlinCompile.compile(kotlincOptions.value, sourceDirectories.value, kotlincPluginOptions.value, diff --git a/src/sbt-test/kotlin/basic-android/project/build.properties b/src/sbt-test/kotlin/basic-android/project/build.properties deleted file mode 100644 index a6e117b..0000000 --- a/src/sbt-test/kotlin/basic-android/project/build.properties +++ /dev/null @@ -1 +0,0 @@ -sbt.version=0.13.8 diff --git a/src/sbt-test/kotlin/extensions-android/project/build.properties b/src/sbt-test/kotlin/extensions-android/project/build.properties deleted file mode 100644 index a6e117b..0000000 --- a/src/sbt-test/kotlin/extensions-android/project/build.properties +++ /dev/null @@ -1 +0,0 @@ -sbt.version=0.13.8 diff --git a/src/sbt-test/kotlin/kotlin-1.1-compat/build.sbt b/src/sbt-test/kotlin/kotlin-1.1-compat/build.sbt new file mode 100644 index 0000000..1165e95 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.1-compat/build.sbt @@ -0,0 +1,10 @@ +kotlinLib("stdlib") + +kotlinVersion := "1.1.4-3" + +val listClasses = taskKey[Unit]("listClasses") + +listClasses := { + val classes = (classDirectory in Compile).value.listFiles() + streams.value.log.info("classes: " + classes) +} diff --git a/src/sbt-test/kotlin/kotlin-1.1-compat/project/plugins.sbt b/src/sbt-test/kotlin/kotlin-1.1-compat/project/plugins.sbt new file mode 100644 index 0000000..ff38770 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.1-compat/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % sys.props("plugin.version")) diff --git a/src/sbt-test/kotlin/kotlin-1.1-compat/src/main/kotlin/simple.kt b/src/sbt-test/kotlin/kotlin-1.1-compat/src/main/kotlin/simple.kt new file mode 100644 index 0000000..161133a --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.1-compat/src/main/kotlin/simple.kt @@ -0,0 +1,10 @@ +package demo +fun main(args: Array) { + val map = mapOf("key" to 42) + val emptyMap = map - "key" + + val list1 = listOf("a", "b") + val list2 = listOf("x", "y", "z") + val minSize = minOf(list1.size, list2.size) + val longestList = maxOf(list1, list2, compareBy { it.size }) +} diff --git a/src/sbt-test/kotlin/kotlin-1.1-compat/test b/src/sbt-test/kotlin/kotlin-1.1-compat/test new file mode 100644 index 0000000..89102f4 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.1-compat/test @@ -0,0 +1,3 @@ +> compile +> listClasses +$ exists target/scala-2.10/classes/demo/SimpleKt.class diff --git a/src/sbt-test/kotlin/kotlin-1.2-compat/build.sbt b/src/sbt-test/kotlin/kotlin-1.2-compat/build.sbt new file mode 100644 index 0000000..8a787d1 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.2-compat/build.sbt @@ -0,0 +1,10 @@ +kotlinLib("stdlib") + +kotlinVersion := "1.2.71" + +val listClasses = taskKey[Unit]("listClasses") + +listClasses := { + val classes = (classDirectory in Compile).value.listFiles() + streams.value.log.info("classes: " + classes) +} diff --git a/src/sbt-test/kotlin/kotlin-1.2-compat/project/plugins.sbt b/src/sbt-test/kotlin/kotlin-1.2-compat/project/plugins.sbt new file mode 100644 index 0000000..ff38770 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.2-compat/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % sys.props("plugin.version")) diff --git a/src/sbt-test/kotlin/kotlin-1.2-compat/src/main/kotlin/simple.kt b/src/sbt-test/kotlin/kotlin-1.2-compat/src/main/kotlin/simple.kt new file mode 100644 index 0000000..efb90c1 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.2-compat/src/main/kotlin/simple.kt @@ -0,0 +1,13 @@ +package demo +fun main(args: Array) { + // Test some Kotlin 1.2 features + val items = (1..9).map { it * it } + + val chunkedIntoLists = items.chunked(4) + val points3d = items.chunked(3) { (x, y, z) -> Triple(x, y, z) } + val windowed = items.windowed(4) + val slidingAverage = items.windowed(4) { it.average() } + val pairwiseDifferences = items.zipWithNext { a, b -> b - a } + + println("Hello, world!") +} diff --git a/src/sbt-test/kotlin/kotlin-1.2-compat/test b/src/sbt-test/kotlin/kotlin-1.2-compat/test new file mode 100644 index 0000000..89102f4 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.2-compat/test @@ -0,0 +1,3 @@ +> compile +> listClasses +$ exists target/scala-2.10/classes/demo/SimpleKt.class diff --git a/src/sbt-test/kotlin/kotlin-1.3-compat/build.sbt b/src/sbt-test/kotlin/kotlin-1.3-compat/build.sbt new file mode 100644 index 0000000..90e16c1 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.3-compat/build.sbt @@ -0,0 +1,10 @@ +kotlinLib("stdlib") + +kotlinVersion := "1.3.41" + +val listClasses = taskKey[Unit]("listClasses") + +listClasses := { + val classes = (classDirectory in Compile).value.listFiles() + streams.value.log.info("classes: " + classes) +} diff --git a/src/sbt-test/kotlin/kotlin-1.3-compat/project/plugins.sbt b/src/sbt-test/kotlin/kotlin-1.3-compat/project/plugins.sbt new file mode 100644 index 0000000..ff38770 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.3-compat/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % sys.props("plugin.version")) diff --git a/src/sbt-test/kotlin/kotlin-1.3-compat/src/main/kotlin/simple.kt b/src/sbt-test/kotlin/kotlin-1.3-compat/src/main/kotlin/simple.kt new file mode 100644 index 0000000..59ad996 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.3-compat/src/main/kotlin/simple.kt @@ -0,0 +1,7 @@ +package demo +fun main(args: Array) { + // Test some Kotlin 1.3 features + val keys = 'a'..'f' + val map = keys.associateWith { it.toString().repeat(5).capitalize() } + map.forEach { println(it) } +} diff --git a/src/sbt-test/kotlin/kotlin-1.3-compat/test b/src/sbt-test/kotlin/kotlin-1.3-compat/test new file mode 100644 index 0000000..89102f4 --- /dev/null +++ b/src/sbt-test/kotlin/kotlin-1.3-compat/test @@ -0,0 +1,3 @@ +> compile +> listClasses +$ exists target/scala-2.10/classes/demo/SimpleKt.class