Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 23629c7

Browse files
committed
Allow defining fatal warnings with sbt settings
1 parent 8393b33 commit 23629c7

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

plugin-sbt/src/main/scala/com/softwaremill/clippy/ClippySbtPlugin.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ object ClippySbtPlugin extends AutoPlugin {
4343
settingKey[Option[String]]("Directory where cached advice data should be stored, if other than default")
4444
val clippyProjectRoot =
4545
settingKey[Option[String]]("Project root in which project-specific advice is stored, if any")
46+
val clippyFatalWarnings =
47+
settingKey[List[String]]("Regular expressions of warning messages which should fail compilation")
4648
}
4749

4850
// in ~/.sbt auto import doesn't work, so providing aliases here for convenience
@@ -56,6 +58,7 @@ object ClippySbtPlugin extends AutoPlugin {
5658
val clippyUrl = autoImport.clippyUrl
5759
val clippyLocalStoreDir = autoImport.clippyLocalStoreDir
5860
val clippyProjectRoot = autoImport.clippyProjectRoot
61+
val clippyFatalWarnings = autoImport.clippyFatalWarnings
5962

6063
override def projectSettings = Seq(
6164
clippyColorsEnabled := false,
@@ -68,6 +71,7 @@ object ClippySbtPlugin extends AutoPlugin {
6871
clippyUrl := None,
6972
clippyLocalStoreDir := None,
7073
clippyProjectRoot := None,
74+
clippyFatalWarnings := Nil,
7175
addCompilerPlugin("com.softwaremill.clippy" %% "plugin" % ClippyBuildInfo.version classifier "bundle"),
7276
scalacOptions := {
7377
val result = ListBuffer(scalacOptions.value: _*)
@@ -81,6 +85,8 @@ object ClippySbtPlugin extends AutoPlugin {
8185
clippyUrl.value.foreach(c => result += s"-P:clippy:url=$c")
8286
clippyLocalStoreDir.value.foreach(c => result += s"-P:clippy:store=$c")
8387
clippyProjectRoot.value.foreach(c => result += s"-P:clippy:projectRoot=$c")
88+
if (clippyFatalWarnings.value.nonEmpty)
89+
result += s"-P:clippy:fatalWarnings=${clippyFatalWarnings.value.mkString("|")}"
8490
result.toList
8591
}
8692
)

plugin/src/main/scala/com/softwaremill/clippy/ClippyPlugin.scala

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ class ClippyPlugin(val global: Global) extends Plugin {
2828

2929
override val description: String = "gives good advice"
3030

31-
var url: String = ""
32-
var colorsConfig: ColorsConfig = ColorsConfig.Disabled
33-
var testMode = false
34-
val DefaultStoreDir = new File(System.getProperty("user.home"), ".clippy")
35-
var localStoreDir = DefaultStoreDir
36-
var projectRoot: Option[File] = None
31+
var url: String = ""
32+
var colorsConfig: ColorsConfig = ColorsConfig.Disabled
33+
var testMode = false
34+
val DefaultStoreDir = new File(System.getProperty("user.home"), ".clippy")
35+
var localStoreDir = DefaultStoreDir
36+
var projectRoot: Option[File] = None
37+
var initialFatalWarnings: List[Warning] = Nil
3738

3839
lazy val localAdviceFiles = {
3940
val classPathURLs = new PathResolver(global.settings).result.asURLs
@@ -72,7 +73,7 @@ class ClippyPlugin(val global: Global) extends Plugin {
7273
testMode = testModeFromOptions(options)
7374
localStoreDir = localStoreDirFromOptions(options)
7475
projectRoot = projectRootFromOptions(options)
75-
76+
initialFatalWarnings = initialFatalWarningsFromOptions(options)
7677
if (testMode) {
7778
val r = global.reporter
7879
global.reporter = new FailOnWarningsReporter(
@@ -177,6 +178,15 @@ class ClippyPlugin(val global: Global) extends Plugin {
177178
.map(new File(_, ".clippy.json"))
178179
.filter(_.exists())
179180

181+
private def initialFatalWarningsFromOptions(options: List[String]): List[Warning] =
182+
options
183+
.find(_.startsWith("fatalWarnings="))
184+
.map(_.substring(14))
185+
.map { str =>
186+
str.split('|').toList.map(str => Warning(RegexT(str), text = None))
187+
}
188+
.getOrElse(Nil)
189+
180190
private def localStoreDirFromOptions(options: List[String]): File =
181191
options.find(_.startsWith("store=")).map(_.substring(6)).map(new File(_)).getOrElse(DefaultStoreDir)
182192

@@ -194,7 +204,7 @@ class ClippyPlugin(val global: Global) extends Plugin {
194204
new AdviceLoader(global, url, localStoreDir, projectAdviceFile, localAdviceFiles).load(),
195205
10.seconds
196206
)
197-
AdvicesAndWarnings(clippyData.advices, clippyData.fatalWarnings)
207+
AdvicesAndWarnings(clippyData.advices, clippyData.fatalWarnings ++ initialFatalWarnings)
198208
} catch {
199209
case e: TimeoutException =>
200210
global.warning(s"Unable to read advices from $url and store to $localStoreDir within 10 seconds.")

0 commit comments

Comments
 (0)