Skip to content

Commit

Permalink
Verify compilation during build
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 19, 2024
1 parent 820794c commit 39bb650
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ jobs:
sudo apt-get update
sudo apt-get install libidn2-dev libcurl3-dev
echo "STTP_NATIVE=1" >> $GITHUB_ENV
- name: Install scala-cli
if: matrix.target-platform == 'JVM' && matrix.java == '21' && matrix.scala-version == '3'
uses: VirtusLab/scala-cli-setup@main
with:
jvm: '' # needed because scala-cli-setup otherwise forces the installation of their default JVM (17)
- name: Enable Loom-specific modules
if: matrix.java == '21'
run: echo "ONLY_LOOM=1" >> $GITHUB_ENV
Expand All @@ -57,6 +62,9 @@ jobs:
- name: Compile documentation
if: matrix.target-platform == 'JVM' && matrix.java == '11'
run: sbt -v compileDocs
- name: Verify that examples compile using Scala CLI
if: matrix.target-platform == 'JVM' && matrix.java == '21' && matrix.scala-version == '3'
run: sbt $SBT_JAVA_OPTS -v "project examples3" verifyExamplesCompileUsingScalaCli
- name: Test
run: sbt -v "testScoped ${{ matrix.scala-version }} ${{ matrix.target-platform }}"
- name: Prepare release notes
Expand Down
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ val ideScalaVersion = scala3

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

// slow down for CI
parallelExecution in Global := false
Expand Down Expand Up @@ -967,7 +968,8 @@ lazy val examples = (projectMatrix in file("examples"))
"org.json4s" %% "json4s-native" % json4sVersion,
pekkoStreams,
logback
)
),
verifyExamplesCompileUsingScalaCli := VerifyExamplesCompileUsingScalaCli(sLog.value, sourceDirectory.value)
)
.jvmPlatform(scalaVersions = List(examplesScalaVersion))
.dependsOn(
Expand Down
22 changes: 22 additions & 0 deletions project/FileUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.io.File
import java.nio.file.{FileVisitResult, Files, Path, SimpleFileVisitor}
import java.nio.file.attribute.BasicFileAttributes

object FileUtils {
def listScalaFiles(basePath: File): Seq[Path] = {
val dirPath = basePath.toPath
var result = Vector.empty[Path]

val fileVisitor = new SimpleFileVisitor[Path] {
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
if (file.toString.endsWith(".scala")) {
result = result :+ file
}
FileVisitResult.CONTINUE
}
}

Files.walkFileTree(dirPath, fileVisitor)
result
}
}
24 changes: 24 additions & 0 deletions project/VerifyExamplesCompileUsingScalaCli.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.io.File
import sbt.Logger
import scala.sys.process.{Process, ProcessLogger}

object VerifyExamplesCompileUsingScalaCli {
def apply(log: Logger, examplesSrcPath: File): Unit = {
val examples = FileUtils.listScalaFiles(examplesSrcPath)
log.info(s"Found ${examples.size} examples")

for (example <- examples) {
log.info(s"Compiling: $example")
val errorOutput = new StringBuilder
val logger = ProcessLogger((o: String) => (), (e: String) => errorOutput.append(e + "\n"))
try {
val result = Process(List("scala-cli", "compile", example.toFile.getAbsolutePath), examplesSrcPath).!(logger)
if (result != 0) {
throw new Exception(s"""Compiling $example failed.\n$errorOutput""".stripMargin)
}
} finally {
Process(List("scala-cli", "clean", example.toFile.getAbsolutePath), examplesSrcPath).!
}
}
}
}

0 comments on commit 39bb650

Please sign in to comment.