Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for shading over InputStream #3

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.filterNot(_._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