Skip to content

Commit

Permalink
passing configured repositories to scalafmt instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Slivka committed Dec 9, 2019
1 parent b8815ce commit 5163dae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lazy val plugin = project
.settings(
moduleName := "sbt-scalafmt",
libraryDependencies ++= List(
"org.scalameta" %% "scalafmt-dynamic" % "2.2.2"
"org.scalameta" %% "scalafmt-dynamic" % "2.3.2"
),
scriptedBufferLog := false,
scriptedLaunchOpts += s"-Dplugin.version=${version.value}"
Expand Down
92 changes: 59 additions & 33 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
import sbt.librarymanagement.MavenRepository

object ScalafmtPlugin extends AutoPlugin {
override def trigger: PluginTrigger = allRequirements
Expand Down Expand Up @@ -94,12 +95,23 @@ object ScalafmtPlugin extends AutoPlugin {
sources: Seq[File],
config: Path,
log: Logger,
writer: OutputStreamWriter
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
)(
onFormat: (File, Input, Output) => T
): Seq[Option[T]] = {
val reporter = new ScalafmtSbtReporter(log, writer)
val scalafmtInstance = globalInstance.withReporter(reporter)
val repositories = resolvers.collect {
case r: MavenRepository => r.root
}
val scalafmtInstance =
globalInstance
.withReporter(reporter)
.withMavenRepositories(repositories: _*)

log.debug(
s"Adding repositories ${repositories.mkString("[", ",", "]")}"
)
sources
.map { file =>
val input = IO.read(file)
Expand All @@ -118,7 +130,8 @@ object ScalafmtPlugin extends AutoPlugin {
sources: Seq[File],
config: Path,
log: Logger,
writer: OutputStreamWriter
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
): Unit = {
trackSourcesAndConfig(cacheStoreFactory, sources, config) {
(outDiff, configChanged, prev) =>
Expand All @@ -133,7 +146,7 @@ object ScalafmtPlugin extends AutoPlugin {
}
if (filesToFormat.nonEmpty) {
log.info(s"Formatting ${filesToFormat.size} Scala sources...")
formatSources(filesToFormat, config, log, writer)
formatSources(filesToFormat, config, log, writer, resolvers)
}
ScalafmtAnalysis(Set.empty)
}
Expand All @@ -143,18 +156,20 @@ object ScalafmtPlugin extends AutoPlugin {
sources: Set[File],
config: Path,
log: Logger,
writer: OutputStreamWriter
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
): Unit = {
val cnt = withFormattedSources(sources.toSeq, config, log, writer)(
(file, input, output) => {
if (input != output) {
IO.write(file, output)
1
} else {
0
val cnt =
withFormattedSources(sources.toSeq, config, log, writer, resolvers)(
(file, input, output) => {
if (input != output) {
IO.write(file, output)
1
} else {
0
}
}
}
).flatten.sum
).flatten.sum

if (cnt > 1) {
log.info(s"Reformatted $cnt Scala sources")
Expand All @@ -167,7 +182,8 @@ object ScalafmtPlugin extends AutoPlugin {
sources: Seq[File],
config: Path,
log: Logger,
writer: OutputStreamWriter
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
): ScalafmtAnalysis = {
trackSourcesAndConfig(cacheStoreFactory, sources, config) {
(outDiff, configChanged, prev) =>
Expand All @@ -180,7 +196,8 @@ object ScalafmtPlugin extends AutoPlugin {
if (configChanged) Set.empty
else prev.failedScalafmtCheck & outDiff.unmodified
prevFailed foreach { warnBadFormat(_, log) }
val result = checkSources(filesToCheck.toSeq, config, log, writer)
val result =
checkSources(filesToCheck.toSeq, config, log, writer, resolvers)
prev.copy(
failedScalafmtCheck = result.failedScalafmtCheck | prevFailed
)
Expand All @@ -205,20 +222,22 @@ object ScalafmtPlugin extends AutoPlugin {
sources: Seq[File],
config: Path,
log: Logger,
writer: OutputStreamWriter
writer: OutputStreamWriter,
resolvers: Seq[Resolver]
): ScalafmtAnalysis = {
if (sources.nonEmpty) {
log.info(s"Checking ${sources.size} Scala sources...")
}
val unformatted = withFormattedSources(sources, config, log, writer)(
(file, input, output) => {
val diff = input != output
if (diff) {
warnBadFormat(file, log)
Some(file)
} else None
}
).flatten.flatten.toSet
val unformatted =
withFormattedSources(sources, config, log, writer, resolvers)(
(file, input, output) => {
val diff = input != output
if (diff) {
warnBadFormat(file, log)
Some(file)
} else None
}
).flatten.flatten.toSet
ScalafmtAnalysis(failedScalafmtCheck = unformatted)
}

Expand Down Expand Up @@ -294,7 +313,8 @@ object ScalafmtPlugin extends AutoPlugin {
(unmanagedSources in scalafmt).value,
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
},
scalafmtIncremental := scalafmt.value,
Expand All @@ -303,13 +323,15 @@ object ScalafmtPlugin extends AutoPlugin {
sbtSources.value.toSet,
sbtConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
formatSources(
metabuildSources.value.toSet,
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
},
scalafmtCheck := {
Expand All @@ -318,7 +340,8 @@ object ScalafmtPlugin extends AutoPlugin {
(unmanagedSources in scalafmt).value,
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
trueOrBoom(analysis)
},
Expand All @@ -328,15 +351,17 @@ object ScalafmtPlugin extends AutoPlugin {
sbtSources.value,
sbtConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
)
trueOrBoom(
checkSources(
metabuildSources.value,
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
)
},
Expand Down Expand Up @@ -366,7 +391,8 @@ object ScalafmtPlugin extends AutoPlugin {
absFiles.toSet,
scalaConfig.value,
streams.value.log,
outputStreamWriter(streams.value)
outputStreamWriter(streams.value),
resolvers.value
)
}
)
Expand Down

0 comments on commit 5163dae

Please sign in to comment.