Skip to content

Commit 20107a6

Browse files
committed
Fix exceptional handling for sbt tasks
1 parent d2c59a0 commit 20107a6

File tree

6 files changed

+68
-24
lines changed

6 files changed

+68
-24
lines changed

plugin/src/main/scala/net/moznion/sbt/SbtSpotless.scala

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.io.File
2121
import com.diffplug.spotless.Provisioner
2222
import net.moznion.sbt.spotless._
2323
import net.moznion.sbt.spotless.config._
24+
import net.moznion.sbt.spotless.exception.FormatException
2425
import net.moznion.sbt.spotless.task._
2526
import sbt.Keys._
2627
import sbt.{Def, _}
@@ -55,8 +56,11 @@ object SbtSpotless extends AutoPlugin {
5556

5657
override def projectSettings: Seq[Def.Setting[_]] =
5758
super.projectSettings ++ Seq(
58-
spotlessCheck := supplySpotlessTaskInitiator(RunningMode(check = true)).value,
59-
spotlessApply := supplySpotlessTaskInitiator(RunningMode(check = true, applyFormat = true)).value,
59+
spotlessCheck := supplySpotlessTaskInitiator(RunningMode(check = true), "spotlessCheck").value,
60+
spotlessApply := supplySpotlessTaskInitiator(
61+
RunningMode(check = true, applyFormat = true),
62+
"spotlessApply",
63+
).value,
6064
)
6165

6266
override def globalSettings: Seq[Def.Setting[_]] = Seq(
@@ -69,8 +73,8 @@ object SbtSpotless extends AutoPlugin {
6973
spotlessSql := SqlConfig(enabled = false),
7074
)
7175

72-
private val supplySpotlessTaskInitiator: RunningMode => Def.Initialize[Task[Unit]] = {
73-
mode: RunningMode =>
76+
private val supplySpotlessTaskInitiator: (RunningMode, String) => Def.Initialize[Task[Unit]] = {
77+
(mode: RunningMode, taskName: String) =>
7478
Def.task {
7579
val defaultBaseDir: File = thisProject.value.base
7680
val config: SpotlessConfig = spotless.value
@@ -86,37 +90,59 @@ object SbtSpotless extends AutoPlugin {
8690
(sources in Compile).value.toList ++ (sources in Test).value.toList
8791

8892
val javaConfig: JavaConfig = spotlessJava.value
93+
94+
var tasksToRun: Seq[RunnableTask[_]] = Seq()
95+
8996
if (javaConfig.enabled) {
9097
val javaFiles =
9198
classPathFiles.filter(p => javaConfig.getExtensions.exists(ext => p.ext.equals(ext)))
92-
Java(javaFiles, javaConfig, pathConfig, logger).run(provisioner, mode)
99+
tasksToRun :+= Java(javaFiles, javaConfig, pathConfig, logger)
93100
}
94101

95102
val scalaConfig: ScalaConfig = spotlessScala.value
96103
if (scalaConfig.enabled) {
97104
val scalaFiles =
98105
classPathFiles.filter(p => scalaConfig.getExtensions.exists(ext => p.ext.equals(ext)))
99-
Scala(scalaFiles, scalaConfig, pathConfig, logger).run(provisioner, mode)
106+
tasksToRun :+= Scala(scalaFiles, scalaConfig, pathConfig, logger)
100107
}
101108

102109
val cppConfig: CppConfig = spotlessCpp.value
103110
if (cppConfig.enabled) {
104-
Cpp(cppConfig, pathConfig, logger).run(provisioner, mode)
111+
tasksToRun :+= Cpp(cppConfig, pathConfig, logger)
105112
}
106113

107114
val groovyConfig: GroovyConfig = spotlessGroovy.value
108115
if (groovyConfig.enabled) {
109-
Groovy(groovyConfig, pathConfig, logger).run(provisioner, mode)
116+
tasksToRun :+= Groovy(groovyConfig, pathConfig, logger)
110117
}
111118

112119
val kotlinConfig: KotlinConfig = spotlessKotlin.value
113120
if (kotlinConfig.enabled) {
114-
Kotlin(kotlinConfig, pathConfig, logger).run(provisioner, mode)
121+
tasksToRun :+= Kotlin(kotlinConfig, pathConfig, logger)
115122
}
116123

117124
val sqlConfig: SqlConfig = spotlessSql.value
118125
if (sqlConfig.enabled) {
119-
Sql(sqlConfig, pathConfig, logger).run(provisioner, mode)
126+
tasksToRun :+= Sql(sqlConfig, pathConfig, logger)
127+
}
128+
129+
val succeeded: Boolean = tasksToRun
130+
.map(task =>
131+
try {
132+
task.run(provisioner, mode)
133+
true
134+
} catch {
135+
case e: FormatException =>
136+
logger.error(e.getMessage)
137+
false
138+
},
139+
)
140+
.reduce((acc, taskResult) => acc && taskResult)
141+
142+
if (!succeeded) {
143+
throw new MessageOnlyException(
144+
s"Failed to run $taskName, please refer to the above logs.",
145+
)
120146
}
121147
}
122148
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2020 moznion.net
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.moznion.sbt.spotless.exception
18+
19+
abstract class FormatException(message: String) extends Exception(message)

plugin/src/main/scala/net/moznion/sbt/spotless/exception/ShouldTurnOnPaddedCellException.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object ShouldTurnOnPaddedCellException {
3232
|work around this bug and generate helpful bug reports for the broken rule
3333
|if you add 'paddedCell = true' to your build.sbt as such:
3434
|
35-
| ${taskName} := ${configClassName}(
35+
| $taskName := $configClassName(
3636
| ...
3737
| paddedCell = true,
3838
| )
@@ -41,15 +41,15 @@ object ShouldTurnOnPaddedCellException {
4141
|"${pathConfig.paddedCellDiagnoseDir.toString}", and spotlessApply
4242
|and spotlessCheck will be self-consistent from here on out.
4343
|
44-
|For details see: ${paddedCellDescriptionURL}""".stripMargin
44+
|For details see: $paddedCellDescriptionURL""".stripMargin
4545
}
4646

4747
case class ShouldTurnOnPaddedCellException(
4848
private val taskName: String,
4949
private val configClassName: String,
5050
private val paddedCellDescriptionURL: String,
5151
private val pathConfig: SpotlessPathConfig,
52-
) extends Exception(
52+
) extends FormatException(
5353
ShouldTurnOnPaddedCellException
5454
.message(taskName, configClassName, paddedCellDescriptionURL, pathConfig),
5555
) {}

plugin/src/main/scala/net/moznion/sbt/spotless/exception/ViolatedFormatException.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
package net.moznion.sbt.spotless.exception
1818

19-
case class ViolatedFormatException(msg: String) extends Exception(msg) {}
19+
case class ViolatedFormatException(msg: String) extends FormatException(msg) {}

plugin/src/main/scala/net/moznion/sbt/spotless/task/Cpp.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ private[sbt] case class Cpp[T <: CppConfig](
3232
private val pathConfig: SpotlessPathConfig,
3333
private val logger: Logger,
3434
) extends RunnableTask[T] {
35-
def run(
36-
provisioner: Provisioner,
37-
mode: RunningMode,
38-
): Unit = {
35+
def run(provisioner: Provisioner, mode: RunningMode): Unit = {
3936
if (!config.enabled) {
4037
return
4138
}

plugin/src/main/scala/net/moznion/sbt/spotless/task/RunnableTask.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import java.nio.charset.Charset
2121
import java.nio.file.{Files, Path, StandardOpenOption}
2222

2323
import com.diffplug.spotless.extra.integration.DiffMessageFormatter
24-
import com.diffplug.spotless.{Formatter, LineEnding, PaddedCell, PaddedCellBulk}
24+
import com.diffplug.spotless.{Formatter, LineEnding, PaddedCell, PaddedCellBulk, Provisioner}
2525
import net.moznion.sbt.spotless.Target.{IsFile, IsString}
2626
import net.moznion.sbt.spotless.config.{FormatterConfig, SpotlessPathConfig}
2727
import net.moznion.sbt.spotless.exception.{ShouldTurnOnPaddedCellException, ViolatedFormatException}
28-
import net.moznion.sbt.spotless.{FormatterSteps, Target}
28+
import net.moznion.sbt.spotless.{FormatterSteps, RunningMode, Target}
2929
import sbt.util.Logger
3030

3131
import _root_.scala.collection.JavaConverters._
@@ -36,6 +36,8 @@ trait RunnableTask[T <: FormatterConfig] {
3636

3737
private[spotless] def getTarget: Seq[File]
3838

39+
private[sbt] def run(provisioner: Provisioner, mode: RunningMode): Unit
40+
3941
private[spotless] def resolveTarget(target: Seq[Target], baseDir: File): Seq[File] = {
4042
target.flatMap {
4143
case IsFile(file) => Seq(file)
@@ -96,9 +98,9 @@ trait RunnableTask[T <: FormatterConfig] {
9698
logger: Logger,
9799
): Unit = {
98100
if (problemFiles.isEmpty) {
99-
logger.info(s"""|${getName} is in `paddedCell` mode, but it doesn't need to be.
101+
logger.info(s"""|$getName is in `paddedCell` mode, but it doesn't need to be.
100102
|If you remove that option, spotless will run ~2x faster.
101-
|For details see ${paddedCellDescriptionURL}""".stripMargin)
103+
|For details see $paddedCellDescriptionURL""".stripMargin)
102104
}
103105

104106
val stillFailingFiles = PaddedCellBulk.check(
@@ -204,6 +206,6 @@ trait RunnableTask[T <: FormatterConfig] {
204206
.build()
205207
}
206208

207-
def getName: String;
208-
def getClassName: String;
209+
def getName: String
210+
def getClassName: String
209211
}

0 commit comments

Comments
 (0)