Skip to content

Commit

Permalink
Add package-scoped options and manifest attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Jan 31, 2021
1 parent 3b697e1 commit 7c9595a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
)
Expand Down
38 changes: 36 additions & 2 deletions project/BuildHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
}
)
}

Expand All @@ -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)): _*
)

Expand Down

0 comments on commit 7c9595a

Please sign in to comment.