-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
258 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ dependencyResolutionManagement { | |
} | ||
|
||
include("vgo-core", "vgo", "vgo-plugin") | ||
include("vgo-cli") |
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,106 @@ | ||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("com.vanniktech.maven.publish") | ||
} | ||
|
||
tasks { | ||
jar { | ||
dependsOn(configurations.runtimeClasspath) | ||
manifest { | ||
attributes["Main-Class"] = "com.jzbrooks.vgo.cli.MainKt" | ||
attributes["Bundle-Version"] = project.properties["VERSION_NAME"] | ||
} | ||
|
||
val sourceClasses = sourceSets.main.get().output.classesDirs | ||
inputs.files(sourceClasses) | ||
destinationDirectory.set(layout.buildDirectory.dir("libs/debug")) | ||
|
||
doFirst { | ||
from(files(sourceClasses)) | ||
from(configurations.runtimeClasspath.get().asFileTree.files.map(::zipTree)) | ||
|
||
exclude( | ||
"**/*.kotlin_metadata", | ||
"**/*.kotlin_module", | ||
"**/*.kotlin_builtins", | ||
"**/module-info.class", | ||
"META-INF/maven/**", | ||
"META-INF/*.version", | ||
"META-INF/LICENSE*", | ||
"META-INF/LGPL2.1", | ||
"META-INF/DEPENDENCIES", | ||
"META-INF/AL2.0", | ||
"META-INF/BCKEY.DSA", | ||
"META-INF/BC2048KE.DSA", | ||
"META-INF/BCKEY.SF", | ||
"META-INF/BC2048KE.SF", | ||
"**/NOTICE*", | ||
"javax/activation/**", | ||
"xsd/catalog.xml", | ||
) | ||
} | ||
} | ||
|
||
|
||
val optimize by registering(JavaExec::class) { | ||
description = "Runs proguard on the jar application." | ||
group = "build" | ||
|
||
inputs.file("build/libs/debug/vgo.jar") | ||
outputs.file("build/libs/vgo.jar") | ||
|
||
val javaHome = System.getProperty("java.home") | ||
|
||
classpath(r8) | ||
mainClass = "com.android.tools.r8.R8" | ||
|
||
args( | ||
"--release", | ||
"--classfile", | ||
"--lib", | ||
javaHome, | ||
"--output", | ||
"build/libs/vgo.jar", | ||
"--pg-conf", | ||
"$rootDir/optimize.pro", | ||
"build/libs/debug/vgo.jar", | ||
) | ||
|
||
dependsOn(getByName("jar")) | ||
} | ||
|
||
val binaryFileProp = layout.buildDirectory.file("libs/vgo") | ||
val binary by registering { | ||
description = "Prepends shell script in the jar to improve CLI" | ||
group = "build" | ||
|
||
dependsOn(optimize) | ||
|
||
inputs.file("build/libs/vgo.jar") | ||
outputs.file(binaryFileProp) | ||
|
||
doLast { | ||
val binaryFile = binaryFileProp.get().asFile | ||
binaryFile.parentFile.mkdirs() | ||
binaryFile.delete() | ||
binaryFile.appendText("#!/bin/sh\n\nexec java \$JAVA_OPTS -jar \$0 \"\$@\"\n\n") | ||
file("build/libs/vgo.jar").inputStream().use { binaryFile.appendBytes(it.readBytes()) } | ||
binaryFile.setExecutable(true, false) | ||
} | ||
} | ||
} | ||
|
||
val r8: Configuration by configurations.creating | ||
|
||
dependencies { | ||
implementation(project(":vgo")) | ||
|
||
implementation("com.android.tools:sdk-common:31.7.0") | ||
|
||
r8("com.android.tools:r8:8.5.35") | ||
|
||
testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.28.1") | ||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.0") | ||
testImplementation("org.junit.jupiter:junit-jupiter-params") | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") | ||
} |
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,84 @@ | ||
package com.jzbrooks.vgo.cli | ||
|
||
import kotlin.collections.any | ||
import kotlin.collections.first | ||
import kotlin.collections.firstOrNull | ||
import kotlin.collections.getOrElse | ||
import kotlin.collections.indexOfFirst | ||
import kotlin.collections.isNotEmpty | ||
import kotlin.collections.map | ||
import kotlin.text.isNotBlank | ||
import kotlin.text.split | ||
|
||
class ArgReader(private val args: MutableList<String>) { | ||
private val hasArguments | ||
get() = args.isNotEmpty() | ||
|
||
fun readFlag(name: String): Boolean { | ||
require(name.isNotBlank()) | ||
|
||
val names = name.split('|') | ||
if (names.size > 1) { | ||
return names.any(::readFlag) | ||
} | ||
|
||
val index = args.indexOfFirst { isOptionArgument(name, it) } | ||
if (index == -1) return false | ||
|
||
args.removeAt(index) | ||
return true | ||
} | ||
|
||
fun readOption(name: String): String? { | ||
require(name.isNotBlank()) | ||
|
||
val names = name.split('|') | ||
if (names.size > 1) { | ||
return names.map(::readOption).firstOrNull { it != null } | ||
} | ||
|
||
val index = args.indexOfFirst { isOptionArgument(name, it) } | ||
if (index == -1) return null | ||
|
||
val value = | ||
args.getOrElse(index + 1) { | ||
throw IllegalStateException("Missing value after ${if (name.length == 1) "-" else "--"}$name") | ||
} | ||
|
||
args.removeAt(index) | ||
args.removeAt(index) | ||
return value | ||
} | ||
|
||
fun readArguments(): List<String> { | ||
val arguments = kotlin.collections.mutableListOf<String>() | ||
while (hasArguments) { | ||
arguments.add(readArgument()) | ||
} | ||
return arguments | ||
} | ||
|
||
private fun readArgument(): String { | ||
val value = args.first() | ||
|
||
check(!isOption(value)) { "Unexpected option $value" } | ||
|
||
args.removeAt(0) | ||
return value | ||
} | ||
|
||
companion object { | ||
private fun isOption(name: String) = name.length >= 2 && name[0] == '-' | ||
|
||
private fun isOptionArgument( | ||
name: String, | ||
argument: String, | ||
): Boolean { | ||
return if (name.length == 1) { | ||
"-$name" == argument | ||
} else { | ||
"--$name" == argument | ||
} | ||
} | ||
} | ||
} |
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,38 @@ | ||
import com.jzbrooks.vgo.Application | ||
import com.jzbrooks.vgo.cli.ArgReader | ||
import kotlin.system.exitProcess | ||
|
||
fun main(args: Array<String>) { | ||
val argReader = ArgReader(args.toMutableList()) | ||
|
||
val printHelp = argReader.readFlag("help|h") | ||
val printVersion = argReader.readFlag("version|v") | ||
val printStats = argReader.readFlag("stats|s") | ||
val indent = argReader.readOption("indent")?.toIntOrNull() | ||
|
||
val outputs = run { | ||
val outputPaths = mutableListOf<String>() | ||
var output = argReader.readOption("output|o") | ||
while (output != null) { | ||
outputPaths.add(output) | ||
output = argReader.readOption("output|o") | ||
} | ||
outputPaths.toList() | ||
} | ||
|
||
var format = argReader.readOption("format") | ||
|
||
var inputs = argReader.readArguments() | ||
|
||
val options = Application.Options( | ||
printHelp = printHelp, | ||
printVersion = printVersion, | ||
printStats = printStats, | ||
indent = indent, | ||
output = outputs, | ||
format = format, | ||
input = inputs | ||
) | ||
|
||
exitProcess(Application(options).run()) | ||
} |
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
Oops, something went wrong.