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

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

Merged
merged 2 commits into from
May 14, 2020
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
@@ -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