Skip to content

Commit

Permalink
Build for Scala 2.13.0-M5
Browse files Browse the repository at this point in the history
See #540
  • Loading branch information
thesamet committed Mar 24, 2019
1 parent 63b1def commit f535362
Show file tree
Hide file tree
Showing 126 changed files with 1,684 additions and 1,391 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ stages:
scala:
- 2.11.12
- 2.12.8
- 2.13.0-M5

env:
- TEST_SCRIPT=e2e.sh E2E_SHADED=0
Expand Down
54 changes: 30 additions & 24 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ val Scala211 = "2.11.12"

val Scala212 = "2.12.8"

val Scala213 = "2.13.0-M5"

val protobufVersion = "3.7.0"

val scalacheckVersion = "1.14.0"
Expand All @@ -18,11 +20,11 @@ val grpcVersion = "1.19.0"

val MimaPreviousVersion = "0.9.0-RC1"

scalaVersion in ThisBuild := Scala212
scalaVersion in ThisBuild := Scala213

crossScalaVersions in ThisBuild := Seq(Scala211, Scala212)
crossScalaVersions in ThisBuild := Seq(Scala211, Scala212, Scala213)

scalacOptions in ThisBuild ++= {
scalacOptions in ThisBuild ++= "-deprecation" :: {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, v)) if v <= 11 => List("-target:jvm-1.7")
case _ => Nil
Expand Down Expand Up @@ -56,7 +58,7 @@ releaseProcess := Seq[ReleaseStep](
commitReleaseVersion,
tagRelease,
releaseStepCommandAndRemaining(";+publishSigned"),
releaseStepCommandAndRemaining(s";++${Scala211};runtimeNative/publishSigned;lensesNative/publishSigned"),
// releaseStepCommandAndRemaining(s";++${Scala211};runtimeNative/publishSigned;lensesNative/publishSigned"),
setNextVersion,
commitNextVersion,
pushChanges,
Expand Down Expand Up @@ -99,14 +101,23 @@ lazy val runtime = crossProject(JSPlatform, JVMPlatform/*, NativePlatform*/)
"commons-codec" % "commons-codec" % "1.12" % "test",
"com.google.protobuf" % "protobuf-java-util" % protobufVersion % "test",
),
unmanagedSourceDirectories in Compile ++= {
val base = (baseDirectory in LocalRootProject).value / "scalapb-runtime" / "shared" / "src" / "main"
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, v)) if v < 13 =>
Seq(base / "scala-pre-2.13")
case _ =>
Nil
}
},
testFrameworks += new TestFramework("utest.runner.Framework"),
unmanagedResourceDirectories in Compile += baseDirectory.value / "../../protobuf",
mimaPreviousArtifacts := Set("com.thesamet.scalapb" %% "scalapb-runtime" % MimaPreviousVersion),
mimaBinaryIssueFilters ++= {
import com.typesafe.tools.mima.core._
Seq(
)
}
},
)
.dependsOn(lenses)
.platformsSettings(JSPlatform/*, NativePlatform*/)(
Expand Down Expand Up @@ -162,7 +173,7 @@ shadeTarget in ThisBuild := s"scalapbshade.v${version.value.replaceAll("[.-]","_

lazy val compilerPlugin = project.in(file("compiler-plugin"))
.settings(
crossScalaVersions := Seq(Scala210, Scala211, Scala212),
crossScalaVersions := Seq(Scala210, Scala211, Scala212, Scala213),
sourceGenerators in Compile += Def.task {
val file = (sourceManaged in Compile).value / "scalapb" / "compiler" / "Version.scala"
IO.write(file,
Expand Down Expand Up @@ -202,7 +213,7 @@ lazy val compilerPluginShaded = project.in(file("compiler-plugin-shaded"))
.dependsOn(compilerPlugin)
.settings(
name := "compilerplugin-shaded",
crossScalaVersions := Seq(Scala210, Scala211, Scala212),
crossScalaVersions := Seq(Scala210, Scala211, Scala212, Scala213),
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("scalapb.options.Scalapb**" -> shadeTarget.value).inProject,
ShadeRule.rename("com.google.**" -> shadeTarget.value).inAll
Expand Down Expand Up @@ -280,30 +291,23 @@ createVersionFile := {
log.info(s"Created $f2")
}


lazy val lenses = crossProject(JSPlatform, JVMPlatform/*, NativePlatform*/).in(file("lenses"))
.settings(
name := "lenses",
sources in Test := {
unmanagedSourceDirectories in Compile ++= {
val base = (baseDirectory in LocalRootProject).value / "lenses" / "shared" / "src" / "main"
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 13)) =>
// TODO utest_2.13.0-M3
Nil
case Some((2, v)) if v < 13 =>
Seq(base / "scala-pre-2.13")
case _ =>
(sources in Test).value
},
},
testFrameworks += new TestFramework("utest.runner.Framework"),
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 13)) =>
// TODO utest_2.13.0-M3
Nil
case _ =>
Seq(
"com.lihaoyi" %%% "utest" % "0.6.6" % "test"
)
}
},
testFrameworks += new TestFramework("utest.runner.Framework"),
libraryDependencies ++= Seq(
"com.lihaoyi" %%% "utest" % "0.6.6" % "test"
),
mimaPreviousArtifacts := Set("com.thesamet.scalapb" %% "lenses" % MimaPreviousVersion),
mimaBinaryIssueFilters ++= {
import com.typesafe.tools.mima.core._
Expand Down Expand Up @@ -332,9 +336,11 @@ lazy val lensesJS = lenses.js
lazy val docs = project.in(file("docs"))
.enablePlugins(MicrositesPlugin, ScalaUnidocPlugin)
.settings(
scalaVersion := Scala212,
crossScalaVersions := Seq(Scala212),
libraryDependencies ++= Seq(
"com.thesamet.scalapb" %% "scalapb-json4s" % "0.7.2",
),
),
micrositeName := "ScalaPB",
micrositeCompilingDocsTool := WithMdoc,
mdocIn := baseDirectory.value / "src" / "main" / "markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
val sealedOneof = for {
file <- files
message <- file.allMessages if message.isSealedOneofType
} yield SealedOneof(message, message.getOneofs.get(0).getFields.asScala.map(_.getMessageType))
} yield
SealedOneof(
message,
message.getOneofs.get(0).getFields.asScala.map(_.getMessageType).toVector
)
new SealedOneofsCache(sealedOneof)
}

