Add sbt-java-formatter for java code formatting.
This commit is contained in:
parent
eaca6de25f
commit
27500001ea
7 changed files with 172 additions and 75 deletions
35
.sbt-java-formatter.conf
Normal file
35
.sbt-java-formatter.conf
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
//ignore by files
|
||||
// 1. the protobuf generated messages
|
||||
ignored-files = [
|
||||
"ArteryControlFormats.java",
|
||||
"ContainerFormats.java",
|
||||
"SystemMessageFormats.java",
|
||||
"WireFormats.java",
|
||||
"StreamRefMessages.java",
|
||||
//in tests
|
||||
"ProtobufProtocol.java"
|
||||
]
|
||||
|
||||
//ignore by pacakges:
|
||||
// 1. adated source code
|
||||
// 2. protobuf generated messages
|
||||
ignored-packages = [
|
||||
"akka.dispatch",
|
||||
"akka.protobuf",
|
||||
"sun.reflect",
|
||||
"akka.cluster.protobuf.msg",
|
||||
"akka.cluster.metrics.protobuf.msg",
|
||||
"akka.cluster.sharding.protobuf.msg",
|
||||
"akka.cluster.sharding.typed.internal.protobuf",
|
||||
"akka.cluster.client.protobuf.msg",
|
||||
"akka.cluster.pubsub.protobuf.msg",
|
||||
"akka.cluster.typed.internal.protobuf",
|
||||
"akka.cluster.ddata.protobuf.msg",
|
||||
"akka.remote.testconductor",
|
||||
"akka.persistence.serialization",
|
||||
//in tests
|
||||
"akka.remote.artery.protobuf",
|
||||
//in docs
|
||||
"docs.ddata.protobuf.msg",
|
||||
"docs.persistence.proto"
|
||||
]
|
||||
12
build.sbt
12
build.sbt
|
|
@ -1,17 +1,19 @@
|
|||
import akka.{AutomaticModuleName, CopyrightHeaderForBuild, ParadoxSupport, ScalafixIgnoreFilePlugin}
|
||||
|
||||
enablePlugins(UnidocRoot, TimeStampede, UnidocWithPrValidation, NoPublish, CopyrightHeader, CopyrightHeaderInPr,
|
||||
ScalafixIgnoreFilePlugin)
|
||||
enablePlugins(UnidocRoot, TimeStampede, UnidocWithPrValidation, NoPublish, CopyrightHeader,
|
||||
CopyrightHeaderInPr,
|
||||
ScalafixIgnoreFilePlugin,
|
||||
JavaFormatterPlugin)
|
||||
disablePlugins(MimaPlugin)
|
||||
addCommandAlias(
|
||||
name ="fixall",
|
||||
value = ";scalafixEnable;compile:scalafix;test:scalafix;multi-jvm:scalafix;test:compile;reload")
|
||||
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.MultiJvm
|
||||
import com.typesafe.tools.mima.plugin.MimaPlugin
|
||||
import spray.boilerplate.BoilerplatePlugin
|
||||
import akka.AkkaBuild._
|
||||
import akka.{AkkaBuild, Dependencies, GitHub, OSGi, Protobuf, SigarLoader, VersionGenerator}
|
||||
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.MultiJvm
|
||||
import com.typesafe.tools.mima.plugin.MimaPlugin
|
||||
import sbt.Keys.{initialCommands, parallelExecution}
|
||||
import spray.boilerplate.BoilerplatePlugin
|
||||
|
||||
initialize := {
|
||||
// Load system properties from a file to make configuration from Jenkins easier
|
||||
|
|
|
|||
|
|
@ -4,10 +4,9 @@
|
|||
|
||||
package akka
|
||||
|
||||
import sbt._
|
||||
import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.MultiJvm
|
||||
import com.typesafe.sbt.SbtScalariform
|
||||
import com.typesafe.sbt.SbtScalariform.ScalariformKeys
|
||||
import sbt._
|
||||
|
||||
object Formatting {
|
||||
import scalariform.formatter.preferences._
|
||||
|
|
|
|||
31
project/JavaFormatter.scala
Normal file
31
project/JavaFormatter.scala
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
import akka.ProjectFileIgnoreSupport
|
||||
import com.lightbend.sbt.JavaFormatterPlugin
|
||||
import sbt.{AutoPlugin, PluginTrigger, Plugins}
|
||||
|
||||
object JavaFormatter extends AutoPlugin with ProjectFileIgnoreSupport {
|
||||
|
||||
override def trigger = PluginTrigger.AllRequirements
|
||||
|
||||
override def requires: Plugins = JavaFormatterPlugin
|
||||
|
||||
final override protected val ignoreConfigFileName: String = ".sbt-java-formatter.conf"
|
||||
|
||||
final override protected val descriptor: String = "sbt-java-formatter"
|
||||
|
||||
import JavaFormatterPlugin.autoImport._
|
||||
import sbt.Keys._
|
||||
import sbt._
|
||||
import sbt.io._
|
||||
|
||||
override def projectSettings: Seq[Def.Setting[_]] = Seq(
|
||||
//below is for sbt java formatter
|
||||
(excludeFilter in format) := {
|
||||
val simpleFileFilter = new SimpleFileFilter(file => isIgnoredByFileOrPackages(file))
|
||||
simpleFileFilter || (excludeFilter in format).value
|
||||
}
|
||||
)
|
||||
}
|
||||
89
project/ProjectFileIgnoreSupport.scala
Normal file
89
project/ProjectFileIgnoreSupport.scala
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package akka
|
||||
|
||||
import java.io.File
|
||||
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import sbt.file
|
||||
import sbt.internal.sbtscalafix.Compat
|
||||
|
||||
trait ProjectFileIgnoreSupport {
|
||||
protected val stdoutLogger = Compat.ConsoleLogger(System.out)
|
||||
|
||||
protected def ignoreConfigFileName: String
|
||||
|
||||
protected def descriptor: String
|
||||
|
||||
lazy val ignoredFiles: Set[String] = {
|
||||
import scala.collection.JavaConverters._
|
||||
val config = ConfigFactory.parseFile(file(ignoreConfigFileName))
|
||||
stdoutLogger.info(s"Loading ignored-files from $ignoreConfigFileName:[${config.origin().url().toURI.getPath}]")
|
||||
config
|
||||
.getStringList("ignored-files")
|
||||
.asScala
|
||||
.toSet
|
||||
}
|
||||
|
||||
lazy val ignoredPackages: Set[String] = {
|
||||
import scala.collection.JavaConverters._
|
||||
val config = ConfigFactory.parseFile(file(ignoreConfigFileName))
|
||||
stdoutLogger.info(s"Loading ignored-packages from $ignoreConfigFileName:[${config.origin().url().toURI.getPath}]")
|
||||
config
|
||||
.getStringList("ignored-packages")
|
||||
.asScala
|
||||
.toSet
|
||||
}
|
||||
|
||||
protected def isIgnoredByFile(file: File): Boolean = {
|
||||
val ignoredByFile = ignoredFiles(file.getName)
|
||||
if (ignoredByFile) {
|
||||
stdoutLogger.info(s"$descriptor ignored file with file name:${file.getName} file:[${file.toPath}]")
|
||||
}
|
||||
ignoredByFile
|
||||
}
|
||||
|
||||
protected def isIgnoredByPackages(file: File): Boolean = {
|
||||
val ignoredByPackages = ignoredPackages.exists(pkg => {
|
||||
getPackageName(file.toURI.toString) match {
|
||||
case Some(packageName) =>
|
||||
val ignored = packageName.startsWith(pkg)
|
||||
if (ignored) {
|
||||
stdoutLogger.info(s"$descriptor ignored file with pkg:$pkg file:[${file.toPath}] ")
|
||||
}
|
||||
ignored
|
||||
case None => false
|
||||
}
|
||||
})
|
||||
ignoredByPackages
|
||||
}
|
||||
|
||||
protected def isIgnoredByFileOrPackages(file: File): Boolean = {
|
||||
isIgnoredByFile(file) || isIgnoredByPackages(file)
|
||||
}
|
||||
|
||||
protected def getPackageName(fileName: String): Option[String] = {
|
||||
def getPackageName0(fileType: String): String = {
|
||||
import java.io.{File => JFile}
|
||||
fileName.split(JFile.separatorChar)
|
||||
.dropWhile(part ⇒ part != fileType)
|
||||
.drop(1)
|
||||
.dropRight(1)
|
||||
.mkString(".")
|
||||
}
|
||||
|
||||
fileName.split('.').lastOption match {
|
||||
case Some(fileType) ⇒
|
||||
fileType match {
|
||||
case "java" ⇒
|
||||
Option(getPackageName0("java"))
|
||||
case "scala" ⇒
|
||||
Option(getPackageName0("scala"))
|
||||
case _ ⇒ None
|
||||
}
|
||||
case None ⇒ None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,82 +4,23 @@
|
|||
|
||||
package sbt
|
||||
|
||||
import java.io.{ File => JFile }
|
||||
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import akka.ProjectFileIgnoreSupport
|
||||
import sbt.Keys.unmanagedSources
|
||||
import sbt.internal.sbtscalafix.Compat
|
||||
|
||||
trait ScalafixSupport {
|
||||
trait ScalafixSupport extends ProjectFileIgnoreSupport {
|
||||
|
||||
import scalafix.sbt.ScalafixPlugin.autoImport._
|
||||
final override protected val ignoreConfigFileName: String = ".scalafix.conf"
|
||||
|
||||
final override protected val descriptor: String = "scalafix"
|
||||
|
||||
protected def ignore(configKey: ConfigKey): Def.Setting[Task[Seq[File]]] = {
|
||||
lazy val stdoutLogger = Compat.ConsoleLogger(System.out)
|
||||
|
||||
lazy val ignoredFiles: Set[String] = {
|
||||
import scala.collection.JavaConverters._
|
||||
val config = ConfigFactory.parseFile(file(".scalafix.conf"))
|
||||
stdoutLogger.info(s"Loading ignored-files from .scalaifx.config:[${config.origin().url().toURI.getPath}]")
|
||||
config
|
||||
.getStringList("ignored-files")
|
||||
.asScala
|
||||
.toSet
|
||||
}
|
||||
|
||||
lazy val ignoredPackages: Set[String] = {
|
||||
import scala.collection.JavaConverters._
|
||||
val config = ConfigFactory.parseFile(file(".scalafix.conf"))
|
||||
stdoutLogger.info(s"Loading ignored-packages from .scalaifx.config:[${config.origin().url().toURI.getPath}]")
|
||||
config
|
||||
.getStringList("ignored-packages")
|
||||
.asScala
|
||||
.toSet
|
||||
}
|
||||
import scalafix.sbt.ScalafixPlugin.autoImport._
|
||||
|
||||
unmanagedSources.in(configKey, scalafix) :=
|
||||
unmanagedSources.in(configKey, scalafix).value
|
||||
.filterNot(file => {
|
||||
val ignoredByFile = ignoredFiles(file.getName)
|
||||
if (ignoredByFile) {
|
||||
stdoutLogger.info(s"scalafix ignored file: ${file.toURI} with file name: ${file.getName}")
|
||||
}
|
||||
val ignoredByPackages = ignoredPackages.exists(pkg => {
|
||||
getPackageName(file.toURI.toString) match {
|
||||
case Some(packageName) =>
|
||||
val ignored = packageName.startsWith(pkg)
|
||||
if (ignored) {
|
||||
stdoutLogger.info(s"scalafix ignored file with pkg:$pkg file:[${file.toPath}] ")
|
||||
}
|
||||
ignored
|
||||
case None => false
|
||||
}
|
||||
})
|
||||
ignoredByFile || ignoredByPackages
|
||||
})
|
||||
.filterNot(file => isIgnoredByFileOrPackages(file))
|
||||
}
|
||||
|
||||
private def getPackageName(fileName: String): Option[String] = {
|
||||
def getPackageName0(fileType: String): String = {
|
||||
fileName.split(JFile.separatorChar)
|
||||
.dropWhile(part ⇒ part != fileType)
|
||||
.drop(1)
|
||||
.dropRight(1)
|
||||
.mkString(".")
|
||||
}
|
||||
|
||||
fileName.split('.').lastOption match {
|
||||
case Some(fileType) ⇒
|
||||
fileType match {
|
||||
case "java" ⇒
|
||||
Option(getPackageName0("java"))
|
||||
case "scala" ⇒
|
||||
Option(getPackageName0("scala"))
|
||||
case _ ⇒ None
|
||||
}
|
||||
case None ⇒ None
|
||||
}
|
||||
}
|
||||
|
||||
import sbt.Keys._
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ libraryDependencies += "org.kohsuke" % "github-api" % "1.95"
|
|||
addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.4.0")
|
||||
//#sbt-multi-jvm
|
||||
|
||||
addSbtPlugin("com.lightbend.sbt" % "sbt-java-formatter" % "0.4.1")
|
||||
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2")
|
||||
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.1")
|
||||
addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.9.4")
|
||||
|
|
@ -24,6 +25,5 @@ addSbtPlugin("com.lightbend" % "sbt-whitesource" % "0.1.13")
|
|||
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0")
|
||||
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.1") // for advanced PR validation features
|
||||
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.0.0") // for maintenance of copyright file header
|
||||
|
||||
// used for @unidoc directive
|
||||
libraryDependencies += "io.github.classgraph" % "classgraph" % "4.4.12"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue