Skip to content

Commit

Permalink
add sbt.ConcurrentRestrictions.Tag("scalafmt") + Tags.CPU on scalafmt…
Browse files Browse the repository at this point in the history
… tasks (#101)

* add sbt.ConcurrentRestrictions.Tag("scalafmt") + Tags.CPU on scalafmt tasks

* Scaladoc fixed

Co-authored-by: Mikhail Chugunkov <[email protected]>
  • Loading branch information
cchepelov and poslegm authored May 14, 2020
1 parent bf481bc commit b13325f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.scalafmt.sbt

trait ConcurrentRestrictionTags {
import sbt.ConcurrentRestrictions.Tag

/**
* This tag can be used to control the maximum number of parallel scalafmt tasks in large-scale build trees.
*
* Global / concurrentRestrictions += Tags.limit(org.scalafmt.sbt.ConcurrentRestrictionTags.Scalafmt, 3)
*
* would prevent SBT from spawning more than three simultaneous Scalafmt tasks
*
* @see https://www.scala-sbt.org/1.x/docs/Parallel-Execution.html
*/
val Scalafmt = Tag("scalafmt")
}

object ConcurrentRestrictionTags extends ConcurrentRestrictionTags
38 changes: 27 additions & 11 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import scala.util.Failure
import scala.util.Success
import scala.util.Try
import org.scalafmt.interfaces.{Scalafmt, ScalafmtSessionFactory}
import sbt.ConcurrentRestrictions.Tag
import sbt.librarymanagement.MavenRepository

object ScalafmtPlugin extends AutoPlugin {
Expand All @@ -25,6 +26,9 @@ object ScalafmtPlugin extends AutoPlugin {
object autoImport {
val scalafmt = taskKey[Unit]("Format Scala sources with scalafmt.")

private[sbt] val ScalafmtTagPack =
Seq(ConcurrentRestrictionTags.Scalafmt, Tags.CPU)

@deprecated("Use scalafmt instead.", "2.0.0")
val scalafmtIncremental = taskKey[Unit](
"Format Scala sources to be compiled incrementally with scalafmt (alias to scalafmt)."
Expand Down Expand Up @@ -317,8 +321,8 @@ object ScalafmtPlugin extends AutoPlugin {
}
}

lazy val scalafmtConfigSettings: Seq[Def.Setting[_]] = Seq(
scalafmt := {
private def scalafmtTask =
Def.task {
formatSources(
streams.value.cacheStoreFactory,
(unmanagedSources in scalafmt).value,
Expand All @@ -327,9 +331,10 @@ object ScalafmtPlugin extends AutoPlugin {
outputStreamWriter(streams.value),
fullResolvers.value
)
},
scalafmtIncremental := scalafmt.value,
scalafmtSbt := {
} tag (ScalafmtTagPack: _*)

private def scalafmtSbtTask =
Def.task {
formatSources(
sbtSources.value.toSet,
sbtConfig.value,
Expand All @@ -344,8 +349,10 @@ object ScalafmtPlugin extends AutoPlugin {
outputStreamWriter(streams.value),
fullResolvers.value
)
},
scalafmtCheck := {
} tag (ScalafmtTagPack: _*)

private def scalafmtCheckTask =
Def.task {
val analysis = checkSources(
(scalafmt / streams).value.cacheStoreFactory,
(unmanagedSources in scalafmt).value,
Expand All @@ -355,8 +362,10 @@ object ScalafmtPlugin extends AutoPlugin {
fullResolvers.value
)
trueOrBoom(analysis)
},
scalafmtSbtCheck := {
} tag (ScalafmtTagPack: _*)

private def scalafmtSbtCheckTask =
Def.task {
trueOrBoom(
checkSources(
sbtSources.value,
Expand All @@ -375,10 +384,17 @@ object ScalafmtPlugin extends AutoPlugin {
fullResolvers.value
)
)
},
} tag (ScalafmtTagPack: _*)

lazy val scalafmtConfigSettings: Seq[Def.Setting[_]] = Seq(
scalafmt := scalafmtTask.value,
scalafmtIncremental := scalafmt.value,
scalafmtSbt := scalafmtSbtTask.value,
scalafmtCheck := scalafmtCheckTask.value,
scalafmtSbtCheck := scalafmtSbtCheckTask.value,
scalafmtDoFormatOnCompile := Def.settingDyn {
if (scalafmtOnCompile.value) {
scalafmt in resolvedScoped.value.scope
(scalafmt in resolvedScoped.value.scope)
} else {
Def.task(())
}
Expand Down

0 comments on commit b13325f

Please sign in to comment.