Skip to content

Commit

Permalink
feat: read scala-dialect from sbt settings (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo-vrijswijk authored Mar 11, 2025
1 parent eed08d1 commit 9879518
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import scala.meta.Dialect
import Stryker4sPlugin.autoImport.*

object SbtConfigSource {
def apply[F[_]]() = Def.setting[ConfigSource[F]] {
def apply[F[_]]() = Def.task[ConfigSource[F]] {
new ConfigSource[F] with CirisConfigDecoders {

override def name: String = "sbt settings"
Expand Down
25 changes: 22 additions & 3 deletions modules/sbt/src/main/scala/stryker4s/sbt/Stryker4sPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sbt.Keys.*
import sbt.plugins.*
import sbt.{given, *}
import stryker4s.config.DashboardReportType
import stryker4s.config.codec.CirisConfigDecoders
import stryker4s.config.source.CliConfigSource
import stryker4s.log.{Logger, SbtLogger}
import stryker4s.run.threshold.ErrorStatus
Expand All @@ -14,7 +15,7 @@ import sttp.model.Uri
import xsbti.FileConverter

import scala.concurrent.duration.FiniteDuration
import scala.meta.Dialect
import scala.meta.{dialects, Dialect}

/** This plugin adds a new task (stryker) to the project that allows you to run mutation testing over your code
*/
Expand Down Expand Up @@ -56,7 +57,7 @@ object Stryker4sPlugin extends AutoPlugin {
)
val strykerMaxTestRunnerReuse = settingKey[Int]("Restart the testrunner after every `n` runs.")
val strykerLegacyTestRunner = settingKey[Boolean]("Use the legacy test runner (not recommended)")
val strykerScalaDialect = settingKey[Dialect]("Dialect for parsing Scala files")
val strykerScalaDialect = taskKey[Dialect]("Dialect for parsing Scala files")
val strykerConcurrency = settingKey[Int]("Number of testrunners to start")
val strykerDebugLogTestRunnerStdout = settingKey[Boolean]("Log test-runner output to debug log")
val strykerDebugDebugTestRunner = settingKey[Boolean]("Pass JVM debugging parameters to the test-runner")
Expand All @@ -82,7 +83,8 @@ object Stryker4sPlugin extends AutoPlugin {
strykerFiles := getSourceDirectories("**").value,
strykerDashboardProject := scmInfo.value.flatMap(toDashboardProject),
strykerDashboardVersion := gitCurrentBranch.?.value.filterNot(_.isBlank()),
strykerDashboardModule := normalizedName.value
strykerDashboardModule := normalizedName.value,
strykerScalaDialect := getStrykerScalaDialect.value
)

/** Dynamically load git-current-branch from sbt-git if it is installed, for the dashboard version config
Expand Down Expand Up @@ -110,6 +112,23 @@ object Stryker4sPlugin extends AutoPlugin {
.getOrElse(baseDirectory.value)
}

/** Get the Scala dialect for the current Scala version.
*/
private def getStrykerScalaDialect = Def.task[Dialect] {
val hasSource3 = (Compile / scalacOptions).value.exists(_.startsWith("-Xsource:3"))
val reader = (major: Long, minor: Long, hasSource3: Boolean) =>
new CirisConfigDecoders {}.dialectReader
.decode(none, s"scala$major$minor${if (hasSource3) "source3" else ""}")
.toOption
CrossVersion
.partialVersion((Compile / scalaVersion).value)
.flatMap {
case (3, minor) => reader(3, minor, false).orElse(dialects.Scala3.some)
case (major, minor) => reader(major, minor, hasSource3)
}
.getOrElse(if (hasSource3) dialects.Scala213Source3 else dialects.Scala213)
}

/** Parse scmInfo to a dashboard project identifier, using the format of `gitProvider/organization/repository`.
*
* E.g. `github.com/stryker-mutator/stryker4s`
Expand Down

0 comments on commit 9879518

Please sign in to comment.