Skip to content

Commit

Permalink
report scalaCompilerBridgeBinaryJar in ScalaData for sbt >= 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
unkarjedy committed Oct 30, 2023
1 parent 92940f8 commit c3ab505
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sbt.jetbrains

import sbt.{Def, File, Task, TaskKey}

object keysAdapterEx {
val myScalaCompilerBridgeBinaryJar: Def.Initialize[Task[Option[File]]] = Def.taskDyn {
Def.task {
None
}
}
}
11 changes: 11 additions & 0 deletions extractor/src/main/scala-sbt-1.2/org.jetbrains/keysAdapterEx.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sbt.jetbrains

import sbt.{Def, File, Task, TaskKey}

object keysAdapterEx {
val myScalaCompilerBridgeBinaryJar: Def.Initialize[Task[Option[File]]] = Def.taskDyn {
Def.task {
None
}
}
}
10 changes: 10 additions & 0 deletions extractor/src/main/scala-sbt-1.3/sbt/jetbrains/keysAdapterEx.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sbt.jetbrains

import sbt.{Def, File, Task, TaskKey}

object keysAdapterEx {
//NOTE: sbt.Keys.scalaCompilerBridgeBinaryJar exists since SBT 1.2.3, so we detect it only since 1.3.0
val myScalaCompilerBridgeBinaryJar: Def.Initialize[Task[Option[File]]] = Def.taskDyn {
sbt.Keys.scalaCompilerBridgeBinaryJar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package extractors

import org.jetbrains.sbt.structure._
import sbt.Def.Initialize
import sbt.{Configuration => _, _}
import sbt.jetbrains.keysAdapterEx
import sbt.{Def, File, Configuration => _, _}

import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
// don't remove this import: sbt.jetbrains.apiAdapter._ -- it shadows some symbols for sbt 1.0 compatibility
import sbt.jetbrains.apiAdapter._

/**
* @author Nikolay Obedin
* @since 4/10/15.
*/
* @author Nikolay Obedin
* @since 4/10/15.
*/
class ProjectExtractor(
projectRef: ProjectRef,
name: String,
Expand All @@ -33,6 +34,7 @@ class ProjectExtractor(
ideOutputDirectory: sbt.Configuration => Option[File],
scalaOrganization: String,
scalaInstance: Option[ScalaInstance],
scalaCompilerBridgeBinaryJar: Option[File],
scalacOptions: Seq[String],
javaHome: Option[File],
javacOptions: Seq[String],
Expand Down Expand Up @@ -117,61 +119,61 @@ class ProjectExtractor(
}

/**
* [[sbt.internal.inc.ScalaInstance]] has different structure in different sbt versions (0.13, 1.0, 1.3, 1.5)<br>
* We need to convert it to our internal representation [[org.jetbrains.sbt.structure.ScalaData]]
* which reflects structure of the latest sbt version (1.5.x)<br>
* To do this there are two options:
* 1. cross-publish sbt-structure-extractor plugin to more then 1 version: 0.13, 1.0, 1.3, 1.5
* and move some methods to [[sbt.jetbrains.apiAdapter]]
* 1. use reflection
*
* We use reflection approach to
* 1. easy project configuration
* 1. decrease Scala Plugin size (we need to bundle all versions of sbt plugin)
*
* Structure of ScalaInstance in different sbt versions: {{{
* //in sbt 0.13.x
* class ScalaInstance(
* ...
* val libraryJar: File,
* val compilerJar: File,
* val extraJars: Seq[File],
* ...
* )
*
* //in zinc-classpath 1.0.0
* class ScalaInstance(
* ...
* val libraryJar: File,
* val compilerJar: File,
* val allJars: Array[File],
* ...
* )
*
* //in zinc-classpath 1.3.0
* class ScalaInstance(
* ...
* val libraryJars: Array[File],
* val compilerJar: File,
* val allJars: Array[File],
* ...
* )
*
* //in zinc-classpath 1.5.0
* class ScalaInstance(
* ...
* val libraryJars: Array[File],
* val compilerJars: Array[File],
* val allJars: Array[File],
* ...
* )
* }}}
*
* @note before `libraryJars: Array[File]` was introduced `libraryJar` contained single `scala-library.jar`.<br>
* Since Scala 3.0 it can contain extra `scala3-library_3.jar`.
* @note before `compilerJars: Array[File]` was introduced `allJars` contained all compiler jars
* @see SCL-19086
*/
* [[sbt.internal.inc.ScalaInstance]] has different structure in different sbt versions (0.13, 1.0, 1.3, 1.5)<br>
* We need to convert it to our internal representation [[org.jetbrains.sbt.structure.ScalaData]]
* which reflects structure of the latest sbt version (1.5.x)<br>
* To do this there are two options:
* 1. cross-publish sbt-structure-extractor plugin to more then 1 version: 0.13, 1.0, 1.3, 1.5
* and move some methods to [[sbt.jetbrains.apiAdapter]]
* 1. use reflection
*
* We use reflection approach to
* 1. easy project configuration
* 1. decrease Scala Plugin size (we need to bundle all versions of sbt plugin)
*
* Structure of ScalaInstance in different sbt versions: {{{
* //in sbt 0.13.x
* class ScalaInstance(
* ...
* val libraryJar: File,
* val compilerJar: File,
* val extraJars: Seq[File],
* ...
* )
*
* //in zinc-classpath 1.0.0
* class ScalaInstance(
* ...
* val libraryJar: File,
* val compilerJar: File,
* val allJars: Array[File],
* ...
* )
*
* //in zinc-classpath 1.3.0
* class ScalaInstance(
* ...
* val libraryJars: Array[File],
* val compilerJar: File,
* val allJars: Array[File],
* ...
* )
*
* //in zinc-classpath 1.5.0
* class ScalaInstance(
* ...
* val libraryJars: Array[File],
* val compilerJars: Array[File],
* val allJars: Array[File],
* ...
* )
* }}}
*
* @note before `libraryJars: Array[File]` was introduced `libraryJar` contained single `scala-library.jar`.<br>
* Since Scala 3.0 it can contain extra `scala3-library_3.jar`.
* @note before `compilerJars: Array[File]` was introduced `allJars` contained all compiler jars
* @see SCL-19086
*/
private def extractScala: Option[ScalaData] = scalaInstance.map { instance =>
def normalize(files: Seq[File]): Seq[File] =
files
Expand All @@ -197,6 +199,7 @@ class ProjectExtractor(
libraryJars,
compilerJars,
extraJars,
scalaCompilerBridgeBinaryJar,
scalacOptions
)
}
Expand Down Expand Up @@ -297,11 +300,12 @@ object ProjectExtractor extends SbtStateOps with TaskOps {
settingInConfiguration(Keys.unmanagedResourceDirectories)

Def.task {

val scalaOrganization =
Keys.scalaOrganization.in(projectRef, Compile).value
val scalaInstance =
taskInCompile(Keys.scalaInstance).onlyIf(options.download).value
val scalaCompilerBridgeBinaryJar =
keysAdapterEx.myScalaCompilerBridgeBinaryJar.value
val scalacOptions =
taskInCompile(Keys.scalacOptions).onlyIf(options.download).value
val javacOptions =
Expand Down Expand Up @@ -334,6 +338,7 @@ object ProjectExtractor extends SbtStateOps with TaskOps {
ideOutputDirectory,
scalaOrganization,
scalaInstance,
scalaCompilerBridgeBinaryJar,
scalacOptions.getOrElse(Seq.empty),
javaHome,
javacOptions.getOrElse(Seq.empty),
Expand All @@ -348,5 +353,4 @@ object ProjectExtractor extends SbtStateOps with TaskOps {
).extract
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jetbrains.sbt.structure

import org.scalatest.funsuite.AnyFunSuiteLike
import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper

import java.io.File
import scala.xml.PrettyPrinter

class DataSerializersTest extends AnyFunSuiteLike {
test("scalaDataSerializer") {
val data = ScalaData(
"myOrg",
"1.2.3",
Seq(new File("a/b/c").getAbsoluteFile),
Seq(new File("a/b/c").getAbsoluteFile),
Seq(new File("a/b/c").getAbsoluteFile),
Some(new File("a/b/c").getAbsoluteFile),
Seq("opt1", "opt2")
)

val elem = scalaDataSerializer.serialize(data)
val dataDeserialized = scalaDataSerializer.deserialize(elem)
dataDeserialized shouldBe Right(data)
}
}
30 changes: 17 additions & 13 deletions shared/src/main/scala/org/jetbrains/sbt/structure/data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,23 @@ case class DirectoryData(file: File, managed: Boolean)
case class JavaData(home: Option[File], options: Seq[String])

/**
* Analog of `sbt.internal.inc.ScalaInstance`
*
* @param libraryJars contains scala-library.jar and (in case of Scala 3) scala3-library_3.jar
* @param compilerJars contains all jars required to instantiate scala compiler<br>
* (except for library jars, which should also be included when creating a compiler instance)
* @param extraJars other jars, usually contain jars required to run ScalaDoc
*/
case class ScalaData(organization: String,
version: String,
libraryJars: Seq[File],
compilerJars: Seq[File],
extraJars: Seq[File],
options: Seq[String]) {
* Analog of `sbt.internal.inc.ScalaInstance`
*
* @param libraryJars contains scala-library.jar and (in case of Scala 3) scala3-library_3.jar
* @param compilerJars contains all jars required to instantiate scala compiler<br>
* (except for library jars, which should also be included when creating a compiler instance)
* @param extraJars other jars, usually contain jars required to run ScalaDoc
* @param compilerBridgeBinaryJar scala3-compiler-bridge_3.x.jar, extracted from `sbt.Keys.scalaCompilerBridgeBinaryJar`
*/
case class ScalaData(
organization: String,
version: String,
libraryJars: Seq[File],
compilerJars: Seq[File],
extraJars: Seq[File],
compilerBridgeBinaryJar: Option[File],
options: Seq[String]
) {
def allJars: Seq[File] = libraryJars ++ compilerJars ++ extraJars
def allCompilerJars: Seq[File] = libraryJars ++ compilerJars
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ trait DataSerializers {
<compilerJars> { what.compilerJars.map { jar => <jar>{jar.path}</jar> }} </compilerJars>
<extraJars> { what.extraJars.map { jar => <jar>{jar.path}</jar> }} </extraJars>

{ what.compilerBridgeBinaryJar.toSeq.map { jar => <compilerBridgeBinaryJar>{jar.path}</compilerBridgeBinaryJar>} }

{ what.options.map { option => <option>{option.canonIfFile}</option> }}
</scala>

Expand All @@ -172,6 +174,7 @@ trait DataSerializers {
val libraryJars = (what \ "libraryJars" \ "jar").map(_.text.file)
val compilerJars = (what \ "compilerJars"\ "jar").map(_.text.file)
val extraJars = (what \ "extraJars"\ "jar").map(_.text.file)
val compilerBridgeBinaryJar = (what \ "compilerBridgeBinaryJar").headOption.map(_.text.file)

val options = (what \ "option").map(o => o.text.canonIfFile)
Right(ScalaData(
Expand All @@ -180,6 +183,7 @@ trait DataSerializers {
libraryJars,
compilerJars,
extraJars,
compilerBridgeBinaryJar,
options
))
}
Expand Down

0 comments on commit c3ab505

Please sign in to comment.