Skip to content

Commit

Permalink
add validEntries setting (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Mar 19, 2021
1 parent 895e7f7 commit 554d880
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/scala/coursier/ShadingPlugin.scala
Expand Up @@ -89,6 +89,7 @@ object ShadingPlugin extends AutoPlugin {
val shadedModules = settingKey[Set[OrganizationArtifactName]]("")
val shadingRules = taskKey[Seq[Rule]]("")
val validNamespaces = taskKey[Set[String]]("")
val validEntries = taskKey[Set[String]]("")

// to be set optionally by users
val shadingVerbose = taskKey[Boolean]("")
Expand Down Expand Up @@ -120,14 +121,12 @@ object ShadingPlugin extends AutoPlugin {
(modId.organization, transformName(modId.name))
}

private def onlyNamespaces(prefixes: Set[String], jar: File, println: String => Unit): Unit = {
private def onlyNamespaces(isValid: String => Boolean, jar: File, println: String => Unit): Unit = {
val zf = new ZipFile(jar)
val unrecognized = zf.entries()
.asScala
.map(_.getName)
.filter { n =>
!n.startsWith("META-INF/") && !prefixes.exists(n.startsWith)
}
.filter(!isValid(_))
.toVector
.sorted
for (u <- unrecognized)
Expand All @@ -140,6 +139,7 @@ object ShadingPlugin extends AutoPlugin {
shadedModules := Set.empty,
shadingRules := Nil,
validNamespaces := Set.empty,
validEntries := Set.empty,

shadingVerbose := false,

Expand Down Expand Up @@ -202,6 +202,7 @@ object ShadingPlugin extends AutoPlugin {
val rules = shadingRules.value
val shadedJars0 = shadedJars.value
val validPrefixes = validNamespaces.value.map(_.replace('.', '/') + "/")
val validEntries = autoImport.validEntries.value
val orig = packageBin.in(Compile).value
val dest = orig.getParentFile / s"${orig.getName.stripSuffix(".jar")}-shading.jar"
if (!dest.exists() || dest.lastModified() < orig.lastModified()) {
Expand All @@ -210,7 +211,11 @@ object ShadingPlugin extends AutoPlugin {
val processor = JJProcessor(rules, verbose, false)
CoursierJarProcessor.run((orig +: shadedJars0).toArray, dest, processor.proc, true)
}
onlyNamespaces(validPrefixes, dest, log.error(_))
def isValid(entry: String): Boolean =
validEntries.contains(entry) ||
entry.startsWith("META-INF/") ||
validPrefixes.exists(entry.startsWith)
onlyNamespaces(isValid, dest, log.error(_))
dest
},

Expand Down
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-shading/valid-entries/build.sbt
@@ -0,0 +1,2 @@
enablePlugins(ShadingPlugin)
validEntries += "NOTICE"
11 changes: 11 additions & 0 deletions src/sbt-test/sbt-shading/valid-entries/project/plugins.sbt
@@ -0,0 +1,11 @@
{
val pluginVersion = sys.props.getOrElse(
"plugin.version",
throw new RuntimeException(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
)

addSbtPlugin("io.get-coursier" % "sbt-shading" % pluginVersion)
}
Empty file.
1 change: 1 addition & 0 deletions src/sbt-test/sbt-shading/valid-entries/test
@@ -0,0 +1 @@
> publishLocal

0 comments on commit 554d880

Please sign in to comment.