diff --git a/.scalafmt.conf b/.scalafmt.conf index ae5197f..35f7546 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -2,6 +2,8 @@ style = defaultWithAlign danglingParentheses = true indentOperator = spray +maxColumn = 100 +project.excludeFilters = [".*\\.sbt"] rewrite.rules = [RedundantBraces, RedundantParens, SortImports] spaces.inImportCurlyBraces = true unindentTopLevelOperators = true diff --git a/build.sbt b/build.sbt index ebf5370..dcbf33f 100644 --- a/build.sbt +++ b/build.sbt @@ -68,11 +68,11 @@ lazy val library = new { object Version { final val akka = "2.4.17" - final val akkaHttp = "10.0.3" + final val akkaHttp = "10.0.5" final val akkaLog4j = "1.3.0" - final val circe = "0.7.0" - final val log4j = "2.8" - final val mockito = "2.6.8" + final val circe = "0.7.1" + final val log4j = "2.8.2" + final val mockito = "2.7.22" final val scalaTest = "3.0.1" } val akkaActor = "com.typesafe.akka" %% "akka-actor" % Version.akka @@ -94,7 +94,6 @@ lazy val library = lazy val settings = commonSettings ++ - scalafmtSettings ++ gitSettings ++ headerSettings ++ sonatypeSettings ++ @@ -107,8 +106,7 @@ lazy val commonSettings = organization := "de.heikoseeberger", licenses += ("Apache 2.0", url("http://www.apache.org/licenses/LICENSE-2.0")), - mappings.in(Compile, packageBin) += - baseDirectory.in(ThisBuild).value / "LICENSE" -> "LICENSE", + mappings.in(Compile, packageBin) += baseDirectory.in(ThisBuild).value / "LICENSE" -> "LICENSE", scalacOptions ++= Seq( "-unchecked", "-deprecation", @@ -116,26 +114,10 @@ lazy val commonSettings = "-target:jvm-1.8", "-encoding", "UTF-8" ), - javacOptions ++= Seq( - "-source", "1.8", - "-target", "1.8" - ), - unmanagedSourceDirectories.in(Compile) := - Seq(scalaSource.in(Compile).value), - unmanagedSourceDirectories.in(Test) := - Seq(scalaSource.in(Test).value) + unmanagedSourceDirectories.in(Compile) := Seq(scalaSource.in(Compile).value), + unmanagedSourceDirectories.in(Test) := Seq(scalaSource.in(Test).value) ) -lazy val scalafmtSettings = - reformatOnCompileSettings ++ - Seq( - formatSbtFiles := false, - scalafmtConfig := - Some(baseDirectory.in(ThisBuild).value / ".scalafmt.conf"), - ivyScala := - ivyScala.value.map(_.copy(overrideScalaVersion = sbtPlugin.value)) // TODO Remove once this workaround no longer needed (https://github.com/sbt/sbt/issues/2786)! - ) - lazy val gitSettings = Seq( git.useGitDescribe := true @@ -164,21 +146,11 @@ lazy val bintraySettings = bintrayPackage := "constructr" ) -import ScalaFmtPlugin.configScalafmtSettings lazy val multiJvmSettings = + automateScalafmtFor(MultiJvm) ++ AutomateHeaderPlugin.automateFor(Compile, Test, MultiJvm) ++ HeaderPlugin.settingsFor(Compile, Test, MultiJvm) ++ - inConfig(MultiJvm)(configScalafmtSettings) ++ Seq( - unmanagedSourceDirectories.in(MultiJvm) := - Seq(scalaSource.in(MultiJvm).value), - test.in(Test) := { - val testValue = test.in(Test).value - test.in(MultiJvm).value - testValue - }, - compileInputs.in(MultiJvm, compile) := { - val scalafmtValue = scalafmt.in(MultiJvm).value - compileInputs.in(MultiJvm, compile).value - } + unmanagedSourceDirectories.in(MultiJvm) := Seq(scalaSource.in(MultiJvm).value), + test.in(Test) := test.in(MultiJvm).dependsOn(test.in(Test)).value ) diff --git a/coordination-etcd/src/main/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordination.scala b/coordination-etcd/src/main/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordination.scala index 8d89060..dff86a4 100644 --- a/coordination-etcd/src/main/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordination.scala +++ b/coordination-etcd/src/main/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordination.scala @@ -21,19 +21,8 @@ import akka.Done import akka.actor.{ ActorSystem, Address, AddressFromURIString } import akka.http.scaladsl.Http import akka.http.scaladsl.client.RequestBuilding.{ Get, Put } -import akka.http.scaladsl.model.StatusCodes.{ - Created, - NotFound, - OK, - PreconditionFailed -} -import akka.http.scaladsl.model.{ - HttpRequest, - HttpResponse, - ResponseEntity, - StatusCode, - Uri -} +import akka.http.scaladsl.model.StatusCodes.{ Created, NotFound, OK, PreconditionFailed } +import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, ResponseEntity, StatusCode, Uri } import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink @@ -54,8 +43,7 @@ object EtcdCoordination { private def toSeconds(duration: Duration) = (duration.toSeconds + 1).toString } -final class EtcdCoordination(clusterName: String, system: ActorSystem) - extends Coordination { +final class EtcdCoordination(clusterName: String, system: ActorSystem) extends Coordination { import EtcdCoordination._ private implicit val mat = ActorMaterializer()(system) diff --git a/coordination-etcd/src/test/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordinationSpec.scala b/coordination-etcd/src/test/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordinationSpec.scala index cd315d6..dd72884 100644 --- a/coordination-etcd/src/test/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordinationSpec.scala +++ b/coordination-etcd/src/test/scala/de/heikoseeberger/constructr/coordination/etcd/EtcdCoordinationSpec.scala @@ -36,10 +36,7 @@ object EtcdCoordinationSpec { } } -class EtcdCoordinationSpec - extends WordSpec - with Matchers - with BeforeAndAfterAll { +class EtcdCoordinationSpec extends WordSpec with Matchers with BeforeAndAfterAll { import EtcdCoordinationSpec._ private implicit val system = { @@ -83,8 +80,7 @@ class EtcdCoordinationSpec super.afterAll() } - private def resultOf[A](awaitable: Awaitable[A], - max: FiniteDuration = 3.seconds.dilated) = + private def resultOf[A](awaitable: Awaitable[A], max: FiniteDuration = 3.seconds.dilated) = Await.result(awaitable, max) private def randomString() = math.abs(Random.nextInt).toString diff --git a/core/src/main/scala/de/heikoseeberger/constructr/Constructr.scala b/core/src/main/scala/de/heikoseeberger/constructr/Constructr.scala index 4024768..1617707 100644 --- a/core/src/main/scala/de/heikoseeberger/constructr/Constructr.scala +++ b/core/src/main/scala/de/heikoseeberger/constructr/Constructr.scala @@ -16,21 +16,9 @@ package de.heikoseeberger.constructr -import akka.actor.{ - Actor, - ActorLogging, - ActorRef, - Props, - SupervisorStrategy, - Terminated -} +import akka.actor.{ Actor, ActorLogging, ActorRef, Props, SupervisorStrategy, Terminated } import akka.cluster.{ Cluster, Member } -import akka.cluster.ClusterEvent.{ - InitialStateAsEvents, - MemberExited, - MemberLeft, - MemberRemoved -} +import akka.cluster.ClusterEvent.{ InitialStateAsEvents, MemberExited, MemberLeft, MemberRemoved } import akka.cluster.MemberStatus.Up import de.heikoseeberger.constructr.coordination.Coordination import scala.concurrent.duration.{ FiniteDuration, NANOSECONDS } diff --git a/core/src/main/scala/de/heikoseeberger/constructr/ConstructrExtension.scala b/core/src/main/scala/de/heikoseeberger/constructr/ConstructrExtension.scala index 260d0fe..60f569c 100644 --- a/core/src/main/scala/de/heikoseeberger/constructr/ConstructrExtension.scala +++ b/core/src/main/scala/de/heikoseeberger/constructr/ConstructrExtension.scala @@ -20,7 +20,6 @@ import akka.actor.{ ExtendedActorSystem, Extension, ExtensionKey } object ConstructrExtension extends ExtensionKey[ConstructrExtension] -final class ConstructrExtension private (system: ExtendedActorSystem) - extends Extension { +final class ConstructrExtension private (system: ExtendedActorSystem) extends Extension { system.systemActorOf(Constructr.props, Constructr.Name) } diff --git a/core/src/main/scala/de/heikoseeberger/constructr/ConstructrMachine.scala b/core/src/main/scala/de/heikoseeberger/constructr/ConstructrMachine.scala index f9c85d8..4140981 100644 --- a/core/src/main/scala/de/heikoseeberger/constructr/ConstructrMachine.scala +++ b/core/src/main/scala/de/heikoseeberger/constructr/ConstructrMachine.scala @@ -20,11 +20,7 @@ import akka.Done import akka.actor.FSM.Failure import akka.actor.{ Address, FSM, Props, Status } import akka.cluster.Cluster -import akka.cluster.ClusterEvent.{ - InitialStateAsEvents, - MemberJoined, - MemberUp -} +import akka.cluster.ClusterEvent.{ InitialStateAsEvents, MemberJoined, MemberUp } import akka.pattern.pipe import akka.stream.ActorMaterializer import de.heikoseeberger.constructr.coordination.Coordination @@ -52,9 +48,7 @@ object ConstructrMachine { case object RetryScheduled extends State } - final case class Data(nodes: Set[Address], - retryState: State, - nrOfRetriesLeft: Int) + final case class Data(nodes: Set[Address], retryState: State, nrOfRetriesLeft: Int) final case class StateTimeoutException(state: State) extends RuntimeException(s"State timeout triggered in state $state!") @@ -111,8 +105,7 @@ final class ConstructrMachine( private implicit val mat = ActorMaterializer() private val cluster = Cluster(context.system) - startWith(State.GettingNodes, - Data(Set.empty, State.GettingNodes, nrOfRetries)) + startWith(State.GettingNodes, Data(Set.empty, State.GettingNodes, nrOfRetries)) // Getting nodes @@ -298,8 +291,7 @@ final class ConstructrMachine( stop(FSM.Failure(s"Number of retries exhausted in $stateName!")) else goto(State.RetryScheduled).using( - stateData.copy(retryState = retryState, - nrOfRetriesLeft = stateData.nrOfRetriesLeft - 1) + stateData.copy(retryState = retryState, nrOfRetriesLeft = stateData.nrOfRetriesLeft - 1) ) private def maxCoordinationTimeout = diff --git a/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeConstructrSpec.scala b/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeConstructrSpec.scala index 86a011a..bb7d325 100644 --- a/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeConstructrSpec.scala +++ b/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeConstructrSpec.scala @@ -43,8 +43,7 @@ object ConstructrMultiNodeConfig { } } -class ConstructrMultiNodeConfig(coordinationPort: Int) - extends MultiNodeConfig { +class ConstructrMultiNodeConfig(coordinationPort: Int) extends MultiNodeConfig { import ConstructrMultiNodeConfig._ commonConfig(ConfigFactory.load()) @@ -109,20 +108,17 @@ abstract class MultiNodeConstructrSpec( become { case "isMember" => sender() ! isMember - case MemberJoined(member) - if member.address == Cluster(context.system).selfAddress => + case MemberJoined(member) if member.address == Cluster(context.system).selfAddress => isMember = true - case MemberUp(member) - if member.address == Cluster(context.system).selfAddress => + case MemberUp(member) if member.address == Cluster(context.system).selfAddress => isMember = true } }) within(20.seconds.dilated) { awaitAssert { implicit val timeout = Timeout(1.second.dilated) - val isMember = Await.result((listener ? "isMember").mapTo[Boolean], - 1.second.dilated) + val isMember = Await.result((listener ? "isMember").mapTo[Boolean], 1.second.dilated) isMember shouldBe true } } diff --git a/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeEtcdConstructrSpec.scala b/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeEtcdConstructrSpec.scala index 5fe772f..c21f075 100644 --- a/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeEtcdConstructrSpec.scala +++ b/core/src/multi-jvm/scala/de/heikoseeberger/constructr/MultiNodeEtcdConstructrSpec.scala @@ -21,16 +21,11 @@ import io.circe.Json import io.circe.parser.parse import java.util.Base64 -class MultiNodeEtcdConstructrSpecMultiJvmNode1 - extends MultiNodeEtcdConstructrSpec -class MultiNodeEtcdConstructrSpecMultiJvmNode2 - extends MultiNodeEtcdConstructrSpec -class MultiNodeEtcdConstructrSpecMultiJvmNode3 - extends MultiNodeEtcdConstructrSpec -class MultiNodeEtcdConstructrSpecMultiJvmNode4 - extends MultiNodeEtcdConstructrSpec -class MultiNodeEtcdConstructrSpecMultiJvmNode5 - extends MultiNodeEtcdConstructrSpec +class MultiNodeEtcdConstructrSpecMultiJvmNode1 extends MultiNodeEtcdConstructrSpec +class MultiNodeEtcdConstructrSpecMultiJvmNode2 extends MultiNodeEtcdConstructrSpec +class MultiNodeEtcdConstructrSpecMultiJvmNode3 extends MultiNodeEtcdConstructrSpec +class MultiNodeEtcdConstructrSpecMultiJvmNode4 extends MultiNodeEtcdConstructrSpec +class MultiNodeEtcdConstructrSpecMultiJvmNode5 extends MultiNodeEtcdConstructrSpec object MultiNodeEtcdConstructrSpec { def toNodes(s: String): Set[Address] = { diff --git a/core/src/test/scala/de/heikoseeberger/constructr/ConstructrMachineSpec.scala b/core/src/test/scala/de/heikoseeberger/constructr/ConstructrMachineSpec.scala index 8714f78..b445b0b 100644 --- a/core/src/test/scala/de/heikoseeberger/constructr/ConstructrMachineSpec.scala +++ b/core/src/test/scala/de/heikoseeberger/constructr/ConstructrMachineSpec.scala @@ -28,10 +28,7 @@ import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } import scala.concurrent.duration.{ Duration, DurationInt } import scala.concurrent.{ Await, Future } -final class ConstructrMachineSpec - extends WordSpec - with Matchers - with BeforeAndAfterAll { +final class ConstructrMachineSpec extends WordSpec with Matchers with BeforeAndAfterAll { import ConstructrMachine._ import Mockito._ diff --git a/project/AutomateScalafmtPlugin.scala b/project/AutomateScalafmtPlugin.scala new file mode 100644 index 0000000..92fd54f --- /dev/null +++ b/project/AutomateScalafmtPlugin.scala @@ -0,0 +1,66 @@ +import org.scalafmt.bootstrap.ScalafmtBootstrap +import org.scalafmt.sbt.ScalafmtPlugin +import sbt._ +import sbt.Keys._ +import sbt.inc.Analysis + +object AutomateScalafmtPlugin extends AutoPlugin { + + object autoImport { + def automateScalafmtFor(configurations: Configuration*): Seq[Setting[_]] = + configurations.flatMap { c => + inConfig(c)( + Seq( + compileInputs.in(compile) := { + scalafmtInc.value + compileInputs.in(compile).value + }, + sourceDirectories.in(scalafmtInc) := Seq(scalaSource.value), + scalafmtInc := { + val cache = streams.value.cacheDirectory / "scalafmt" + val include = includeFilter.in(scalafmtInc).value + val exclude = excludeFilter.in(scalafmtInc).value + val sources = + sourceDirectories + .in(scalafmtInc) + .value + .descendantsExcept(include, exclude) + .get + .toSet + def format(handler: Set[File] => Unit, msg: String) = { + def update(handler: Set[File] => Unit, msg: String)(in: ChangeReport[File], + out: ChangeReport[File]) = { + val label = Reference.display(thisProjectRef.value) + val files = in.modified -- in.removed + Analysis + .counted("Scala source", "", "s", files.size) + .foreach(count => streams.value.log.info(s"$msg $count in $label ...")) + handler(files) + files + } + FileFunction.cached(cache)(FilesInfo.hash, FilesInfo.exists)(update(handler, msg))( + sources + ) + } + def formattingHandler(files: Set[File]) = + if (files.nonEmpty) { + val filesArg = files.map(_.getAbsolutePath).mkString(",") + ScalafmtBootstrap.main(List("--quiet", "-i", "-f", filesArg)) + } + format(formattingHandler, "Formatting") + format(_ => (), "Reformatted") // Recalculate the cache + } + ) + ) + } + } + + private val scalafmtInc = taskKey[Unit]("Incrementally format modified sources") + + override def requires = ScalafmtPlugin + + override def trigger = allRequirements + + override def projectSettings = + (includeFilter.in(scalafmtInc) := "*.scala") +: autoImport.automateScalafmtFor(Compile, Test) +} diff --git a/project/plugins.sbt b/project/plugins.sbt index adf1a12..ff09a30 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,7 +1,9 @@ -addSbtPlugin("com.dwijnand" % "sbt-travisci" % "1.0.0") -addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.4.10") +addSbtPlugin("com.dwijnand" % "sbt-travisci" % "1.1.0") +addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.6.6") addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5") +addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.9.2") addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.11") -addSbtPlugin("de.heikoseeberger" % "sbt-header" % "1.6.0") +addSbtPlugin("de.heikoseeberger" % "sbt-header" % "1.8.0") addSbtPlugin("me.lessis" % "bintray-sbt" % "0.3.0") + +libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.25" // Needed by sbt-git