Skip to content

Commit

Permalink
feat: Add scala-cli script generator to verify jdk classes
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Jan 28, 2024
1 parent d55350e commit 7e83ab7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ lazy val stream = pekkoModule("stream")
.settings(AutomaticModuleName.settings("pekko.stream"))
.settings(OSGi.stream)
.settings(Protobuf.settings)
.settings(VerifyJDK9Classes.settings)
.enablePlugins(BoilerplatePlugin, Jdk9)

lazy val streamTestkit = pekkoModule("stream-testkit")
Expand Down
56 changes: 56 additions & 0 deletions project/VerifyJDK9Classes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@


object VerifyJDK9Classes {
import sbt.*
import sbt.Keys.*

lazy val settings: Seq[Setting[_]] = inConfig(Compile) {
Seq {
sourceGenerators += {
generateAndWriteScalaCLIScript(
target,
_ / "scala-cli" / "VerifyJDK9Classes.sc")
}
}
}

def generateAndWriteScalaCLIScript(dir: SettingKey[File], locate: File => File): Def.Initialize[Task[Seq[sbt.File]]] =
Def.task[Seq[File]] {
val script = generateScalaCLIScript(version.value)
val file = locate(dir.value)
val content = script.stripMargin.format(version.value)
if (!file.exists || IO.read(file) != content) IO.write(file, content)
//the generated file is not used.
Nil
}

private def generateScalaCLIScript(version: String): String =
s"""
|//> using repository m2Local
|//> using repository sonatype:snapshots
|//> using repository sonatype:releases
|//> using dep "org.apache.pekko::pekko-stream:${version}"
|////> using jvm 11
|object VerifyJDK9Classes {
| def main(args: Array[String]): Unit = {
| import org.apache.pekko.actor.ActorSystem
| import org.apache.pekko.stream.scaladsl.{ JavaFlowSupport, Source }
|
| import java.lang.System.exit
| import scala.concurrent.Await
| import scala.concurrent.duration.DurationInt
| implicit val system: ActorSystem = ActorSystem.create("test")
| val future = Source(1 to 3).runWith(
| JavaFlowSupport.Sink.asPublisher[Int](fanout = false).mapMaterializedValue { p =>
| JavaFlowSupport.Source.fromPublisher(p).runFold(0)(_ + _)
| })
|
| val result = Await.result(future, 3.seconds)
| println(s"Result:" + result)
| system.terminate()
| exit(if (result == 6) 0 else 1)
| }
|}
|
|""".stripMargin
}

0 comments on commit 7e83ab7

Please sign in to comment.