From f7a112c59a54a4bed754af26021cb6b43da24bb1 Mon Sep 17 00:00:00 2001 From: Andrzej Ressel Date: Sun, 19 May 2024 20:39:11 +0200 Subject: [PATCH] Implement preferScala3 Equivalent of preferScala2 but for Scala 3 --- .../scala/org/jetbrains/sbt/CreateTasks.scala | 2 +- .../sbt/extractors/UtilityTasks.scala | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/extractor/src/main/scala/org/jetbrains/sbt/CreateTasks.scala b/extractor/src/main/scala/org/jetbrains/sbt/CreateTasks.scala index 220374e..d73d259 100644 --- a/extractor/src/main/scala/org/jetbrains/sbt/CreateTasks.scala +++ b/extractor/src/main/scala/org/jetbrains/sbt/CreateTasks.scala @@ -11,7 +11,7 @@ import sbt.jetbrains.apiAdapter._ object CreateTasks extends (State => State) with SbtStateOps { lazy val globalSettings: Seq[Setting[_]] = Seq[Setting[_]]( - Keys.commands += UtilityTasks.preferScala2, + Keys.commands ++= Seq(UtilityTasks.preferScala2, UtilityTasks.preferScala3), StructureKeys.sbtStructureOpts := StructureKeys.sbtStructureOptions.apply(Options.readFromString).value, StructureKeys.dumpStructure := UtilityTasks.dumpStructure.value, StructureKeys.acceptedProjects := UtilityTasks.acceptedProjects.value, diff --git a/extractor/src/main/scala/org/jetbrains/sbt/extractors/UtilityTasks.scala b/extractor/src/main/scala/org/jetbrains/sbt/extractors/UtilityTasks.scala index 7f463f6..f6bab54 100644 --- a/extractor/src/main/scala/org/jetbrains/sbt/extractors/UtilityTasks.scala +++ b/extractor/src/main/scala/org/jetbrains/sbt/extractors/UtilityTasks.scala @@ -47,6 +47,26 @@ object UtilityTasks extends SbtStateOps { } } + lazy val preferScala3: Command = Command.command("preferScala3") { state => + val (structure, data) = { + val extracted = Project.extract(state) + (extracted.structure, extracted.structure.data) + } + + val crossScala3VersionsInScala3Projects = structure.allProjectRefs.flatMap { project => + Keys.scalaVersion.in(project).get(data) + .filter(_.startsWith("2.")) + .map(_ => Keys.crossScalaVersions.in(project).get(data).getOrElse(Seq.empty).filter(_.startsWith("3."))) + .getOrElse(Seq.empty) + } + + if (crossScala3VersionsInScala3Projects.nonEmpty) { + "++" + crossScala3VersionsInScala3Projects.maxBy(numbersOf) :: state + } else { + state + } + } + private def numbersOf(version: String): (Int, Int, Int) = { val prefix = version.split('.').filter(s => s.nonEmpty && s.forall(_.isDigit)).map(_.toInt).take(3) val xs = prefix ++ Seq.fill(3 - prefix.length)(0)