Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ object JavaFormatterPlugin extends AutoPlugin {
.build(),
javafmt := {
val streamz = streams.value
val projectId = thisProjectRef.value.project
val sD = (javafmt / sourceDirectories).value.toList
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
Expand All @@ -141,6 +142,7 @@ object JavaFormatterPlugin extends AutoPlugin {
val removeUnusedImports = javafmtRemoveUnusedImports.value
val reflowLongStrings = javafmtReflowLongStrings.value
JavaFormatter(
projectId,
sD,
iF,
eF,
Expand All @@ -155,6 +157,7 @@ object JavaFormatterPlugin extends AutoPlugin {
},
javafmtCheck := {
val streamz = streams.value
val projectId = thisProjectRef.value.project
val baseDir = (ThisBuild / baseDirectory).value
val sD = (javafmt / sourceDirectories).value.toList
val iF = (javafmt / includeFilter).value
Expand All @@ -167,6 +170,7 @@ object JavaFormatterPlugin extends AutoPlugin {
val removeUnusedImports = javafmtRemoveUnusedImports.value
val reflowLongStrings = javafmtReflowLongStrings.value
JavaFormatter.check(
projectId,
baseDir,
sD,
iF,
Expand All @@ -182,6 +186,7 @@ object JavaFormatterPlugin extends AutoPlugin {
},
javafmtFixImports := {
val streamz = streams.value
val projectId = thisProjectRef.value.project
val sD = (javafmt / sourceDirectories).value.toList
val iF = (javafmt / includeFilter).value
val eF = (javafmt / excludeFilter).value
Expand All @@ -193,6 +198,7 @@ object JavaFormatterPlugin extends AutoPlugin {
val removeUnusedImports = javafmtRemoveUnusedImports.value
val reflowLongStrings = javafmtReflowLongStrings.value
JavaFormatter.fixImports(
projectId,
sD,
iF,
eF,
Expand All @@ -207,6 +213,7 @@ object JavaFormatterPlugin extends AutoPlugin {
},
javafmtFixImportsCheck := {
val streamz = streams.value
val projectId = thisProjectRef.value.project
val baseDir = (ThisBuild / baseDirectory).value
val sD = (javafmt / sourceDirectories).value.toList
val iF = (javafmt / includeFilter).value
Expand All @@ -219,6 +226,7 @@ object JavaFormatterPlugin extends AutoPlugin {
val removeUnusedImports = javafmtRemoveUnusedImports.value
val reflowLongStrings = javafmtReflowLongStrings.value
JavaFormatter.fixImportsCheck(
projectId,
baseDir,
sD,
iF,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ object JavaFormatter {
private val GoogleJavaFormatMain = "com.google.googlejavaformat.java.Main"
private val JavaHomeEnvVar = "SBT_JAVAFMT_JAVA_HOME"
private val JavaHomeProperty = "sbt-javafmt.java.home"
private val incompatibleJavaRuntimeHelpLogged = new AtomicBoolean(false)
private val incompatibleJavaRuntimeHelpLoggedByProject =
new scala.collection.concurrent.TrieMap[String, AtomicBoolean]

private val JavaExports = Seq("api", "code", "file", "parser", "tree", "util").map { exportedPackage =>
s"--add-exports=jdk.compiler/com.sun.tools.javac.$exportedPackage=ALL-UNNAMED"
}

def apply(
projectId: String,
sourceDirectories: Seq[File],
includeFilter: FileFilter,
excludeFilter: FileFilter,
Expand All @@ -55,6 +57,7 @@ object JavaFormatter {
cacheStoreFactory,
files,
streams.log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -65,6 +68,7 @@ object JavaFormatter {
}

def fixImports(
projectId: String,
sourceDirectories: Seq[File],
includeFilter: FileFilter,
excludeFilter: FileFilter,
Expand All @@ -81,6 +85,7 @@ object JavaFormatter {
cacheStoreFactory,
files,
streams.log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -91,6 +96,7 @@ object JavaFormatter {
}

def check(
projectId: String,
baseDir: File,
sourceDirectories: Seq[File],
includeFilter: FileFilter,
Expand All @@ -110,6 +116,7 @@ object JavaFormatter {
baseDir,
files,
streams.log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -121,6 +128,7 @@ object JavaFormatter {
}

def fixImportsCheck(
projectId: String,
baseDir: File,
sourceDirectories: Seq[File],
includeFilter: FileFilter,
Expand All @@ -140,6 +148,7 @@ object JavaFormatter {
baseDir,
files,
streams.log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand Down Expand Up @@ -178,6 +187,7 @@ object JavaFormatter {
baseDir: File,
sources: Seq[File],
log: Logger,
projectId: String,
options: JavaFormatterOptions,
formatterClasspath: Seq[File],
javaMaxHeap: Option[String],
Expand All @@ -195,6 +205,7 @@ object JavaFormatter {
baseDir,
filesToCheck.toList,
log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -214,6 +225,7 @@ object JavaFormatter {
baseDir: File,
sources: Seq[File],
log: Logger,
projectId: String,
options: JavaFormatterOptions,
formatterClasspath: Seq[File],
javaMaxHeap: Option[String],
Expand All @@ -229,6 +241,7 @@ object JavaFormatter {
baseDir,
sources,
log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -244,6 +257,7 @@ object JavaFormatter {
cacheStoreFactory: CacheStoreFactory,
sources: Seq[File],
log: Logger,
projectId: String,
options: JavaFormatterOptions,
formatterClasspath: Seq[File],
javaMaxHeap: Option[String],
Expand All @@ -260,6 +274,7 @@ object JavaFormatter {
formatSources(
filesToFormat,
log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -275,6 +290,7 @@ object JavaFormatter {
private def formatSources(
sources: Set[File],
log: Logger,
projectId: String,
options: JavaFormatterOptions,
formatterClasspath: Seq[File],
javaMaxHeap: Option[String],
Expand All @@ -287,6 +303,7 @@ object JavaFormatter {
baseDir = new File("."),
sources.toList,
log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand All @@ -299,6 +316,7 @@ object JavaFormatter {
runReplace(
changed.toList,
log,
projectId,
options,
formatterClasspath,
javaMaxHeap,
Expand Down Expand Up @@ -357,11 +375,13 @@ object JavaFormatter {

private case class CliResult(exitCode: Int, stdout: Vector[String], stderr: Vector[String])

private def logCliFailure(result: CliResult, log: Logger): Unit = {
private def logCliFailure(result: CliResult, log: Logger, projectId: String): Unit = {
result.stderr.foreach(line => log.error(line))
result.stdout.foreach(line => log.error(line))
incompatibleJavaRuntimeHelp(result).foreach { message =>
if (incompatibleJavaRuntimeHelpLogged.compareAndSet(false, true)) {
val loggedForProject =
incompatibleJavaRuntimeHelpLoggedByProject.getOrElseUpdate(projectId, new AtomicBoolean(false))
if (loggedForProject.compareAndSet(false, true)) {
log.info(message)
}
}
Expand Down Expand Up @@ -449,6 +469,7 @@ object JavaFormatter {
baseDir: File,
sources: Seq[File],
log: Logger,
projectId: String,
options: JavaFormatterOptions,
formatterClasspath: Seq[File],
javaMaxHeap: Option[String],
Expand All @@ -469,13 +490,13 @@ object JavaFormatter {
result.exitCode match {
case 0 | 1 =>
if (result.exitCode == 1 && changed.isEmpty) {
logCliFailure(result, log)
logCliFailure(result, log, projectId)
throw new MessageOnlyException("google-java-format check failed")
}
changed
case _ =>
if (warnOnFailure) {
logCliFailure(result, log)
logCliFailure(result, log, projectId)
}
throw new MessageOnlyException("google-java-format check failed")
}
Expand All @@ -484,6 +505,7 @@ object JavaFormatter {
private def runReplace(
sources: Seq[File],
log: Logger,
projectId: String,
options: JavaFormatterOptions,
formatterClasspath: Seq[File],
javaMaxHeap: Option[String],
Expand All @@ -499,8 +521,7 @@ object JavaFormatter {
"--replace") ++ sources.map(_.getAbsolutePath)
val result = runCli(args, formatterClasspath, log, javaMaxHeap)
if (result.exitCode != 0) {
result.stderr.foreach(line => log.error(line))
result.stdout.foreach(line => log.error(line))
logCliFailure(result, log, projectId)
throw new MessageOnlyException("google-java-format failed")
}
}
Expand Down