-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to relocate/shade in assembly
- Loading branch information
Showing
4 changed files
with
135 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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 | ||
|
||
object Shaderr { | ||
def shadeDirectory( | ||
rules: Seq[ShadeRule], | ||
dir: Path, | ||
mappings: Seq[(Path, String)], | ||
verbose: Boolean | ||
): Unit = { | ||
val inputStreams = mappings.filter(x => !Files.isDirectory(x._1)).map(x => Files.newInputStream(x._1) -> x._2) | ||
val result = shadeInputStreams(rules, inputStreams, verbose) | ||
mappings.foreach(f => Files.delete(f._1)) | ||
result.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 | ||
} | ||
case ShadePattern.Zap(patterns) => | ||
patterns.map { pattern => | ||
val jrule = new Zap() | ||
jrule.setPattern(pattern) | ||
jrule | ||
} | ||
case ShadePattern.Keep(patterns) => | ||
patterns.map { pattern => | ||
val jrule = new Keep() | ||
jrule.setPattern(pattern) | ||
jrule | ||
} | ||
case _ => Nil | ||
} | ||
} | ||
|
||
val proc = new JJProcessor(jjrules, verbose, true, null) | ||
|
||
/* | ||
jarjar MisplacedClassProcessor class transforms byte[] to a class using org.objectweb.asm.ClassReader.getClassName | ||
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 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 | ||
if (proc.process(entry)) Some(new ByteArrayInputStream(entry.data) -> entry.name) | ||
else None | ||
} | ||
val excludes = proc.getExcludes | ||
shadedInputStreams.filterNot(mapping => excludes.contains(mapping._2)) | ||
} | ||
} | ||
|
||
sealed trait ShadePattern { | ||
def inAll: ShadeRule = ShadeRule(this, Vector(ShadeTarget.inAll)) | ||
def inProject: ShadeRule = ShadeRule(this, Vector(ShadeTarget.inProject)) | ||
def inModuleCoordinates(moduleId: ModuleCoordinate*): ShadeRule = | ||
ShadeRule(this, moduleId.toVector map ShadeTarget.inModuleCoordinate) | ||
} | ||
|
||
object ShadePattern { | ||
case class Rename(patterns: List[(String, String)]) extends ShadePattern | ||
case class Zap(patterns: List[String]) extends ShadePattern | ||
case class Keep(patterns: List[String]) extends ShadePattern | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters