Skip to content

Commit

Permalink
Update to sbt 1 (pfn#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffboutotte authored and pfn committed Jul 19, 2019
1 parent f72112b commit e26675c
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 63 deletions.
36 changes: 17 additions & 19 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
//import ScriptedPlugin._
import bintray.Keys._

name := "kotlin-plugin"

organization := "com.hanhuy.sbt"

version := "1.0.10-SNAPSHOT"
version := "2.0.0-SNAPSHOT"

scalacOptions ++= Seq("-deprecation","-Xlint","-feature")
/*
libraryDependencies ++= Seq(
"com.hanhuy.sbt" %% "bintray-update-checker" % "0.2"
)
*/

libraryDependencies ++=
"com.hanhuy.sbt" %% "bintray-update-checker" % "0.2" ::
Nil
libraryDependencies ++= Seq(
"io.argonaut" %% "argonaut" % "6.2",
"org.scalaz" %% "scalaz-core" % "7.2.28"
)

sbtPlugin := true

// build info plugin

buildInfoSettings

sourceGenerators in Compile += buildInfo
enablePlugins(BuildInfoPlugin, SbtPlugin)

buildInfoPackage := "kotlin"

// bintray
bintrayPublishSettings

repository in bintray := "sbt-plugins"
bintrayRepository := "sbt-plugins"

publishMavenStyle := false

licenses += ("MIT", url("http://opensource.org/licenses/MIT"))

bintrayOrganization in bintray := None
bintrayOrganization := None

// scripted
scriptedSettings

scriptedLaunchOpts ++= "-Xmx1024m" ::
"-Dplugin.version=" + version.value ::
Nil
scriptedLaunchOpts ++= Seq(
"-Xmx1024m",
"-Dplugin.version=" + version.value
)
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.16
sbt.version=1.2.8
7 changes: 3 additions & 4 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
libraryDependencies += "org.scala-sbt" % "scripted-plugin" % sbtVersion.value
libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value

addSbtPlugin("me.lessis" % "bintray-sbt" % "0.1.2")

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.2.5")
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
2 changes: 1 addition & 1 deletion src/main/scala/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object Keys {
val kotlinSource = SettingKey[File]("kotlin-source", "kotlin source directory")
val kotlinVersion = SettingKey[String]("kotlin-version",
"version of kotlin to use for building")
val kotlincOptions = TaskKey[Seq[String]]("kotlinc-options",
val kotlincOptions = SettingKey[Seq[String]]("kotlinc-options",
"options to pass to the kotlin compiler")

def kotlinLib(name: String) = sbt.Keys.libraryDependencies +=
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/KotlinCompile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import java.util.jar.JarEntry

import sbt.Keys.{Classpath, TaskStreams}
import sbt._
import sbt.classpath.ClasspathUtilities
import sbt.io._
import sbt.internal.inc.classpath.ClasspathUtilities

import collection.JavaConverters._
import language.existentials
import scala.util.Try

/**
Expand Down Expand Up @@ -89,7 +89,7 @@ object KotlinReflection {
"org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments")
val clitool = cl.loadClass(
"org.jetbrains.kotlin.cli.common.CLITool")
clitool.getMethod("exec",
clitool.getMethod("exec",
messageCollectorClass, servicesClass, commonToolArguments)
}

Expand Down
1 change: 0 additions & 1 deletion src/main/scala/KotlinPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kotlin

import Keys._
import com.hanhuy.sbt.bintray.UpdateChecker
import sbt._
import sbt.Keys._
import sbt.plugins.JvmPlugin
Expand Down
48 changes: 34 additions & 14 deletions src/main/scala/KotlinTest.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package sbt

import sbt.Keys._
import sbt.classfile.Analyze
import sbt.classpath.ClasspathUtilities
import sbt.inc.{Analysis, IncOptions, IncrementalCompile}
import xsbti.compile.SingleOutput
import sbt.internal.inc.classfile.Analyze
import sbt.internal.inc.classpath.ClasspathUtilities
import sbt.internal.inc._
import xsbti.compile._

object KotlinTest {
private object EmptyLookup extends Lookup {
def changedClasspathHash: Option[Vector[FileHash]] = None

def analyses: Vector[CompileAnalysis] = Vector.empty

def lookupOnClasspath(binaryClassName: String): Option[File] = None

def lookupAnalysis(binaryClassName: String): Option[CompileAnalysis] = None
def changedBinaries(previousAnalysis: xsbti.compile.CompileAnalysis): Option[Set[java.io.File]] = None
def changedSources(previousAnalysis: xsbti.compile.CompileAnalysis): Option[xsbti.compile.Changes[java.io.File]] = None
def removedProducts(previousAnalysis: xsbti.compile.CompileAnalysis): Option[Set[java.io.File]] = None
def shouldDoIncrementalCompilation(changedClasses: Set[String],analysis: xsbti.compile.CompileAnalysis): Boolean = true

def hashClasspath(x$1: Array[java.io.File]): java.util.Optional[Array[xsbti.compile.FileHash]] = java.util.Optional.empty()
}

val kotlinTests = Def.task {
val out = ((target in Test).value ** "scala-*").get.head / "test-classes"
val srcs = ((sourceDirectory in Test).value ** "*.kt").get.toList
Expand All @@ -17,24 +33,28 @@ object KotlinTest {
})
val log = streams.value.log
val a0 = IncrementalCompile(
srcs.toSet, s => None,
(fs, changs, callback) => {
def readAPI(source: File, classes: Seq[Class[_]]): Set[String] = {
val (api, inherits) = ClassToAPI.process(classes)
callback.api(source, api)
inherits.map(_.getName)
srcs.toSet,
EmptyLookup,
(fs, changs, callback, clsFileMgr) => {
def readAPI(source: File, classes: Seq[Class[_]]): Set[(String, String)] = {
val (apis, mainClasses, inherits) = ClassToAPI.process(classes)
apis.foreach(callback.api(source, _))
mainClasses.foreach(callback.mainClass(source, _))
inherits.map {
case (from, to) => (from.getName, to.getName)
}
}

Analyze(xs, srcs, log)(callback, loader, readAPI)
},
Analysis.Empty, f => None,
Analysis.Empty,
new SingleOutput {
def outputDirectory = out
def getOutputDirectory(): java.io.File = out
},
log,
IncOptions.Default)._2
incOptions.value)._2
val frameworks = (loadedTestFrameworks in Test).value.values.toList
log.info(s"Compiling ${srcs.length} Kotlin source to ${out}...")
log.info(s"Compiling ${srcs.length} Kotlin source to $out...")
Tests.discover(frameworks, a0, log)._1
}
}
50 changes: 50 additions & 0 deletions src/main/scala/UpdateChecker.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kotlin

import java.io.{InputStreamReader, BufferedReader, StringWriter}

import argonaut._, Argonaut._

import scala.concurrent.Future
import scala.util.{Failure, Success}

object UpdateChecker {
import scala.concurrent.ExecutionContext.Implicits.global
type Result = (Set[String],String)
type Callback[A] = Either[Throwable,Result] => A

def apply[A](user: String, repo: String, name: String)(result: Callback[A]): Unit = {
val bintray = new java.net.URL(
s"https://api.bintray.com/packages/$user/$repo/$name")
Future {
val uc = bintray.openConnection()
val in = new BufferedReader(new InputStreamReader(uc.getInputStream, "utf-8"))
try {
val sw = new StringWriter
val buf = Array.ofDim[Char](8192)
Stream.continually(in.read(buf, 0, 8192)) takeWhile (
_ != -1) foreach (sw.write(buf, 0, _))
sw.toString
} finally {
in.close()
}
} onComplete {
case Success(json) =>
val decoded = json.decode[PackageInfo]
val res: Either[Throwable, Result] = decoded match {
case Left(Left(str)) =>
Left(new IllegalArgumentException(str))
case Left(Right(cursorHistory)) =>
Left(new IllegalArgumentException(cursorHistory._1))
case Right(packageInfo) =>
Right(packageInfo.versions.toSet -> packageInfo.version)
}
result(res)
case Failure(t) => result(Left(t))
}
}

implicit def PackageInfoCodecJson: CodecJson[PackageInfo] = casecodec3(
PackageInfo.apply, PackageInfo.unapply)("name", "latest_version", "versions")

case class PackageInfo(name: String, version: String, versions: List[String])
}
4 changes: 2 additions & 2 deletions src/sbt-test/kotlin/basic-android/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
androidBuild
enablePlugins(AndroidApp)

kotlinClasspath(Compile, bootClasspath in Android)

kotlinLib("stdlib")
kotlinLib("stdlib")
2 changes: 1 addition & 1 deletion src/sbt-test/kotlin/basic-android/project/android.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("org.scala-android" % "sbt-android" % "1.6.15")
addSbtPlugin("org.scala-android" % "sbt-android" % "2.0.0-SNAPSHOT")
2 changes: 1 addition & 1 deletion src/sbt-test/kotlin/basic-android/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
> android:package
$ exists target/android/output/basic-android-debug.apk
> check-dex
> checkDex
9 changes: 5 additions & 4 deletions src/sbt-test/kotlin/basic-android/tests.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import android.Keys._
import sys.process._

val androidBuilder = SettingKey[Logger => com.android.builder.core.AndroidBuilder]("android-builder") in Android

TaskKey[Unit]("check-dex") := {
TaskKey[Unit]("checkDex") := {
val p = androidBuilder.value
val s = streams.value
val layout = (projectLayout in Android).value
Expand All @@ -11,12 +12,12 @@ TaskKey[Unit]("check-dex") := {
val dexdump = tools / "dexdump"
val lines = Seq(
dexdump.getAbsolutePath, "-i",
(layout.dex / "classes.dex").getAbsolutePath).lines
(layout.dex / "classes.dex").getAbsolutePath).lineStream
val hasKotlinClasses = lines map (_.trim) exists { l =>
l.startsWith("Class descriptor") && l.endsWith("'Lkotlin/Unit;'")
}
if (!hasKotlinClasses) {
lines filter (_.trim.startsWith("Class descriptor")) foreach (l => s.log.info(l))
error("Kotlin classes not found")
sys.error("Kotlin classes not found")
}
}
}
4 changes: 2 additions & 2 deletions src/sbt-test/kotlin/basic/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ kotlinLib("stdlib")
val listClasses = taskKey[Unit]("listClasses")

listClasses := {
val classes = (classDirectory in Compile).value.***.get
val classes = (classDirectory in Compile).value.listFiles()
streams.value.log.info("classes: " + classes)
}
}
2 changes: 1 addition & 1 deletion src/sbt-test/kotlin/basic/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
> compile
> listClasses
$ exists target/scala-2.10/classes/demo/SimpleKt.class
$ exists target/scala-2.12/classes/demo/SimpleKt.class
2 changes: 1 addition & 1 deletion src/sbt-test/kotlin/extensions-android/project/android.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("org.scala-android" % "sbt-android" % "1.7.2")
addSbtPlugin("org.scala-android" % "sbt-android" % "2.0.0-SNAPSHOT")
2 changes: 1 addition & 1 deletion src/sbt-test/kotlin/extensions-android/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
> android:package
$ exists target/android/output/extensions-android-debug.apk
> check-dex
> checkDex
9 changes: 5 additions & 4 deletions src/sbt-test/kotlin/extensions-android/tests.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import android.Keys._
import sys.process._

val androidBuilder = SettingKey[Logger => com.android.builder.core.AndroidBuilder]("android-builder") in Android

TaskKey[Unit]("check-dex") := {
TaskKey[Unit]("checkDex") := {
implicit val out = outputLayout.value
val p = androidBuilder.value
val s = streams.value
Expand All @@ -11,12 +12,12 @@ TaskKey[Unit]("check-dex") := {
val dexdump = tools / "dexdump"
val lines = Seq(
dexdump.getAbsolutePath, "-i",
(layout.dex / "classes.dex").getAbsolutePath).lines
(layout.dex / "classes.dex").getAbsolutePath).lineStream
val hasKotlinClasses = lines map (_.trim) exists { l =>
l.startsWith("Class descriptor") && l.endsWith("'Lkotlin/Unit;'")
}
if (!hasKotlinClasses) {
lines filter (_.trim.startsWith("Class descriptor")) foreach (l => s.log.info(l))
error("Kotlin classes not found")
sys.error("Kotlin classes not found")
}
}
}
3 changes: 3 additions & 0 deletions src/sbt-test/kotlin/mixed/src/main/scala/ScalaA.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package demo

class ScalaA extends KotlinA
4 changes: 2 additions & 2 deletions src/sbt-test/kotlin/mixed/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
> debug
> compile
> info
$ exists target/scala-2.10/classes/demo/SimpleKt.class
> check-classes
$ exists target/scala-2.12/classes/demo/SimpleKt.class
> checkClasses
2 changes: 1 addition & 1 deletion src/sbt-test/kotlin/mixed/tests.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TaskKey[Unit]("check-classes") := {
val classes = (classDirectory in Compile).value
val classList = (classes ** "*.class").get
if (classList.size != 5) {
if (classList.size != 6) {
throw new MessageOnlyException(s"Incorrect number of classes: ${classList.size} =>\n${classList.mkString("\n")}")
}
}

0 comments on commit e26675c

Please sign in to comment.