Skip to content

add asDottyDep to sbt plugin #9974

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

Closed
Closed
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
50 changes: 50 additions & 0 deletions sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,56 @@ object DottyPlugin extends AutoPlugin {
else
moduleID
}

/** If this ModuleID cross-version does not match the Scala 3 version provided,
* replace it, else do nothing.
*
* This setting is useful when your build contains dependencies that have
* been published with Scala 3.x, but you would like to use them from Scala 2.x.
*
* If you have:
* {{{
* val dottyVersion = "3.0.0" // or equivalent
* libraryDependencies += "a" %% "b" % "c"
* }}}
* you can replace it by:
* {{{
* libraryDependencies += ("a" %% "b" % "c").asDottyDep(dottyVersion)
* }}}
* This will have no effect when compiling when `scalaVersion` == `dottyVersion`,
* but when compiling
* with Scala 2 this will change the cross-version to a Scala 3.x one. This
* works because Scala 2.13 is currently forward-compatible with Scala 3.x.
*
* NOTE: As a special-case, the cross-version of dotty-library, dotty-compiler and
* dotty will never be rewritten because we know that they're Dotty-only.
* This makes it possible to do something like:
* {{{
* libraryDependencies ~= (_.map(_.withDottyCompat(scalaVersion.value)))
* }}}
*/
def asDottyDep(scalaVersion: String): ModuleID = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a simpler implementation by using the cross operation? https://www.scala-sbt.org/1.x/docs/Cross-Build.html#More+about+using+cross-built+libraries

val name = moduleID.name
if (name != "dotty" && name != "dotty-library" && name != "dotty-compiler")
moduleID.crossVersion match {
case binary: librarymanagement.Binary =>
val compatVersion =
CrossVersion.partialVersion(scalaVersion) match {
case Some((m @ (3 | 0), x)) =>
s"$m.$x"
case _ =>
""
}
if (compatVersion.nonEmpty)
moduleID.cross(CrossVersion.constant(binary.prefix + compatVersion + binary.suffix))
else
moduleID
case _ =>
moduleID
}
else
moduleID
}
}
}

Expand Down