Skip to content

Commit

Permalink
Use the new sbt syntax to specify that scala 213 libraries can be use…
Browse files Browse the repository at this point in the history
…d in Scala 3 (#235)
  • Loading branch information
mlachkar authored Jun 8, 2021
1 parent fd91725 commit f096b44
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ case class MigratedLibsImpl(

private def initialLibSameThanCompatible(initialLib: InitialLib, compatible: CompatibleWithScala3): Boolean =
compatible match {
case keptInitialLib: CompatibleWithScala3.KeptInitialLib
case keptInitialLib: CompatibleWithScala3.Lib // maybe need to be fixed
if initialLib.crossVersion == keptInitialLib.crossVersion =>
true
case _ => false
}

def allLibs: Map[InitialLib, MigratedLib] = compatibleWithScala3 ++ uncompatibleWithScala3
}

Expand Down
18 changes: 9 additions & 9 deletions migrate/src/main/scala/migrate/internal/InitialLib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ case class InitialLib(
// look for revisions that are compatible with scala 3 full version
case CrossVersion.Full(_, _) => getCompatibleWhenFullCrossVersion(this)
// already compatible
case CrossVersion.For2_13Use3(_, _) => CompatibleWithScala3Lib.from(this, Reason.IsAlreadyValid)
case CrossVersion.For3Use2_13(_, _) => CompatibleWithScala3Lib.from(this, Reason.IsAlreadyValid)
case CrossVersion.For2_13Use3(_, _) => keepTheSameLib(Reason.IsAlreadyValid)
case CrossVersion.For3Use2_13(_, _) => keepTheSameLib(Reason.IsAlreadyValid)
// For Patch and Constant, we search full compatible scala 3 version
case CrossVersion.Patch =>
CoursierHelper
Expand All @@ -54,17 +54,17 @@ case class InitialLib(
.getOrElse(this.toUncompatible(Reason.FullVersionNotAvailable))
}

def toUncompatible(reason: Reason): UncompatibleWithScala3 =
private def toUncompatible(reason: Reason): UncompatibleWithScala3 =
UncompatibleWithScala3(organization, name, revision, crossVersion, configurations, reason)

def keepTheSameLib(reason: Reason): CompatibleWithScala3.KeptInitialLib =
CompatibleWithScala3.KeptInitialLib(organization, name, revision, crossVersion, configurations, reason)
private def keepTheSameLib(reason: Reason): CompatibleWithScala3.Lib =
CompatibleWithScala3.Lib(organization, name, Seq(revision), crossVersion, configurations, reason)

def for3Use2_13(reason: Reason): CompatibleWithScala3.KeptInitialLib =
CompatibleWithScala3.KeptInitialLib(
private def for3Use2_13(reason: Reason): CompatibleWithScala3.Lib =
CompatibleWithScala3.Lib(
organization,
name,
revision,
Seq(revision),
CrossVersion.For3Use2_13("", ""),
configurations,
reason
Expand Down Expand Up @@ -97,7 +97,7 @@ case class InitialLib(
private def getCompatibleWhenFullCrossVersion(lib: InitialLib): MigratedLibImp =
CoursierHelper.getCompatibleForScala3Full(lib) match {
case Some(_) =>
CompatibleWithScala3.Scala3Lib(
CompatibleWithScala3.Lib(
lib.organization,
Name(lib.name.value),
Seq(lib.revision),
Expand Down
43 changes: 9 additions & 34 deletions migrate/src/main/scala/migrate/internal/MigratedLib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object MigratedLib {
override def toString: String = value.scala3Value
}

case class Scala3Lib(
case class Lib(
organization: Organization,
name: Name,
revisions: Seq[Revision],
Expand All @@ -27,46 +27,19 @@ object MigratedLib {
val revision = revisions.head // we only display the first revision available for Scala3. It's a choice.

override def toString: String = {
val configuration = configurations.map(c => " % " + withQuote(c)).getOrElse("")
val orgQuoted = withQuote(organization.value)
withQuote(name.value)
val configuration = configurations.map(c => " % " + withQuote(c)).getOrElse("")
val orgQuoted = withQuote(organization.value)
val revisionQuoted = withQuote(revision.value)
crossVersion match {
case CrossVersion.Disabled => s"$orgQuoted % ${withQuote(name.value)} % $revisionQuoted$configuration"
case CrossVersion.For2_13Use3(_, _) =>
s"$orgQuoted %% ${withQuote(name.value)} % $revisionQuoted$configuration"
case CrossVersion.For3Use2_13(_, _) =>
s"$orgQuoted %% ${withQuote(name.value)} % $revisionQuoted$configuration cross CrossVersion.for3Use2_13"
case _ => s"$orgQuoted % ${withQuote(name.value)} % $revisionQuoted$configuration"
}
}

private def withQuote(s: String) = "\"" + s + "\""
}

case class KeptInitialLib(
organization: Organization,
name: Name,
revision: Revision,
crossVersion: CrossVersion,
configurations: Option[String],
reason: Reason
) extends CompatibleWithScala3 {

override def toString: String = {
val configuration = configurations.map(c => " % " + withQuote(c)).getOrElse("")
val orgQuoted = withQuote(organization.value)
withQuote(name.value)
val revisionQuoted = withQuote(revision.value)
crossVersion match {
case CrossVersion.Disabled => s"$orgQuoted % ${withQuote(name.value)} % $revisionQuoted$configuration"
case CrossVersion.For2_13Use3(_, _) =>
s"$orgQuoted %% ${withQuote(name.value)} % $revisionQuoted$configuration"
case _ => s"$orgQuoted % ${withQuote(name.value)} % $revisionQuoted$configuration"
}
}

private def withQuote(s: String) = "\"" + s + "\""
}

}

case class UncompatibleWithScala3(
Expand All @@ -78,11 +51,13 @@ object MigratedLib {
reason: Reason
) extends MigratedLibImp {
override def isCompatibleWithScala3: Boolean = false
override def toString: String =
s"${organization.value}:${name.value}:${revision.value}${configurations.map(":" + _).getOrElse("")}"
}

object CompatibleWithScala3Lib {
def from(lib: InitialLib, reason: Reason): CompatibleWithScala3.Scala3Lib =
CompatibleWithScala3.Scala3Lib(
def from(lib: InitialLib, reason: Reason): CompatibleWithScala3.Lib =
CompatibleWithScala3.Lib(
lib.organization,
lib.name,
Seq(lib.revision),
Expand Down
13 changes: 5 additions & 8 deletions migrate/src/main/scala/migrate/utils/CoursierHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ object CoursierHelper {
val scala3Binary = "3"
val scala213Binary = "2.13"

def getCompatibleForScala3Binary(lib: InitialLib): Option[CompatibleWithScala3.Scala3Lib] = {
def getCompatibleForScala3Binary(lib: InitialLib): Option[CompatibleWithScala3.Lib] = {
val revisions = searchRevisionsFor(lib, scala3Binary)
if (revisions.isEmpty) None
else {
val all = CompatibleWithScala3.Scala3Lib(
val all = CompatibleWithScala3.Lib(
lib.organization,
lib.name,
revisions,
Expand All @@ -31,11 +31,11 @@ object CoursierHelper {
Some(getNewerRevision(lib, all))
}
}
def getCompatibleForScala3Full(lib: InitialLib): Option[CompatibleWithScala3.Scala3Lib] = {
def getCompatibleForScala3Full(lib: InitialLib): Option[CompatibleWithScala3.Lib] = {
val revisions = searchRevisionsFor(lib, scala3Full)
if (revisions.isEmpty) None
else {
val all = CompatibleWithScala3.Scala3Lib(
val all = CompatibleWithScala3.Lib(
lib.organization,
Name(lib.name.value + s"_${CoursierHelper.scala3Full}"),
revisions,
Expand Down Expand Up @@ -64,10 +64,7 @@ object CoursierHelper {
}

// Rely on coursier order
private def getNewerRevision(
lib: InitialLib,
compatibleLibs: CompatibleWithScala3.Scala3Lib
): CompatibleWithScala3.Scala3Lib = {
private def getNewerRevision(lib: InitialLib, compatibleLibs: CompatibleWithScala3.Lib): CompatibleWithScala3.Lib = {
val revisions = compatibleLibs.revisions
val index = revisions.zipWithIndex.toMap.get(lib.revision)
index match {
Expand Down
16 changes: 8 additions & 8 deletions migrate/src/test/scala/migrate/MigrateLibsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class MigrateLibsSuite extends AnyFunSuiteLike with DiffAssertions {
val macroLib: InitialLib = InitialLib.from("com.softwaremill.scalamacrodebug:macros:0.4.1", binary, None).get

test("Not available lib") {
Scala3Migrate.migrateLibs(Seq(kind)).allLibs
// val compiler = res.compatibleWithScala3.values
// assert(compiler.nonEmpty)
// assert(compiler.values.head == ScalacOption.Specific3.KindProjector)
val migrated = Scala3Migrate.migrateLibs(Seq(kind)).allLibs
val res = migrated(kind)
assert(res.isCompatibleWithScala3)
assert(res.isInstanceOf[CompatibleWithScala3.ScalacOption])
}
test("Java lib") {
val migrated = Scala3Migrate.migrateLibs(Seq(opentelemetry)).allLibs
Expand All @@ -39,11 +39,11 @@ class MigrateLibsSuite extends AnyFunSuiteLike with DiffAssertions {
val migrated = Scala3Migrate.migrateLibs(Seq(cats)).allLibs
val res = migrated(cats)
assert(res.isCompatibleWithScala3)
assert(res.asInstanceOf[CompatibleWithScala3.Scala3Lib].crossVersion.isInstanceOf[CrossVersion.For2_13Use3])
assert(res.asInstanceOf[CompatibleWithScala3.Lib].crossVersion.isInstanceOf[CrossVersion.For2_13Use3])
}
test("Don't show older version") {
val migrated = Scala3Migrate.migrateLibs(Seq(collection)).allLibs
val res = migrated(collection).asInstanceOf[CompatibleWithScala3.Scala3Lib].revisions
val res = migrated(collection).asInstanceOf[CompatibleWithScala3.Lib].revisions
val revision = Revision("2.3.2")
assert(!res.contains(revision))
}
Expand All @@ -52,7 +52,7 @@ class MigrateLibsSuite extends AnyFunSuiteLike with DiffAssertions {
val res = migrateLib(scalafix)
assert(res.isCompatibleWithScala3)
assert(!isTheSame(scalafix, res))
assert(res.asInstanceOf[CompatibleWithScala3.KeptInitialLib].crossVersion.isInstanceOf[CrossVersion.For3Use2_13])
assert(res.asInstanceOf[CompatibleWithScala3.Lib].crossVersion.isInstanceOf[CrossVersion.For3Use2_13])
}
// Warning: this test may change if the lib is ported to scala 3
test("Not migrated because macro lib") {
Expand All @@ -63,7 +63,7 @@ class MigrateLibsSuite extends AnyFunSuiteLike with DiffAssertions {

private def isTheSame(lib: InitialLib, migrated: MigratedLib) =
migrated match {
case migrated: CompatibleWithScala3.KeptInitialLib =>
case migrated: CompatibleWithScala3.Lib =>
lib.organization == migrated.organization && lib.name == migrated.name && lib.revision == migrated.revision && lib.crossVersion == migrated.crossVersion
case _ => false
}
Expand Down
34 changes: 16 additions & 18 deletions plugin/src/main/scala/migrate/LibsMigration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ private[migrate] object LibsMigration {
val validSign = s"""${BOLD}${CYAN}Valid${RESET}"""
val toBeUpdated = s"""${BOLD}${BLUE}To be updated${RESET}"""

val spacesForLib = computeLongestValue((incompatibleLibs ++ validLibs ++ toUpdate.keys).map(_.toString))
val spacesForLib = computeLongestValue((incompatibleLibs ++ validLibs ++ toUpdate.keys).map(_.toString))
def reasonWhy(lib: MigratedLib) = if (lib.getReasonWhy.isEmpty) "" else s": ${YELLOW}${lib.getReasonWhy}${RESET}"

def formatIncompatibleLibs: String = incompatibleLibs.map { lib =>
s"""${formatValueWithSpace(lib.toString, spacesForLib)} -> $removedSign : ${YELLOW}${lib.getReasonWhy}${RESET}"""
s"""${formatValueWithSpace(lib.toString, spacesForLib)} -> $removedSign ${reasonWhy(lib)}"""
}.mkString("\n")

def formatValid: String =
validLibs
.map(lib =>
s"""${formatValueWithSpace(
lib.toString,
spacesForLib
)} -> $validSign : ${YELLOW}${lib.getReasonWhy}${RESET}"""
)
validLibs.map { lib =>
s"""${formatValueWithSpace(lib.toString, spacesForLib)} -> $validSign ${reasonWhy(lib)}"""
}
.mkString("\n")

def formatLibToUpdate: String =
toUpdate.map { case (initial, migrated) =>
s"""${formatValueWithSpace(initial.toString, spacesForLib)} -> ${BLUE}${migrated.toString}$RESET ${reasonWhy(
migrated
)}"""
}
.mkString("\n")

val spacesForHelp = computeLongestValue(Seq(removedSign, validSign, toBeUpdated))
Expand All @@ -72,17 +78,9 @@ private[migrate] object LibsMigration {
|${formatValueWithSpace(toBeUpdated, spacesForHelp)} $BLUE: Need to be updated to the following version$RESET
|""".stripMargin

Seq(help, formatIncompatibleLibs, formatValid, formatLibToUpdate(toUpdate, spacesForLib))
Seq(help, formatIncompatibleLibs, formatValid, formatLibToUpdate)
.filterNot(_.isEmpty)
.mkString("\n")
}

private def formatLibToUpdate(libs: Map[Lib, MigratedLib], longestValue: Int): String =
libs.map { case (initial, migrated) => format(initial, migrated, longestValue) }
.mkString("\n")

private def format(initial: Lib, migrated: MigratedLib, longestValue: Int): String = {
val numberOfSpaces = " " * (longestValue - initial.toString.length)
s"""$initial$numberOfSpaces -> ${BLUE}${migrated.toString}$RESET : ${YELLOW}${migrated.getReasonWhy}${RESET}"""
}
}

0 comments on commit f096b44

Please sign in to comment.