Skip to content

Commit

Permalink
Add support for shading over InputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
joan38 committed Aug 17, 2020
1 parent e6bc7a6 commit a543a49
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 30 deletions.
43 changes: 43 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db

# Editor Files #
################
*~
*.swp

# Build output directories #
############################
target
/build
/bin
*/bin
/classes

# IntelliJ specific files/directories #
#######################################
out
.idea
!.idea/runConfigurations
*.ipr
*.iws
*.iml
atlassian-ide-plugin.xml

# Eclipse specific files/directories #
######################################
.classpath
.project
.settings
.metadata

# NetBeans specific files/directories #
#######################################
.nbattrs

# BSP
.bsp
68 changes: 38 additions & 30 deletions core/src/main/scala/com/eed3si9n/jarjarabrams/Shader.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eed3si9n.jarjarabrams

import java.io.{ByteArrayInputStream, InputStream}
import java.nio.file.{ Files, Path, StandardOpenOption }
import org.pantsbuild.jarjar.{ JJProcessor, _ }
import org.pantsbuild.jarjar.util.EntryStruct
Expand All @@ -11,29 +12,41 @@ object Shader {
mappings: Seq[(Path, String)],
verbose: Boolean
): Unit = {
val jjrules = rules flatMap { r =>
val inputStreams = mappings.filter(x => !Files.isDirectory(x._1)).map(x => Files.newInputStream(x._1) -> x._2)
val newMappings = shadeInputStreams(rules, inputStreams, verbose)
mappings.sortBy(_._1.toFile.isDirectory).foreach(f => Files.delete(f._1))
newMappings.foreach { case (inputStream, mapping) =>
val out = dir.resolve(mapping)
if (!Files.exists(out.getParent)) Files.createDirectories(out.getParent)
Files.write(out, inputStream.readAllBytes(), StandardOpenOption.CREATE)
}
}

def shadeInputStreams(
rules: Seq[ShadeRule],
mappings: Seq[(InputStream, String)],
verbose: Boolean
): Seq[(InputStream, String)] = {
val jjrules = rules.flatMap { r =>
r.shadePattern match {
case ShadePattern.Rename(patterns) =>
patterns.map {
case (from, to) =>
val jrule = new Rule()
jrule.setPattern(from)
jrule.setResult(to)
jrule
patterns.map { case (from, to) =>
val jrule = new Rule()
jrule.setPattern(from)
jrule.setResult(to)
jrule
}
case ShadePattern.Zap(patterns) =>
patterns.map {
case pattern =>
val jrule = new Zap()
jrule.setPattern(pattern)
jrule
patterns.map { pattern =>
val jrule = new Zap()
jrule.setPattern(pattern)
jrule
}
case ShadePattern.Keep(patterns) =>
patterns.map {
case pattern =>
val jrule = new Keep()
jrule.setPattern(pattern)
jrule
patterns.map { pattern =>
val jrule = new Keep()
jrule.setPattern(pattern)
jrule
}
case _ => Nil
}
Expand All @@ -46,24 +59,19 @@ object Shader {
which always translates class names containing '.' into '/', regardless of OS platform.
We need to transform any windows file paths in order for jarjar to match them properly and not omit them.
*/
val files = mappings.map(f => if (f._2.contains('\\')) (f._1, f._2.replace('\\', '/')) else f)
val entry = new EntryStruct
files.filter(x => !Files.isDirectory(x._1)) foreach { f =>
entry.data = Files.readAllBytes(f._1)
val sanitizedMappings =
mappings.map(f => if (f._2.contains('\\')) (f._1, f._2.replace('\\', '/')) else f)
val shadedInputStreams = sanitizedMappings.flatMap { f =>
val entry = new EntryStruct
entry.data = f._1.readAllBytes()
entry.name = f._2
entry.time = -1
entry.skipTransform = false
Files.delete(f._1)
if (proc.process(entry)) {
val out = dir.resolve(entry.name)
if (!Files.exists(out.getParent)) {
Files.createDirectories(out.getParent)
}
Files.write(out, entry.data, StandardOpenOption.CREATE)
}
if (proc.process(entry)) Some(new ByteArrayInputStream(entry.data) -> entry.name)
else None
}
val excludes = proc.getExcludes
excludes.foreach(exclude => Files.delete(dir.resolve(exclude)))
shadedInputStreams.filterNot(mapping => excludes.contains(mapping._2))
}
}

Expand Down

0 comments on commit a543a49

Please # to comment.