Skip to content

Commit

Permalink
Introduce tpolecatExcludeOptions setting key and fix wrong addition…
Browse files Browse the repository at this point in the history
… of unwanted options to console task

Fix test reference

Fix test reference

Fix test reference
  • Loading branch information
Ivan Stanislavciuc committed Apr 15, 2022
1 parent 5726c98 commit 20f29c4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/main/scala/io/github/davidgregory084/ScalacOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,12 @@ trait ScalacOptions {
optimizerInline,
optimizerInlineFrom(inlineFromPackages: _*)
)

/** Default options to exclude in console tasks
*/
val defaultConsoleExclude: Set[ScalacOption] = privateWarnUnusedOptions ++
warnUnusedOptions ++
fatalWarningOptions +
privateWarnDeadCode +
warnDeadCode
}
32 changes: 18 additions & 14 deletions src/main/scala/io/github/davidgregory084/TpolecatPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object TpolecatPlugin extends AutoPlugin {
modeScalacOptions: Set[ScalacOption]
): Seq[String] = {
val supportedOptions = (CrossVersion.partialVersion(version), version.split('.')) match {
case (Some((0, min)), _) => // dotty prereleases use 0 as major version
case (Some((0, _)), _) => // dotty prereleases use 0 as major version
modeScalacOptions
.filter(_.isSupported(V3_0_0)) // treat dotty prereleases as 3.0.0
case (Some((maj, min)), Array(maj2, min2, patch))
Expand All @@ -51,16 +51,6 @@ object TpolecatPlugin extends AutoPlugin {
supportedOptions.toList.flatMap(_.tokens)
}

val tpolecatConsoleOptionsFilter = { options: Set[ScalacOption] =>
options.filterNot(
ScalacOptions.privateWarnUnusedOptions ++
ScalacOptions.warnUnusedOptions ++
ScalacOptions.fatalWarningOptions +
ScalacOptions.privateWarnDeadCode +
ScalacOptions.warnDeadCode
)
}

val tpolecatDefaultOptionsMode = settingKey[OptionsMode](
"The default mode to use for configuring scalac options via the sbt-tpolecat plugin."
)
Expand Down Expand Up @@ -96,6 +86,10 @@ object TpolecatPlugin extends AutoPlugin {
val tpolecatScalacOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin."
)

val tpolecatExcludeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be excluded."
)
}

import autoImport._
Expand Down Expand Up @@ -129,7 +123,13 @@ object TpolecatPlugin extends AutoPlugin {

override def projectSettings: Seq[Setting[_]] = Seq(
Def.derive(
scalacOptions ++= scalacOptionsFor(scalaVersion.value, tpolecatScalacOptions.value)
scalacOptions := {
val previous = scalacOptions.value
val scalaV = scalaVersion.value
val filters = scalacOptionsFor(scalaV, tpolecatExcludeOptions.value).toSet
val newOptions = scalacOptionsFor(scalaV, tpolecatScalacOptions.value)
(previous ++ newOptions).filterNot(filters).distinct
}
),
tpolecatDevModeOptions := ScalacOptions.default,
Def.derive(
Expand All @@ -145,7 +145,11 @@ object TpolecatPlugin extends AutoPlugin {
case ReleaseMode => tpolecatReleaseModeOptions.value
}
}),
Compile / console / tpolecatScalacOptions ~= tpolecatConsoleOptionsFilter,
Test / console / tpolecatScalacOptions ~= tpolecatConsoleOptionsFilter
Compile / console / tpolecatExcludeOptions ++= ScalacOptions.defaultConsoleExclude,
Test / console / tpolecatExcludeOptions ++= ScalacOptions.defaultConsoleExclude
)

override def globalSettings: Seq[Def.Setting[_]] = Seq(
tpolecatExcludeOptions := Set.empty
)
}
34 changes: 34 additions & 0 deletions src/sbt-test/sbt-tpolecat/scalacOptions/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,37 @@ TaskKey[Unit]("checkReleaseMode") := {

assertEquals(actualOptions, expectedOptions)
}

TaskKey[Unit]("checkConsoleScalacOptions") := {
val shouldBeMissing = ScalacOptions.defaultConsoleExclude.flatMap(_.tokens).toSet
val testConsoleOptions = (Test / console / scalacOptions).value
val compileConsoleOptions = (Compile / console / scalacOptions).value

testConsoleOptions.foreach { opt =>
assert(!shouldBeMissing.contains(opt), s"$opt is not excluded from Test/console")
}

compileConsoleOptions.foreach { opt =>
assert(!shouldBeMissing.contains(opt), s"$opt is not excluded from Compile/console")
}
}

addCommandAlias(
"addScalacOptionsToThisProject",
"set ThisProject / scalacOptions += \"non-existent-key\""
)

TaskKey[Unit]("checkThisProjectScalacOptions") := {
val options = (Compile / scalacOptions).value
assert(options.contains("non-existent-key"), "Scope ThisProject was ignored")
}

addCommandAlias(
"addScalacOptionsToThisBuild",
"set ThisBuild / scalacOptions += \"non-existent-key-2\""
)

TaskKey[Unit]("checkThisBuildScalacOptions") := {
val options = (Compile / scalacOptions).value
assert(options.contains("non-existent-key-2"), "Scope ThisBuild was ignored")
}
8 changes: 8 additions & 0 deletions src/sbt-test/sbt-tpolecat/scalacOptions/test
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@
> tpolecatReleaseMode
> +checkReleaseMode
> +compile
# Check console options
> +checkConsoleScalacOptions
# Check ThisProject
> addScalacOptionsToThisProject
> +checkThisProjectScalacOptions
# Check ThisBuild (Ignored)
> addScalacOptionsToThisBuild
> +checkThisBuildScalacOptions

0 comments on commit 20f29c4

Please sign in to comment.