-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from eed3si9n/wip/cli
Implement drop-in CLI
- Loading branch information
Showing
11 changed files
with
526 additions
and
11 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,82 @@ | ||
package com.eed3si9n.jarjarabrams | ||
|
||
import com.eed3si9n.jarjar.{ MainUtil, RulesFileParser } | ||
import java.nio.file.Path; | ||
import scala.collection.JavaConverters._ | ||
|
||
class Main { | ||
def help(): Unit = Main.printHelp() | ||
|
||
def process(rulesFile: Path, inJar: Path, outJar: Path): Unit = { | ||
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) | ||
} | ||
} | ||
|
||
object Main { | ||
def main(args: Array[String]): Unit = { | ||
MainUtil.runMain(new Main(), args, "help") | ||
} | ||
|
||
def printHelp(): Unit = { | ||
val jarName = "jarjar-abrams-assembly.jar" | ||
val str = s"""Jar Jar Abrams - A utility to repackage and embed Java and Scala libraries | ||
|
||
Command-line usage: | ||
|
||
java -jar $jarName [help] | ||
|
||
Prints this help message. | ||
|
||
java -jar $jarName process <rulesFile> <inJar> <outJar> | ||
|
||
Transform the <inJar> jar file, writing a new jar file to <outJar>. | ||
Any existing file named by <outJar> will be deleted. | ||
|
||
The transformation is defined by a set of rules in the file specified | ||
by the rules argument (see below). | ||
|
||
Rules file format: | ||
|
||
The rules file is a text file, one rule per line. Leading and trailing | ||
whitespace is ignored. There are three types of rules: | ||
|
||
rule <pattern> <result> | ||
zap <pattern> | ||
keep <pattern> | ||
|
||
The standard rule ("rule") is used to rename classes. All references | ||
to the renamed classes will also be updated. If a class name is | ||
matched by more than one rule, only the first one will apply. | ||
|
||
<pattern> is a class name with optional wildcards. "**" will | ||
match against any valid class name substring. To match a single | ||
package component (by excluding "." from the match), a single "*" may | ||
be used instead. | ||
|
||
<result> is a class name which can optionally reference the | ||
substrings matched by the wildcards. A numbered reference is available | ||
for every "*" or "**" in the <pattern>, starting from left to | ||
right: "@1", "@2", etc. A special "@0" reference contains the entire | ||
matched class name. | ||
|
||
The "zap" rule causes any matched class to be removed from the resulting | ||
jar file. All zap rules are processed before renaming rules. | ||
|
||
The "keep" rule marks all matched classes as "roots". If any keep | ||
rules are defined all classes which are not reachable from the roots | ||
via dependency analysis are discarded when writing the output | ||
jar. This is the last step in the process, after renaming and zapping. | ||
""" | ||
str.linesIterator.foreach(Console.err.println) | ||
} | ||
} |
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,86 @@ | ||
package com.eed3si9n.jarjarabrams | ||
|
||
import java.io.{ | ||
BufferedInputStream, | ||
BufferedOutputStream, | ||
FileInputStream, | ||
FileOutputStream, | ||
InputStream, | ||
IOException, | ||
OutputStream | ||
} | ||
import java.nio.file.{ Files, Path } | ||
import java.util.zip.ZipInputStream | ||
import scala.util.control.NonFatal | ||
import scala.reflect.ClassTag | ||
|
||
abstract class Using[Source, A] { | ||
protected def open(src: Source): A | ||
def apply[R](src: Source)(f: A => R): R = { | ||
val resource = open(src) | ||
try { | ||
f(resource) | ||
} finally { | ||
close(resource) | ||
} | ||
} | ||
protected def close(out: A): Unit | ||
} | ||
|
||
object Using { | ||
def fileOutputStream(append: Boolean): Using[Path, OutputStream] = | ||
file(f => new BufferedOutputStream(new FileOutputStream(f.toFile, append))) | ||
val fileInputStream: Using[Path, InputStream] = | ||
file(f => new BufferedInputStream(new FileInputStream(f.toFile))) | ||
|
||
val zipInputStream = wrap((in: InputStream) => new ZipInputStream(in)) | ||
|
||
def file[A1 <: AutoCloseable](action: Path => A1): Using[Path, A1] = | ||
file(action, closeCloseable) | ||
|
||
def file[A1](action: Path => A1, closeF: A1 => Unit): Using[Path, A1] = | ||
new OpenFile[A1] { | ||
def openImpl(file: Path) = action(file) | ||
def close(a: A1) = closeF(a) | ||
} | ||
|
||
def wrap[Source: ClassTag, A1 <: AutoCloseable: ClassTag]( | ||
action: Source => A1 | ||
): Using[Source, A1] = | ||
wrap(action, closeCloseable) | ||
|
||
def wrap[Source: ClassTag, A1: ClassTag]( | ||
action: Source => A1, | ||
closeF: A1 => Unit | ||
): Using[Source, A1] = | ||
new WrapUsing[Source, A1] { | ||
def openImpl(source: Source) = action(source) | ||
def close(a: A1) = closeF(a) | ||
} | ||
|
||
private def closeCloseable[A1 <: AutoCloseable]: A1 => Unit = _.close() | ||
|
||
private abstract class WrapUsing[Source: ClassTag, A1: ClassTag] extends Using[Source, A1] { | ||
protected def label[A: ClassTag] = implicitly[ClassTag[A]].runtimeClass.getSimpleName | ||
protected def openImpl(source: Source): A1 | ||
protected final def open(source: Source): A1 = | ||
try { | ||
openImpl(source) | ||
} catch { | ||
case NonFatal(e) => | ||
throw new RuntimeException(s"error wrapping ${label[Source]} in ${label[A1]}", e) | ||
} | ||
} | ||
|
||
private trait OpenFile[A] extends Using[Path, A] { | ||
protected def openImpl(file: Path): A | ||
protected final def open(file: Path): A = { | ||
val parent = file.getParent | ||
if (parent != null) { | ||
try Files.createDirectories(parent) | ||
catch { case _: IOException => } | ||
} | ||
openImpl(file) | ||
} | ||
} | ||
} |
Oops, something went wrong.