Skip to content

Commit

Permalink
feat: Allow the use of an explicit d8 version for dexJar
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Sep 16, 2024
1 parent 116b311 commit b142df8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "com.xpdustry"
version = "4.0.3" + if (indraGit.headTag() == null) "-SNAPSHOT" else ""
version = "4.1.0" + if (indraGit.headTag() == null) "-SNAPSHOT" else ""
description = "Gradle plugin for building and testing mindustry mods/plugins."

repositories {
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/xpdustry/toxopid/Toxopid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
*/
package com.xpdustry.toxopid

import java.net.http.HttpClient

/**
* Constants for Toxopid.
*/
public object Toxopid {
public const val EXTENSION_NAME: String = "toxopid"
public const val TASK_GROUP_NAME: String = "toxopid"
public const val DEFAULT_MINDUSTRY_VERSION: String = "146"

internal val HTTP = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build()
}
71 changes: 56 additions & 15 deletions src/main/kotlin/com/xpdustry/toxopid/task/DexJar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package com.xpdustry.toxopid.task

import com.xpdustry.toxopid.Toxopid
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
Expand All @@ -35,10 +36,15 @@ import org.gradle.api.tasks.CompileClasspath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.property
import java.io.File
import java.net.URI
import java.net.http.HttpRequest
import java.net.http.HttpResponse.BodyHandlers
import kotlin.io.path.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.listDirectoryEntries
Expand Down Expand Up @@ -74,6 +80,12 @@ public open class DexJar : DefaultTask() {
@get:[InputFiles CompileClasspath]
public val classpath: ConfigurableFileCollection = project.objects.fileCollection()

/**
* The version of d8 to use. If not set, the d8 tool from the selected local Android SDK will be used.
*/
@get:[Input Optional]
public val d8Version: Property<String> = project.objects.property()

init {
minSdkVersion.convention(14)
output.convention { temporaryDir.resolve("output.jar") }
Expand Down Expand Up @@ -121,21 +133,50 @@ public open class DexJar : DefaultTask() {
add(source.get().asFile.absolutePath)
}

val d8 =
Path(sdk)
.resolve("build-tools")
.listDirectoryEntries()
.filter { it.fileName.toString().startsWith(version.toString()) }
.maxOrNull()
?.resolve(if (System.getProperty("os.name").lowercase().contains("windows")) "d8.bat" else "d8")
?.absolutePathString()
?: error("No d8 found in Android SDK for version $version")

logger.info("Dexing jar with d8: $d8")

project.exec {
executable = d8
args = arguments
if (d8Version.isPresent) {
logger.info("Dexing jar using explicit d8 version: ${d8Version.get()}")

val r8lib = project.gradle.gradleUserHomeDir.resolve("caches/toxopid/r8/r8lib-${d8Version.get()}.jar")
r8lib.parentFile.mkdirs()

if (!r8lib.exists()) {
val response =
Toxopid.HTTP.send(
HttpRequest.newBuilder(URI("https://storage.googleapis.com/r8-releases/raw/${d8Version.get()}/r8lib.jar"))
.GET()
.build(),
BodyHandlers.ofFile(r8lib.toPath()),
)

if (response.statusCode() != 200) {
error("Failed to download r8lib: (code=${response.statusCode()}, version=${d8Version.get()})")
}

logger.info("Downloaded r8 version ${d8Version.get()}")
}

project.javaexec {
mainClass = "com.android.tools.r8.D8"
classpath(r8lib)
args = arguments
}
} else {
val d8 =
Path(sdk)
.resolve("build-tools")
.listDirectoryEntries()
.filter { it.fileName.toString().startsWith(version.toString()) }
.maxOrNull()
?.resolve(if (System.getProperty("os.name").lowercase().contains("windows")) "d8.bat" else "d8")
?.absolutePathString()
?: error("No d8 found in Android SDK for version $version")

logger.info("Dexing jar using user d8: $d8")

project.exec {
executable = d8
args = arguments
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package com.xpdustry.toxopid.task

import com.xpdustry.toxopid.Toxopid
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
Expand All @@ -37,7 +38,6 @@ import org.gradle.kotlin.dsl.property
import org.hjson.JsonObject
import java.io.File
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

Expand Down Expand Up @@ -96,7 +96,7 @@ public open class GithubAssetDownload : DefaultTask() {
}

val release =
HTTP.send(
Toxopid.HTTP.send(
HttpRequest.newBuilder(URI("https://api.github.com/repos/${owner.get()}/${repo.get()}/releases/tags/${version.get()}"))
.header("Accept", "application/vnd.github+json")
.applyAuthorization()
Expand All @@ -117,7 +117,7 @@ public open class GithubAssetDownload : DefaultTask() {
?: error("Failed to find asset named $asset")

val download =
HTTP.send(
Toxopid.HTTP.send(
HttpRequest.newBuilder(URI(asset["url"].asString()))
.header("Accept", "application/octet-stream")
.applyAuthorization()
Expand Down Expand Up @@ -146,6 +146,5 @@ public open class GithubAssetDownload : DefaultTask() {
public companion object {
public const val MINDUSTRY_DESKTOP_DOWNLOAD_TASK_NAME: String = "downloadMindustryDesktop"
public const val MINDUSTRY_SERVER_DOWNLOAD_TASK_NAME: String = "downloadMindustryServer"
private val HTTP = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build()
}
}

0 comments on commit b142df8

Please sign in to comment.