Skip to content

Commit

Permalink
Merge pull request #175 from eed3si9n/wip/exclude3
Browse files Browse the repository at this point in the history
protobufExcludeFilters
  • Loading branch information
eed3si9n authored Mar 5, 2024
2 parents 2e7a80a + 6ba2e22 commit ae22aa1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/main/scala/sbtprotobuf/ProtobufPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sbtprotobuf

import sbt._
import Keys._
import sbt.Defaults.{collectFiles, packageTaskSettings}
import sbt.Defaults.packageTaskSettings
import java.io.File
import com.github.os72.protocjar

Expand All @@ -24,6 +24,9 @@ class ScopedProtobufPlugin(configuration: Configuration, private[sbtprotobuf] va
val protobufConfig = ProtobufConfig

val protobufIncludePaths = taskKey[Seq[File]]("The paths that contain *.proto dependencies.")
val protobufSources = taskKey[Seq[File]]("Protobuf files")
val protobufIncludeFilters = settingKey[Seq[Glob]]("Include filters")
val protobufExcludeFilters = settingKey[Seq[Glob]]("Exclude filters")
val protobufUseSystemProtoc = settingKey[Boolean]("Use the protoc installed on the machine.")
val protobufProtoc = settingKey[String]("The path+name of the protoc executable if protobufUseSystemProtoc is enabled.")
val protobufRunProtoc = taskKey[Seq[String] => Int]("A function that executes the protobuf compiler with the given arguments, returning the exit code of the compilation run.")
Expand All @@ -46,13 +49,43 @@ class ScopedProtobufPlugin(configuration: Configuration, private[sbtprotobuf] va
override lazy val globalSettings: Seq[Setting[_]] = Seq(
protobufUseSystemProtoc := false,
protobufGeneratedTargets := Nil,
protobufProtocOptions := Nil
protobufProtocOptions := Nil,
protobufIncludeFilters := Nil,
protobufExcludeFilters := Nil,
)

override lazy val projectSettings: Seq[Setting[_]] = inConfig(ProtobufConfig)(Seq[Setting[_]](
sourceDirectory := { (configuration / sourceDirectory).value / "protobuf" },
sourceDirectories := (sourceDirectory.value :: Nil),
includeFilter := "*.proto",
protobufSources := {
val dirs = sourceDirectories.value
val includes = protobufIncludeFilters.value
val excludes = protobufExcludeFilters.value
dirs.flatMap { dir =>
val allFiles = (dir ** "*").get.map(_.toPath())
allFiles
.filter(x => includes.exists(_.matches(x)))
.filterNot(x => excludes.exists(_.matches(x)))
.map(_.toFile())
}
},
protobufIncludeFilters ++= {
val dirs = sourceDirectories.value
dirs.map(d => Glob(d.toPath()) / "**" / "*.proto")
},
protobufExcludeFilters ++= {
val dirs = sourceDirectories.value
val excludes = excludeFilter.value
excludes match {
case NothingFilter | HiddenFileFilter => Nil
case f: ExactFilter => dirs.map(d => Glob(d.toPath()) / f.matchName)
case filter => sys.error(s"unsupported excludeFilter $filter. migrate to ProtobufConfig / protobufExcludeFilters")
}
},
protobufExcludeFilters ++= {
val dirs = sourceDirectories.value
dirs.map(d => Glob(d.toPath()) / "google" / "protobuf" / "*.proto")
},
javaSource := { (configuration / sourceManaged).value / "compiled_protobuf" },
protobufExternalIncludePath := (target.value / "protobuf_external"),
protobufProtoc := "protoc",
Expand Down Expand Up @@ -87,8 +120,7 @@ class ScopedProtobufPlugin(configuration: Configuration, private[sbtprotobuf] va
protobufIncludePaths := ((ProtobufConfig / sourceDirectory).value :: Nil),
protobufIncludePaths += protobufExternalIncludePath.value,

protobufGenerate := sourceGeneratorTask.dependsOn(protobufUnpackDependencies).value

protobufGenerate := sourceGeneratorTask.dependsOn(protobufUnpackDependencies).value,
)) ++ inConfig(ProtobufConfig)(
packageTaskSettings(protobufPackage, packageProtoMappings)
) ++ Seq[Setting[_]](
Expand All @@ -112,15 +144,22 @@ class ScopedProtobufPlugin(configuration: Configuration, private[sbtprotobuf] va
throw new RuntimeException("error occurred while compiling protobuf files: %s" format(e.getMessage), e)
}

private[this] def compile(protocCommand: Seq[String] => Int, schemas: Set[File], includePaths: Seq[File], protocOptions: Seq[String], generatedTargets: Seq[(File, String)], log: Logger) = {
private[this] def compile(
protocCommand: Seq[String] => Int,
schemas: Set[File],
includePaths: Seq[File],
protocOptions: Seq[String],
generatedTargets: Seq[(File, String)],
log: Logger,
) = {
val generatedTargetDirs = generatedTargets.map(_._1)
generatedTargetDirs.foreach{ targetDir =>
IO.delete(targetDir)
targetDir.mkdirs()
}

if(!schemas.isEmpty){
log.info("Compiling %d protobuf files to %s".format(schemas.size, generatedTargetDirs.mkString(",")))
log.info("compiling %d protobuf files to %s".format(schemas.size, generatedTargetDirs.mkString(",")))
log.debug("protoc options:")
protocOptions.map("\t"+_).foreach(log.debug(_))
schemas.foreach(schema => log.info("Compiling schema %s" format schema))
Expand Down Expand Up @@ -152,8 +191,7 @@ class ScopedProtobufPlugin(configuration: Configuration, private[sbtprotobuf] va
private[this] def sourceGeneratorTask =
Def.task {
val out = streams.value
val schemas = collectFiles(ProtobufConfig / sourceDirectories, ProtobufConfig / includeFilter, ProtobufConfig / excludeFilter)
.value.toSet[File].map(_.getAbsoluteFile)
val schemas = protobufSources.value.toSet[File].map(_.getAbsoluteFile)
// Include Scala binary version like "_2.11" for cross building.
val cacheFile = out.cacheDirectory / s"protobuf_${scalaBinaryVersion.value}"
val runProtoc = protobufRunProtoc.value
Expand All @@ -178,9 +216,10 @@ class ScopedProtobufPlugin(configuration: Configuration, private[sbtprotobuf] va
UnpackedDependencies(extractTarget, extractedFiles)
}

private[this] def packageProtoMappings = Def.task {
collectFiles(ProtobufConfig / sourceDirectories, ProtobufConfig / includeFilter, ProtobufConfig / excludeFilter)
.value.map(f => (f, f.getName))
}

private[this] def packageProtoMappings =
Def.task {
protobufSources.value.map {
case x => (x, x.getName)
}
}
}
20 changes: 20 additions & 0 deletions src/sbt-test/sbt-protobuf/exclude/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enablePlugins(ProtobufPlugin)
version := "0.1.0-SNAPSHOT"
name := "exclude-test"
scalaVersion := "2.13.13"

libraryDependencies ++= Seq(
"com.google.protobuf" % "protobuf-java" % (ProtobufConfig / version).value % ProtobufConfig,
)

ProtobufConfig / sourceDirectories += (ProtobufConfig / protobufExternalIncludePath).value

TaskKey[Unit]("checkJar") := {
val jar = (Compile / packageBin).value
IO.withTemporaryDirectory{ dir =>
val files = IO.unzip(jar, dir)
val expect = Set(dir / "META-INF" / "MANIFEST.MF")
assert(files == expect, s"""actual = $files,
expected = $expect""")
}
}
7 changes: 7 additions & 0 deletions src/sbt-test/sbt-protobuf/exclude/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("com.github.sbt" % "sbt-protobuf" % pluginVersion)
}
1 change: 1 addition & 0 deletions src/sbt-test/sbt-protobuf/exclude/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> checkJar
13 changes: 13 additions & 0 deletions version.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// So that publishLocal doesn't continuously create new versions
def versionFmt(out: sbtdynver.GitDescribeOutput): String = {
val snapshotSuffix =
if (out.isSnapshot()) "-SNAPSHOT"
else ""
out.ref.dropPrefix + snapshotSuffix
}
def fallbackVersion(d: java.util.Date): String = s"HEAD-${sbtdynver.DynVer timestamp d}"
ThisBuild / version := dynverGitDescribeOutput.value.mkVersion(versionFmt, fallbackVersion(dynverCurrentDate.value))
ThisBuild / dynver := {
val d = new java.util.Date
sbtdynver.DynVer.getGitDescribeOutput(d).mkVersion(versionFmt, fallbackVersion(d))
}

0 comments on commit ae22aa1

Please sign in to comment.