Skip to content

Commit 39bb650

Browse files
committed
Verify compilation during build
1 parent 820794c commit 39bb650

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ jobs:
4949
sudo apt-get update
5050
sudo apt-get install libidn2-dev libcurl3-dev
5151
echo "STTP_NATIVE=1" >> $GITHUB_ENV
52+
- name: Install scala-cli
53+
if: matrix.target-platform == 'JVM' && matrix.java == '21' && matrix.scala-version == '3'
54+
uses: VirtusLab/scala-cli-setup@main
55+
with:
56+
jvm: '' # needed because scala-cli-setup otherwise forces the installation of their default JVM (17)
5257
- name: Enable Loom-specific modules
5358
if: matrix.java == '21'
5459
run: echo "ONLY_LOOM=1" >> $GITHUB_ENV
@@ -57,6 +62,9 @@ jobs:
5762
- name: Compile documentation
5863
if: matrix.target-platform == 'JVM' && matrix.java == '11'
5964
run: sbt -v compileDocs
65+
- name: Verify that examples compile using Scala CLI
66+
if: matrix.target-platform == 'JVM' && matrix.java == '21' && matrix.scala-version == '3'
67+
run: sbt $SBT_JAVA_OPTS -v "project examples3" verifyExamplesCompileUsingScalaCli
6068
- name: Test
6169
run: sbt -v "testScoped ${{ matrix.scala-version }} ${{ matrix.target-platform }}"
6270
- name: Prepare release notes

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ val ideScalaVersion = scala3
2121

2222
lazy val testServerPort = settingKey[Int]("Port to run the http test server on")
2323
lazy val startTestServer = taskKey[Unit]("Start a http server used by tests")
24+
lazy val verifyExamplesCompileUsingScalaCli = taskKey[Unit]("Verify that each example compiles using Scala CLI")
2425

2526
// slow down for CI
2627
parallelExecution in Global := false
@@ -967,7 +968,8 @@ lazy val examples = (projectMatrix in file("examples"))
967968
"org.json4s" %% "json4s-native" % json4sVersion,
968969
pekkoStreams,
969970
logback
970-
)
971+
),
972+
verifyExamplesCompileUsingScalaCli := VerifyExamplesCompileUsingScalaCli(sLog.value, sourceDirectory.value)
971973
)
972974
.jvmPlatform(scalaVersions = List(examplesScalaVersion))
973975
.dependsOn(

project/FileUtils.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.File
2+
import java.nio.file.{FileVisitResult, Files, Path, SimpleFileVisitor}
3+
import java.nio.file.attribute.BasicFileAttributes
4+
5+
object FileUtils {
6+
def listScalaFiles(basePath: File): Seq[Path] = {
7+
val dirPath = basePath.toPath
8+
var result = Vector.empty[Path]
9+
10+
val fileVisitor = new SimpleFileVisitor[Path] {
11+
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
12+
if (file.toString.endsWith(".scala")) {
13+
result = result :+ file
14+
}
15+
FileVisitResult.CONTINUE
16+
}
17+
}
18+
19+
Files.walkFileTree(dirPath, fileVisitor)
20+
result
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.io.File
2+
import sbt.Logger
3+
import scala.sys.process.{Process, ProcessLogger}
4+
5+
object VerifyExamplesCompileUsingScalaCli {
6+
def apply(log: Logger, examplesSrcPath: File): Unit = {
7+
val examples = FileUtils.listScalaFiles(examplesSrcPath)
8+
log.info(s"Found ${examples.size} examples")
9+
10+
for (example <- examples) {
11+
log.info(s"Compiling: $example")
12+
val errorOutput = new StringBuilder
13+
val logger = ProcessLogger((o: String) => (), (e: String) => errorOutput.append(e + "\n"))
14+
try {
15+
val result = Process(List("scala-cli", "compile", example.toFile.getAbsolutePath), examplesSrcPath).!(logger)
16+
if (result != 0) {
17+
throw new Exception(s"""Compiling $example failed.\n$errorOutput""".stripMargin)
18+
}
19+
} finally {
20+
Process(List("scala-cli", "clean", example.toFile.getAbsolutePath), examplesSrcPath).!
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)