From 7c9595a9a875d53b949b592d7ece2748162d839d Mon Sep 17 00:00:00 2001 From: Nadav Samet Date: Sat, 30 Jan 2021 16:28:40 -0800 Subject: [PATCH] Add package-scoped options and manifest attribute. See ScalaPB/scalapb-validate#70 --- build.sbt | 3 +++ project/BuildHelpers.scala | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/build.sbt b/build.sbt index c22b29f..396a3bf 100644 --- a/build.sbt +++ b/build.sbt @@ -12,6 +12,7 @@ def commonProtos = ProtosProject( "com.google.api.grpc" % "proto-google-common-protos" % "1.18.1", grpc = true, + protoPackage = "google", buildNumber = 0 ) lazy val commonProtos09 = commonProtos.scalapb09 @@ -21,6 +22,7 @@ lazy val commonProtos11 = commonProtos.scalapb11 val cloudPubSub = ProtosProject( "com.google.api.grpc" % "proto-google-cloud-pubsub-v1" % "1.92.3", grpc = true, + protoPackage = "google", buildNumber = 0 ).dependsOn(commonProtos) lazy val cloudPubSub09 = cloudPubSub.scalapb09 @@ -30,6 +32,7 @@ lazy val cloudPubSub11 = cloudPubSub.scalapb11 val pgvProto = ProtosProject( "io.envoyproxy.protoc-gen-validate" % "pgv-java-stub" % "0.4.1", grpc = false, + protoPackage = "validate", packageName = Some("pgv-proto"), buildNumber = 0 ) diff --git a/project/BuildHelpers.scala b/project/BuildHelpers.scala index d109f35..e4783c7 100644 --- a/project/BuildHelpers.scala +++ b/project/BuildHelpers.scala @@ -15,6 +15,7 @@ object NoProcessLogger extends ProcessLogger { final case class ProtosProject( module: ModuleID, grpc: Boolean, + protoPackage: String, buildNumber: Int = 0, packageName: Option[String] = None, dependencies: Seq[ProtosProject] = Seq.empty @@ -27,6 +28,8 @@ final case class ProtosProject( val projectName = module.name + scalapbMajorMinor.replace('.', '_') + val optionsFile = protoPackage.replace('.', '/') + "/scalapb-options.proto" + sbt .Project(projectName, new File(projectName)) .settings( @@ -67,7 +70,38 @@ final case class ProtosProject( publish / skip := (sys.env .getOrElse("PUBLISH_ONLY", basePackageName) != basePackageName), sonatypeBundleDirectory := (ThisBuild / baseDirectory).value / "target" / "sonatype-staging", - publishArtifact in (Compile, packageDoc) := scalaVersion.value != Scala3 + publishArtifact in (Compile, packageDoc) := scalaVersion.value != Scala3, + Compile / resourceGenerators += Def.task { + + val packageOptionsFile = + (Compile / resourceManaged).value / optionsFile + IO.write( + packageOptionsFile, + s"""|// This file is generated by ScalaPB/common-protos project/BuildHelpers.scala. + |// It is added as an input in user's builds to ensure the protos shipped with + |// this jar are interpreted with the original generator options, and not influenced + |// by generator options in the user's build. + | + |syntax = "proto3"; + |package $protoPackage; + | + |import "scalapb/scalapb.proto"; + |option (scalapb.options) = { + | scope: PACKAGE + | flat_package: false + | lenses: true + | // java_conversions as a file-level option is only introduced in 0.10.11 + | // so it is not yet included here for backwards-compatiblity. + | // java_conversions: true + | preserve_unknown_fields: true + |}; + """.stripMargin + ) + Seq(packageOptionsFile) + }.taskValue, + Compile / packageBin / packageOptions += { + Package.ManifestAttributes("ScalaPB-Options-Proto" -> optionsFile) + } ) } @@ -77,7 +111,7 @@ final case class ProtosProject( ) val scalapb10: Project = - protoProject("0.10.10").dependsOn( + protoProject("0.10.11").dependsOn( dependencies.map(d => ClasspathDependency(d.scalapb10, None)): _* )