-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add scala-cli script generator to verify jdk classes
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |