Skip to content

Commit

Permalink
Start decoupling AGP
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbrooks committed Oct 4, 2024
1 parent 8a92fff commit d220d52
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 128 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dependencyResolutionManagement {
}

include("vgo-core", "vgo", "vgo-plugin")
include("vgo-cli")
106 changes: 106 additions & 0 deletions vgo-cli/build.gradle.kts
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")
}
84 changes: 84 additions & 0 deletions vgo-cli/src/main/kotlin/com/jzbrooks/vgo/cli/ArgReader.kt
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
}
}
}
}
38 changes: 38 additions & 0 deletions vgo-cli/src/main/kotlin/com/jzbrooks/vgo/cli/main.kt
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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.jzbrooks.vgo.core.graphic.Extra
import com.jzbrooks.vgo.core.graphic.Graphic
import com.jzbrooks.vgo.core.graphic.Group
import com.jzbrooks.vgo.core.graphic.Path
import com.jzbrooks.vgo.core.util.math.computeAbsoluteCoordinates

/**
* Merges multiple paths into a single path where possible
Expand Down Expand Up @@ -64,7 +65,7 @@ class MergePaths : BottomUpOptimization {
//
// There might be a reasonable way to deduce that situation more
// specifically, which could enable merging of some even odd paths.
if (!haveSameAttributes(current, previous) || current.fillRule == Path.FillRule.EVEN_ODD) {
if (!haveSameAttributes(current, previous)) {
mergedPaths.add(current)
} else {
previous.commands += current.commands
Expand Down
90 changes: 2 additions & 88 deletions vgo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,23 @@ plugins {

kotlin.sourceSets.getByName("main").kotlin.srcDir("src/generated/kotlin")

val r8: Configuration by configurations.creating

dependencies {
implementation(project(":vgo-core"))
implementation("com.android.tools:sdk-common:31.7.0")

compileOnly("com.android.tools:sdk-common:31.7.0")

testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.28.1")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.1")
testImplementation("org.junit.jupiter:junit-jupiter-params")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")

r8("com.android.tools:r8:8.5.35")
}

tasks {
compileKotlin {
dependsOn("generateConstants")
}

jar {
dependsOn(configurations.runtimeClasspath)
manifest {
attributes["Main-Class"] = "com.jzbrooks.vgo.Application"
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 generateConstants by registering {
finalizedBy("compileKotlin")

Expand Down Expand Up @@ -115,53 +76,6 @@ tasks {
mustRunAfter(generateConstants)
}

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 updateBaselineOptimizations by registering(Copy::class) {
description = "Updates baseline assets with the latest integration test outputs."
group = "Build Setup"
Expand Down
Loading

0 comments on commit d220d52

Please sign in to comment.