Expand Down Expand Up @@ -243,9 +247,15 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
if (isSingular) EnclosingType.None
else if (supportsPresence || fd.isInOneof) EnclosingType.ScalaOption
else {
EnclosingType.Collection
EnclosingType.Collection(collectionType)
}

def fieldMapEnclosingType: EnclosingType =
if (isSingular) EnclosingType.None
else if (supportsPresence || fd.isInOneof) EnclosingType.ScalaOption
else if (!fd.isMapField) EnclosingType.Collection(collectionType)
else EnclosingType.Collection(ScalaSeq)

def isMapField = isMessage && fd.isRepeated && fd.getMessageType.isMapEntry

def mapType: MessageDescriptorPimp#MapType = {
Expand Down Expand Up @@ -293,7 +303,7 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
def fieldsMapEmptyCollection: String = {
require(fd.isRepeated)
if (fd.isMapField) s"$ScalaSeq.empty"
else s"${collectionType}.empty"
else emptyCollection
}

def scalaTypeName: String =
Expand Down Expand Up @@ -443,7 +453,7 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {

implicit class MessageDescriptorPimp(val message: Descriptor) {

def fields = message.getFields.asScala.filter(_.getLiteType != FieldType.GROUP)
def fields = message.getFields.asScala.filter(_.getLiteType != FieldType.GROUP).toSeq

def fieldsWithoutOneofs = fields.filterNot(_.isInOneof)

Expand Down Expand Up @@ -707,10 +717,10 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
else s"${enum.getContainingType.scalaTypeName}.scalaDescriptor.enums(${enum.getIndex})"

def baseTraitExtends: Seq[String] =
"_root_.scalapb.GeneratedEnum" +: scalaOptions.getExtendsList.asScala
"_root_.scalapb.GeneratedEnum" +: scalaOptions.getExtendsList.asScala.toSeq

def companionExtends: Seq[String] =
s"_root_.scalapb.GeneratedEnumCompanion[${nameSymbol}]" +: scalaOptions.getCompanionExtendsList.asScala
s"_root_.scalapb.GeneratedEnumCompanion[${nameSymbol}]" +: scalaOptions.getCompanionExtendsList.asScala.toSeq

def sourcePath: Seq[Int] = {
if (enum.isTopLevel) Seq(FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER, enum.getIndex)
Expand All @@ -735,13 +745,16 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
enumValue.getOptions.getExtension[EnumValueOptions](Scalapb.enumValue)

def valueExtends: Seq[String] =
enumValue.getType.nameSymbol +: scalaOptions.getExtendsList.asScala
enumValue.getType.nameSymbol +: scalaOptions.getExtendsList.asScala.toSeq

def isName = {
Helper.makeUniqueNames(
enumValue.getType.getValues.asScala.sortBy(v => (v.getNumber, v.getName)).map { e =>
e -> ("is" + allCapsToCamelCase(e.getName, true))
}
enumValue.getType.getValues.asScala
.sortBy(v => (v.getNumber, v.getName))
.map { e =>
e -> ("is" + allCapsToCamelCase(e.getName, true))
}
.toSeq
)(enumValue)
}

Expand Down Expand Up @@ -796,7 +809,7 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
private def scalaPackageParts: Seq[String] = {
val requestedPackageName: Seq[String] =
(if (scalaOptions.hasPackageName) scalaOptions.getPackageName.split('.')
else javaPackage.split('.')).filterNot(_.isEmpty)
else javaPackage.split('.')).toIndexedSeq.filterNot(_.isEmpty)

if (scalaOptions.getFlatPackage || (params.flatPackage && !isNonFlatDependency))
requestedPackageName
Expand Down Expand Up @@ -905,10 +918,11 @@ class DescriptorImplicits(params: GeneratorParams, files: Seq[FileDescriptor]) {
}

private[scalapb] object DescriptorImplicits {
val ScalaSeq = "_root_.scala.collection.Seq"
val ScalaMap = "_root_.scala.collection.immutable.Map"
val ScalaVector = "_root_.scala.collection.immutable.Vector"
val ScalaOption = "_root_.scala.Option"
val ScalaSeq = "_root_.scala.Seq"
val ScalaMap = "_root_.scala.collection.immutable.Map"
val ScalaVector = "_root_.scala.collection.immutable.Vector"
val ScalaIterable = "_root_.scala.collection.immutable.Iterable"
val ScalaOption = "_root_.scala.Option"
}

object Helper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package scalapb.compiler
import scalapb.compiler.EnclosingType.Collection

sealed trait Expression extends Product with Serializable {
def andThen(other: Expression) = (this, other) match {
Expand Down Expand Up @@ -50,35 +51,46 @@ object ExpressionBuilder {
case OperatorApplication(name) :: tail => s"${runSingleton(tail)(e)} $name"
}

def convertCollection(expr: String, enclosingType: EnclosingType): String = {
val convert = List(enclosingType match {
case Collection(DescriptorImplicits.ScalaVector) => MethodApplication("toVector")
case Collection(DescriptorImplicits.ScalaSeq) => MethodApplication("toSeq")
case Collection(DescriptorImplicits.ScalaMap) => MethodApplication("toMap")
case Collection(DescriptorImplicits.ScalaIterable) => MethodApplication("toIterable")
case Collection(cc) => FunctionApplication("_root_.scalapb.Utils.convertTo")
case _ => Identity
})
runSingleton(convert)(expr)
}

def runCollection(
es: List[LiteralExpression]
)(e: String, enclosingType: EnclosingType, mustCopy: Boolean): String = {
)(e0: String, enclosingType: EnclosingType, mustCopy: Boolean): String = {
require(enclosingType != EnclosingType.None)
val nontrivial = es.filterNot(_.isIdentity)
val nontrivial: List[LiteralExpression] = es.filterNot(_.isIdentity)
val needVariable =
nontrivial
.filterNot(_.isIdentity)
.dropRight(1)
.exists(_.isFunctionApplication)

val convert =
if (enclosingType == EnclosingType.Collection)
"(_root_.scala.collection.breakOut)"
else ""
val e = if (enclosingType.isInstanceOf[Collection]) {
e0 + ".iterator"
} else e0

if (needVariable)
s"""$e.map(__e => ${runSingleton(nontrivial)("__e")})$convert"""
convertCollection(s"""$e.map(__e => ${runSingleton(nontrivial)("__e")})""", enclosingType)
else if (nontrivial.nonEmpty) {
val f = nontrivial match {
case List(FunctionApplication(name)) =>
name
case _ =>
runSingleton(nontrivial)("_")
}
s"""$e.map($f)$convert"""
convertCollection(s"""$e.map($f)""", enclosingType)
} else if (mustCopy) {
s"""$e.map(_root_.scala.Predef.identity)$convert"""
} else e
convertCollection(s"""$e.map(_root_.scala.Predef.identity)""", enclosingType)
} else e0
}

def run(
Expand All @@ -103,5 +115,8 @@ sealed trait EnclosingType
object EnclosingType {
case object None extends EnclosingType
case object ScalaOption extends EnclosingType
case object Collection extends EnclosingType

/** Indicates that the result should be a collection with type constructor cc, such as List, Map.
*/
case class Collection(cc: String) extends EnclosingType
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ case class FunctionalPrinter(content: Vector[String] = Vector.empty, indentLevel

// Strips the margin, splits lines and adds.
def addStringMargin(s: String): FunctionalPrinter =
add(s.stripMargin.split("\n", -1): _*)
add(s.stripMargin.split("\n", -1).toSeq: _*)

// Adds the strings, while putting a delimiter between two lines.
def addWithDelimiter(delimiter: String)(s: Seq[String]) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ final class GrpcServicePrinter(service: ServiceDescriptor, implicits: Descriptor
}

private[this] val stub: PrinterEndo = {
val methods = service.getMethods.asScala.map(clientMethodImpl(_, false))
val methods = service.getMethods.asScala.map(clientMethodImpl(_, false)).toSeq
stubImplementation(service.stub, service.name, methods)
}

Expand Down
Loading

0 comments on commit f535362

Please sign in to comment.