Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the relaxedMode & strictMode commands #48

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ By default the plugin only applies this filtering to the `console` task in the `
scalacOptions.in(Tut) ~= filterConsoleScalacOptions
```

Additionally, if you need to temporarily disable fatal warnings.
You can do it by running the `relaxedMode` command.
Then, you can enable them again by running the `strictMode` command.<br>
You may also enable the relaxed mode by setting the `SBT_TPOLECAT_RELAXED` env var.

### Caveat

I can't promise this plugin will work for old minor releases of Scala. It has been tested with:
Expand Down
77 changes: 54 additions & 23 deletions src/main/scala/io/github/davidgregory084/TpolecatPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.github.davidgregory084

import sbt._
import sbt.Keys._
import scala.util.Try
import scala.util.{Properties, Try}

object TpolecatPlugin extends AutoPlugin {
override def trigger: PluginTrigger = allRequirements
Expand Down Expand Up @@ -128,27 +128,31 @@ object TpolecatPlugin extends AutoPlugin {
addedPriorTo && notYetRemoved
}

def scalacOptionsFor(version: String): Seq[String] =
List(
"-encoding", "utf8" // Specify character encoding used by source files.
) ++ {

val flags = (CrossVersion.partialVersion(version), version.split('.')) match {
case (Some((0, min)), _) => // dotty prereleases use 0 as major version
allScalacOptions
.filter(validFor(V3_0_0)) // treat dotty prereleases as 3.0.0
case (Some((maj, min)), Array(maj2, min2, patch)) if maj.toString == maj2 && min.toString == min2 =>
allScalacOptions
.filter(validFor(Version(maj, min, Try(patch.toLong).getOrElse(0))))
case (Some((maj, min)), _) =>
allScalacOptions
.filter(validFor(Version(maj, min, 0)))
case (None, _) =>
Nil
}

flags.map(_.name)
}
def scalacOptionsFor(version: String): Seq[String] = {
val flags = (CrossVersion.partialVersion(version), version.split('.')) match {
case (Some((0, min)), _) => // dotty prereleases use 0 as major version
allScalacOptions
.filter(validFor(V3_0_0)) // treat dotty prereleases as 3.0.0
case (Some((maj, min)), Array(maj2, min2, patch)) if maj.toString == maj2 && min.toString == min2 =>
allScalacOptions
.filter(validFor(Version(maj, min, Try(patch.toLong).getOrElse(0))))
case (Some((maj, min)), _) =>
allScalacOptions
.filter(validFor(Version(maj, min, 0)))
case (None, _) =>
Nil
}

// Specify character encoding used by source files.
val allFlags = "-encoding" :: "utf8" :: flags.map(_.name)

// Check if we are in relaxed mode.
val relaxed = Properties.envOrNone(name = "SBT_TPOLECAT_RELAXED")
relaxed.fold(ifEmpty = allFlags) { _ =>
allFlags.filterNot(Set("-Xfatal-warnings"))
}
}

val filterConsoleScalacOptions = { options: Seq[String] =>
options.filterNot(Set(
"-Werror",
Expand All @@ -161,13 +165,40 @@ object TpolecatPlugin extends AutoPlugin {
"-Xfatal-warnings"
))
}

lazy val strictMode: Command = Command.command("strictMode") { state =>
val projectRef = Project.extract(state)

val actions = projectRef.structure.allProjectRefs.map { p =>
p / scalacOptions += "-Xfatal-warnings"
}

projectRef.appendWithSession(
actions,
state
)
}

lazy val relaxedMode: Command = Command.command("relaxedMode") { state =>
val projectRef = Project.extract(state)

val actions = projectRef.structure.allProjectRefs.map { p =>
p / scalacOptions -= "-Xfatal-warnings"
}

projectRef.appendWithSession(
actions,
state
)
}
}

import autoImport._

override def projectSettings: Seq[Setting[_]] = Seq(
scalacOptions ++= scalacOptionsFor(scalaVersion.value),
Compile / console / scalacOptions ~= filterConsoleScalacOptions,
Test / console / scalacOptions ~= filterConsoleScalacOptions
Test / console / scalacOptions ~= filterConsoleScalacOptions,
commands ++= Seq(strictMode, relaxedMode)
)
}