Skip to content

Commit 537203c

Browse files
committed
Implement scalafmt-dynamic downloader using sbt
Reduce dependency to scalafmt-dynamic-core, which does not depend on own version of coursier.
1 parent dfbff78 commit 537203c

3 files changed

Lines changed: 82 additions & 8 deletions

File tree

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ lazy val plugin = project.enablePlugins(SbtPlugin, ScriptedPlugin).settings(
6363
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
6464
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion cross
6565
CrossVersion.for3Use2_13,
66-
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion cross
66+
"org.scalameta" %% "scalafmt-dynamic-core" % scalafmtVersion cross
6767
CrossVersion.for3Use2_13,
6868
),
6969
scriptedBufferLog := false,

plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import java.io.OutputStreamWriter
44
import java.nio.file.{Files, Path}
55

66
import sbt.Keys.*
7-
import sbt.librarymanagement.MavenRepository
7+
import sbt.librarymanagement as lm
88
import sbt.util.CacheImplicits.*
99
import sbt.util.{CacheStoreFactory, FileInfo, Level}
1010
import sbt.{given, *}
@@ -102,7 +102,7 @@ object ScalafmtPlugin extends AutoPlugin {
102102
private type Output = String
103103
private type InitTask = Def.Initialize[Task[Unit]]
104104

105-
val globalInstance = Scalafmt.create(this.getClass.getClassLoader)
105+
private val globalInstance = Scalafmt.create(this.getClass.getClassLoader)
106106
.withRespectProjectFilters(true)
107107

108108
private object FilterMode {
@@ -133,6 +133,8 @@ object ScalafmtPlugin extends AutoPlugin {
133133
currentProject: ResolvedProject,
134134
filterMode: String,
135135
errorHandling: ErrorHandling,
136+
dependencyResolution: lm.DependencyResolution,
137+
updateConfiguration: lm.UpdateConfiguration,
136138
) {
137139
locally {
138140
def cmp[A: Ordering](x: Iterable[A], y: Iterable[A]): Int = Ordering
@@ -162,8 +164,10 @@ object ScalafmtPlugin extends AutoPlugin {
162164
errorHandling,
163165
)
164166

165-
private val scalafmtSession = {
166-
val repositories = resolvers.collect { case r: MavenRepository => r.root }
167+
private def getFallbackInstance = {
168+
val repositories = resolvers.collect { case r: lm.MavenRepository =>
169+
r.root
170+
}
167171
val repoCredentials = credentials.flatMap { c =>
168172
Try(CredentialsUtils.toDirect(c)).toOption
169173
.map(dc => new RepositoryCredential(dc.host, dc.userName, dc.passwd))
@@ -175,10 +179,21 @@ object ScalafmtPlugin extends AutoPlugin {
175179
info.mkString("Adding credentials [", ",", "]")
176180
}
177181

178-
val scalafmtSession = globalInstance.withReporter(reporter)
179-
.withMavenRepositories(repositories*)
182+
globalInstance.withMavenRepositories(repositories*)
180183
.withRepositoryCredentials(repoCredentials*)
181-
.createSession(config.toAbsolutePath)
184+
}
185+
186+
private val scalafmtSession = {
187+
val factory = new ScalafmtSbtDependencyDownloader(
188+
taskStreams,
189+
dependencyResolution,
190+
updateConfiguration,
191+
)
192+
193+
val withFactory = globalInstance.withRepositoryPackageDownloader(factory)
194+
val scalafmtSession = {
195+
if (withFactory ne globalInstance) withFactory else getFallbackInstance
196+
}.withReporter(reporter).createSession(config.toAbsolutePath)
182197
if (scalafmtSession == null) throw messageException(
183198
"failed to create formatting session. Please report bug to https://github.com/scalameta/sbt-scalafmt",
184199
)
@@ -473,6 +488,8 @@ object ScalafmtPlugin extends AutoPlugin {
473488
!noThrow && scalafmtFailOnErrors.value,
474489
scalafmtDetailedError.value,
475490
),
491+
dependencyResolution.value,
492+
updateConfiguration.value,
476493
)
477494
func(files, dirs, session)
478495
}
@@ -519,6 +536,8 @@ object ScalafmtPlugin extends AutoPlugin {
519536
scalafmtFailOnErrors.value,
520537
scalafmtDetailedError.value,
521538
),
539+
dependencyResolution.value,
540+
updateConfiguration.value,
522541
).formatSources(absFiles, Nil)
523542
},
524543
))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.scalafmt.sbt
2+
3+
import java.io.File
4+
import java.lang as jl
5+
6+
import sbt.Keys.TaskStreams
7+
import sbt.internal.librarymanagement.InternalDefaults.{
8+
modulePrefixTemp, sbtOrgTemp,
9+
}
10+
import sbt.{librarymanagement as lm, richUpdateReport}
11+
12+
import org.scalafmt.CompatCollections.JavaConverters.*
13+
import org.scalafmt.interfaces.*
14+
15+
class ScalafmtSbtDependencyDownloader(
16+
taskStreams: TaskStreams,
17+
dependencyResolution: lm.DependencyResolution,
18+
updateConfiguration: lm.UpdateConfiguration,
19+
) extends RepositoryPackageDownloaderFactory with RepositoryPackageDownloader {
20+
override def create(
21+
reporter: ScalafmtReporter,
22+
properties: RepositoryProperties,
23+
): RepositoryPackageDownloader = this
24+
25+
override def download(
26+
scalaVersion: String,
27+
scalafmtVersion: String,
28+
reporter: ScalafmtReporter,
29+
dependencies: jl.Iterable[RepositoryPackage],
30+
): jl.Iterable[File] = {
31+
val name = s"${modulePrefixTemp}scalafmt-${scalafmtVersion}_$scalaVersion"
32+
val moduleId = lm.ModuleID(sbtOrgTemp, name, "1.0")
33+
val scalaModule = lm.ScalaModuleInfo(
34+
scalaFullVersion = scalaVersion,
35+
scalaBinaryVersion = lm.CrossVersion.binaryScalaVersion(scalaVersion),
36+
configurations = Vector.empty,
37+
checkExplicit = true,
38+
filterImplicit = true,
39+
overrideScalaVersion = false, // disables autoScalaLibrary
40+
)
41+
val moduleDependencies = dependencies.asScala
42+
.map(d => lm.ModuleID(d.group, d.artifact, d.version)).toVector
43+
val moduleConf = lm
44+
.ModuleDescriptorConfiguration(moduleId, lm.ModuleInfo("sandbox"))
45+
.withDependencies(moduleDependencies).withScalaModuleInfo(scalaModule)
46+
47+
dependencyResolution.update(
48+
dependencyResolution.moduleDescriptor(moduleConf),
49+
updateConfiguration,
50+
lm.UnresolvedWarningConfiguration(),
51+
taskStreams.log,
52+
).fold(x => throw x.resolveException, x => x.allFiles.asJava)
53+
}
54+
55+
}

0 commit comments

Comments
 (0)