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

[INFRA] Improve the java style checks log the errors to sbt console #3115

Merged
merged 3 commits into from
May 22, 2024
Merged
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
39 changes: 1 addition & 38 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// scalastyle:off line.size.limit

import java.nio.file.Files
import Checkstyle._
import Mima._
import Unidoc._

Expand Down Expand Up @@ -1309,44 +1310,6 @@ lazy val kernelGroup = project
}
).configureUnidoc(docTitle = "Delta Kernel")

/*
***********************
* ScalaStyle settings *
***********************
*/
ThisBuild / scalastyleConfig := baseDirectory.value / "scalastyle-config.xml"

lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
lazy val testScalastyle = taskKey[Unit]("testScalastyle")

lazy val scalaStyleSettings = Seq(
compileScalastyle := (Compile / scalastyle).toTask("").value,

Compile / compile := ((Compile / compile) dependsOn compileScalastyle).value,

testScalastyle := (Test / scalastyle).toTask("").value,

Test / test := ((Test / test) dependsOn testScalastyle).value
)

/*
****************************
* Java checkstyle settings *
****************************
*/

def javaCheckstyleSettings(checkstyleFile: String): Def.SettingsDefinition = {
// Can be run explicitly via: build/sbt $module/checkstyle
// Will automatically be run during compilation (e.g. build/sbt compile)
// and during tests (e.g. build/sbt test)
Seq(
checkstyleConfigLocation := CheckstyleConfigLocation.File(checkstyleFile),
checkstyleSeverityLevel := CheckstyleSeverityLevel.Error,
(Compile / compile) := ((Compile / compile) dependsOn (Compile / checkstyle)).value,
(Test / test) := ((Test / test) dependsOn (Test / checkstyle)).value
)
}

/*
********************
* Release settings *
Expand Down
103 changes: 103 additions & 0 deletions project/Checkstyle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (2024) The Delta Lake Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.etsy.sbt.checkstyle.CheckstylePlugin.autoImport._
import org.scalastyle.sbt.ScalastylePlugin.autoImport._
import sbt._
import sbt.Keys._

object Checkstyle {

/*
*****************************
* Scala checkstyle settings *
*****************************
*/

ThisBuild / scalastyleConfig := baseDirectory.value / "scalastyle-config.xml"

private lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
private lazy val testScalastyle = taskKey[Unit]("testScalastyle")

lazy val scalaStyleSettings = Seq(
compileScalastyle := (Compile / scalastyle).toTask("").value,

Compile / compile := ((Compile / compile) dependsOn compileScalastyle).value,

testScalastyle := (Test / scalastyle).toTask("").value,

Test / test := ((Test / test) dependsOn testScalastyle).value
)

/*
****************************
* Java checkstyle settings *
****************************
*/

private lazy val compileJavastyle = taskKey[Unit]("compileJavastyle")
private lazy val testJavastyle = taskKey[Unit]("testJavastyle")

def javaCheckstyleSettings(checkstyleFile: String): Def.SettingsDefinition = {
// Can be run explicitly via: build/sbt $module/checkstyle
// Will automatically be run during compilation (e.g. build/sbt compile)
// and during tests (e.g. build/sbt test)
Seq(
checkstyleConfigLocation := CheckstyleConfigLocation.File(checkstyleFile),
// if we keep the Error severity, `build/sbt` will throw an error and immediately stop at
// the `checkstyle` phase (if error) -> never execute the `check-report` phase of
// `checkstyle-report.xml` and `checkstyle-test-report.xml`. We need to ignore and throw
// error if exists when checking *report.xml.
checkstyleSeverityLevel := CheckstyleSeverityLevel.Ignore,

compileJavastyle := {
(Compile / checkstyle).value
javaCheckstyle(streams.value.log, checkstyleOutputFile.value)
},
(Compile / compile) := ((Compile / compile) dependsOn compileJavastyle).value,

testJavastyle := {
(Test / checkstyle).value
javaCheckstyle(streams.value.log, (Compile / target).value / "checkstyle-test-report.xml")
},
(Test / test) := ((Test / test) dependsOn (Test / testJavastyle)).value
)
}

private def javaCheckstyle(log: Logger, reportFile: File): Unit = {
val report = scala.xml.XML.loadFile(reportFile)

val errors = (report \\ "file").flatMap { fileNode =>
val file = fileNode.attribute("name").get.head.text
(fileNode \ "error").map { error =>
val line = error.attribute("line").get.head.text
val message = error.attribute("message").get.head.text
(file, line, message)
}
}

if (errors.nonEmpty) {
var errorMsg = "Found checkstyle errors"
errors.foreach { case (file, line, message) =>
val lineError = s"File: $file, Line: $line, Message: $message"
log.error(lineError)
errorMsg += ("\n" + lineError)
}
sys.error(errorMsg + "\n")
}
}

}
Loading