Skip to content
New issue

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

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

Already on GitHub? # to your account

In-memory shading #39

Merged
merged 12 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ lazy val core = project

Compile / scalacOptions += "-deprecation"
Compile / scalacOptions ++= {
if (scalaVersion.value.startsWith("2.12.")) Vector("-Xlint", "-Xfatal-warnings")
if (scalaVersion.value.startsWith("2.13.")) Vector("-Xlint", "-Xsource:3")
else if (scalaVersion.value.startsWith("2.12.")) Vector("-Xlint", "-Xfatal-warnings")
else Vector("-Xlint")
}
})
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/com/eed3si9n/jarjar/JJProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class JJProcessor(
val renamer: String => Option[String] = {
val wildcards = PatternElement.createWildcards(ruleList.asJava).asScala

value: String => {
(value: String) => {
val result = wildcards.flatMap { wc =>
val slashed = value.replace('.', '/') // The jarjar wildcards expect slashes instead of dots
// Hack to replace the package object name.
Expand Down
25 changes: 15 additions & 10 deletions core/src/main/scala/com/eed3si9n/jarjarabrams/Main.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.eed3si9n.jarjarabrams

import com.eed3si9n.jarjar.{ MainUtil, RulesFileParser }
import java.nio.file.Path;
import scala.collection.JavaConverters._
import com.eed3si9n.jarjar.MainUtil
import java.nio.file.Path

class Main {
def help(): Unit = Main.printHelp()
Expand All @@ -11,14 +10,20 @@ class Main {
if (rulesFile == null || inJar == null || outJar == null) {
throw new IllegalArgumentException("rulesFile, inJar, and outJar are required");
}
val rules = RulesFileParser
.parse(rulesFile.toFile)
.asScala
.toList
.map(Shader.toShadeRule)
val verbose = java.lang.Boolean.getBoolean("verbose")
val skipManifest = java.lang.Boolean.getBoolean("skipManifest");
Shader.shadeFile(rules, inJar, outJar, verbose, skipManifest)
val skipManifest = java.lang.Boolean.getBoolean("skipManifest")
val resetTimestamp = sys.props.get("resetTimestamp") match {
case Some(_) => java.lang.Boolean.getBoolean("resetTimestamp")
case None => true
}
Shader.shadeFile(
Shader.parseRulesFile(rulesFile),
inJar,
outJar,
verbose,
skipManifest,
resetTimestamp
)
}
}

Expand Down
32 changes: 12 additions & 20 deletions core/src/main/scala/com/eed3si9n/jarjarabrams/Shader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.eed3si9n.jarjarabrams
import java.nio.file.{ Files, Path, StandardOpenOption }
import com.eed3si9n.jarjar.{ JJProcessor, _ }
import com.eed3si9n.jarjar.util.EntryStruct
import Zip.{ createDirectories, resetModifiedTime }
import Zip.createDirectories
import scala.collection.JavaConverters._

object Shader {
Expand All @@ -12,27 +12,16 @@ object Shader {
inputJar: Path,
outputJar: Path,
verbose: Boolean,
skipManifest: Boolean
skipManifest: Boolean,
resetTimestamp: Boolean
): Unit = {
val tempDir = Files.createTempDirectory("jarjar-in")
val outDir = Files.createTempDirectory("jarjar-out")
val tempJar = Files.createTempFile("jarjar", ".jar")
Zip.unzip(inputJar, tempDir)
shadeDirectory(
rules,
outDir,
makeMappings(tempDir),
verbose = verbose,
skipManifest = skipManifest
)
Zip.zip(makeMappings(outDir), tempJar)
resetModifiedTime(tempJar)
if (Files.exists(outputJar)) {
Files.delete(outputJar)
val shader = bytecodeShader(rules, verbose, skipManifest)
Zip.transformJarFile(inputJar, outputJar, resetTimestamp) { struct0 =>
shader(struct0.data, struct0.name).map {
case (shadedBytes, shadedName) =>
Zip.entryStruct(shadedName, struct0.time, shadedBytes, struct0.skipTransform)
}
}
createDirectories(outputJar.getParent)
Files.copy(tempJar, outputJar)
resetModifiedTime(outputJar)
}

def makeMappings(dir: Path): List[(Path, String)] =
Expand Down Expand Up @@ -133,6 +122,9 @@ object Shader {
None
}

def parseRulesFile(rulesFile: Path): List[ShadeRule] =
RulesFileParser.parse(rulesFile.toFile()).asScala.map(toShadeRule).toList

def toShadeRule(rule: PatternElement): ShadeRule =
rule match {
case r: Rule =>
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/scala/com/eed3si9n/jarjarabrams/Using.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import java.io.{
OutputStream
}
import java.nio.file.{ Files, Path }
import java.security.{ MessageDigest, DigestInputStream }
import java.util.jar.{ JarFile, JarOutputStream }
import java.util.zip.ZipInputStream
import scala.util.control.NonFatal
import scala.reflect.ClassTag
Expand All @@ -30,10 +32,14 @@ abstract class Using[Source, A] {
object Using {
def fileOutputStream(append: Boolean): Using[Path, OutputStream] =
file(f => new BufferedOutputStream(new FileOutputStream(f.toFile, append)))
val jarOutputStream: Using[Path, JarOutputStream] =
file(f => new JarOutputStream(new BufferedOutputStream(new FileOutputStream(f.toFile))))
val fileInputStream: Using[Path, InputStream] =
file(f => new BufferedInputStream(new FileInputStream(f.toFile)))

val jarFile: Using[Path, JarFile] =
file(f => new JarFile(f.toFile))
val zipInputStream = wrap((in: InputStream) => new ZipInputStream(in))
def digestInputStream(digest: MessageDigest) = wrap((in: InputStream) => new DigestInputStream(in, digest))

def file[A1 <: AutoCloseable](action: Path => A1): Using[Path, A1] =
file(action, closeCloseable)
Expand Down
Loading