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

Support several scalafixScalaBinaryVersion within a build #380

Merged
merged 11 commits into from
Dec 19, 2023
81 changes: 47 additions & 34 deletions src/main/scala/scalafix/internal/sbt/ScalafixInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,47 +116,60 @@ object ScalafixInterface {
private lazy val _value = scala.util.Try(thunk())
override def apply(): T = _value.get
}
private val fromToolClasspathMemo: scala.collection.mutable.HashMap[
zarthross marked this conversation as resolved.
Show resolved Hide resolved
(String, Seq[ModuleID], Seq[Repository]),
ScalafixInterface
] = scala.collection.mutable.HashMap.empty

def fromToolClasspath(
scalafixBinaryScalaVersion: String,
scalafixDependencies: Seq[ModuleID],
scalafixCustomResolvers: Seq[Repository],
logger: Logger = ConsoleLogger(System.out)
): () => ScalafixInterface =
new LazyValue({ () =>
if (scalafixBinaryScalaVersion.startsWith("3"))
logger.error(
"To use Scalafix on Scala 3 projects, you must unset `scalafixBinaryScalaVersion`. " +
"Rules such as ExplicitResultTypes requiring the project version to match the Scalafix " +
"version are unsupported for the moment."
)
else if (scalafixBinaryScalaVersion == "2.11")
logger.error(
"Scala 2.11 is no longer supported. Please downgrade to the final version supporting " +
"it: sbt-scalafix 0.10.4."
)
val callback = new ScalafixLogger(logger)
val scalafixArguments = ScalafixAPI
.fetchAndClassloadInstance(
fromToolClasspathMemo.getOrElseUpdate(
bjaglin marked this conversation as resolved.
Show resolved Hide resolved
(
scalafixBinaryScalaVersion,
scalafixCustomResolvers.asJava
)
.newArguments()
.withMainCallback(callback)
val printStream =
new PrintStream(
LoggingOutputStream(
logger,
Level.Info
)
)
new ScalafixInterface(scalafixArguments, Nil)
.withArgs(
Arg.PrintStream(printStream),
Arg.ToolClasspath(
Nil,
scalafixDependencies,
scalafixCustomResolvers
)
)
scalafixDependencies,
scalafixCustomResolvers
), {
if (scalafixBinaryScalaVersion.startsWith("3"))
logger.error(
"To use Scalafix on Scala 3 projects, you must unset `scalafixBinaryScalaVersion`. " +
"Rules such as ExplicitResultTypes requiring the project version to match the Scalafix " +
"version are unsupported for the moment."
)
else if (scalafixBinaryScalaVersion == "2.11")
logger.error(
"Scala 2.11 is no longer supported. Please downgrade to the final version supporting " +
"it: sbt-scalafix 0.10.4."
)
val callback = new ScalafixLogger(logger)
val scalafixArguments = ScalafixAPI
.fetchAndClassloadInstance(
scalafixBinaryScalaVersion,
scalafixCustomResolvers.asJava
)
.newArguments()
.withMainCallback(callback)
val printStream =
new PrintStream(
LoggingOutputStream(
logger,
Level.Info
)
)
new ScalafixInterface(scalafixArguments, Nil)
.withArgs(
Arg.PrintStream(printStream),
Arg.ToolClasspath(
Nil,
scalafixDependencies,
scalafixCustomResolvers
)
)
}
)
})
}