Skip to content

Commit

Permalink
Get Kotlin 1.2.x and 1.3.x to compile (pfn#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffboutotte authored and pfn committed Jul 19, 2019
1 parent 91f234b commit f72112b
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
23 changes: 19 additions & 4 deletions src/main/scala/KotlinCompile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/KotlinPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object KotlinPlugin extends AutoPlugin {
}
}
},
kotlinVersion := "1.1.4-3",
kotlinVersion := "1.3.41",
kotlincOptions := Nil,
kotlincPluginOptions := Nil,
watchSources ++= {
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/sbt-test/kotlin/basic-android/project/build.properties

This file was deleted.

This file was deleted.

10 changes: 10 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.1-compat/build.sbt
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions src/sbt-test/kotlin/kotlin-1.1-compat/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % sys.props("plugin.version"))
10 changes: 10 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.1-compat/src/main/kotlin/simple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package demo
fun main(args: Array<String>) {
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 })
}
3 changes: 3 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.1-compat/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> compile
> listClasses
$ exists target/scala-2.10/classes/demo/SimpleKt.class
10 changes: 10 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.2-compat/build.sbt
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions src/sbt-test/kotlin/kotlin-1.2-compat/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % sys.props("plugin.version"))
13 changes: 13 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.2-compat/src/main/kotlin/simple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package demo
fun main(args: Array<String>) {
// 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!")
}
3 changes: 3 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.2-compat/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> compile
> listClasses
$ exists target/scala-2.10/classes/demo/SimpleKt.class
10 changes: 10 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.3-compat/build.sbt
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions src/sbt-test/kotlin/kotlin-1.3-compat/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.hanhuy.sbt" % "kotlin-plugin" % sys.props("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package demo
fun main(args: Array<String>) {
// Test some Kotlin 1.3 features
val keys = 'a'..'f'
val map = keys.associateWith { it.toString().repeat(5).capitalize() }
map.forEach { println(it) }
}
3 changes: 3 additions & 0 deletions src/sbt-test/kotlin/kotlin-1.3-compat/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> compile
> listClasses
$ exists target/scala-2.10/classes/demo/SimpleKt.class

0 comments on commit f72112b

Please sign in to comment.