Re-use http client across tasks using a build service

And some other cleanup
This commit is contained in:
Kyle Wood 2020-11-14 22:01:24 -08:00
parent d0c03da648
commit d0362412d2
11 changed files with 199 additions and 140 deletions

View file

@ -20,15 +20,10 @@
* USA
*/
package io.papermc.paperweight.util
package io.papermc.paperweight
import io.papermc.paperweight.PaperweightException
import java.io.File
import java.net.URI
import java.net.URL
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit
import io.papermc.paperweight.util.convertToFile
import io.papermc.paperweight.util.convertToUrl
import org.apache.http.HttpHost
import org.apache.http.HttpStatus
import org.apache.http.client.config.CookieSpecs
@ -36,31 +31,40 @@ import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.utils.DateUtils
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import java.io.File
import java.net.URL
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit
fun download(source: Any, target: Any) {
val url = source.convertToUrl()
val file = target.convertToFile()
download(url, file)
}
abstract class DownloadService : BuildService<BuildServiceParameters.None>, AutoCloseable {
private fun download(source: URL, target: File) {
target.parentFile.mkdirs()
val etagFile = target.resolveSibling(target.name + ".etag")
val etag = if (etagFile.exists()) etagFile.readText() else null
val host = HttpHost(source.host, source.port, source.protocol)
val httpClient = HttpClientBuilder.create().run {
setRetryHandler { _, count, _ -> count < 3 }
useSystemProperties()
build()
private val httpClient: CloseableHttpClient = HttpClientBuilder.create().let { builder ->
builder.setRetryHandler { _, count, _ -> count < 3 }
builder.useSystemProperties()
builder.build()
}
val time = if (target.exists()) target.lastModified() else 0
fun download(source: Any, target: Any) {
val url = source.convertToUrl()
val file = target.convertToFile()
download(url, file)
}
private fun download(source: URL, target: File) {
target.parentFile.mkdirs()
val etagFile = target.resolveSibling(target.name + ".etag")
val etag = if (etagFile.exists()) etagFile.readText() else null
val host = HttpHost(source.host, source.port, source.protocol)
val time = if (target.exists()) target.lastModified() else 0
httpClient.use { client ->
val httpGet = HttpGet(source.file)
// Super high timeout, reduce chances of weird things going wrong
val timeouts = TimeUnit.MINUTES.toMillis(5).toInt()
@ -73,14 +77,14 @@ private fun download(source: URL, target: File) {
.build()
if (time > 0) {
val value = DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(time))
val value = DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(time).atZone(ZoneOffset.UTC))
httpGet.setHeader("If-Modified-Since", value)
}
if (etag != null) {
httpGet.setHeader("If-None-Match", etag)
}
client.execute(host, httpGet).use { response ->
httpClient.execute(host, httpGet).use { response ->
val code = response.statusLine.statusCode
if ((code < 200 || code > 299) && code != HttpStatus.SC_NOT_MODIFIED) {
val reason = response.statusLine.reasonPhrase
@ -91,52 +95,46 @@ private fun download(source: URL, target: File) {
saveEtag(response, lastModified, target, etagFile)
}
}
}
private fun handleResponse(response: CloseableHttpResponse, time: Long, target: File): Long {
val lastModified = with(response.getLastHeader("Last-Modified")) {
if (this == null) {
return@with 0
private fun handleResponse(response: CloseableHttpResponse, time: Long, target: File): Long {
val lastModified = with(response.getLastHeader("Last-Modified")) {
if (this == null) {
return@with 0
}
if (value.isNullOrBlank()) {
return@with 0
}
val date = DateUtils.parseDate(value) ?: return@with 0
return@with date.time
}
if (value.isNullOrBlank()) {
return@with 0
if (response.statusLine.statusCode == HttpStatus.SC_NOT_MODIFIED) {
if (lastModified != 0L && time >= lastModified) {
return lastModified
}
}
val date = DateUtils.parseDate(value) ?: return@with 0
return@with date.time
}
if (response.statusLine.statusCode == HttpStatus.SC_NOT_MODIFIED) {
if (lastModified != 0L && time >= lastModified) {
return lastModified
val entity = response.entity ?: return lastModified
entity.content.use { input ->
target.outputStream().buffered().use { output ->
input.copyTo(output)
}
}
return lastModified
}
val entity = response.entity ?: return lastModified
entity.content.use { input ->
target.outputStream().buffered().use { output ->
input.copyTo(output)
private fun saveEtag(response: CloseableHttpResponse, lastModified: Long, target: File, etagFile: File) {
if (lastModified > 0) {
target.setLastModified(lastModified)
}
val header = response.getFirstHeader("ETag") ?: return
val etag = header.value
etagFile.writeText(etag)
}
return lastModified
}
private fun saveEtag(response: CloseableHttpResponse, lastModified: Long, target: File, etagFile: File) {
if (lastModified > 0) {
target.setLastModified(lastModified)
}
val header = response.getFirstHeader("ETag") ?: return
val etag = header.value
etagFile.writeText(etag)
}
private fun Any.convertToUrl(): URL {
return when (this) {
is URL -> this
is URI -> this.toURL()
is String -> URI.create(this).toURL()
is Provider<*> -> this.get().convertToUrl()
else -> throw PaperweightException("Unknown URL type: ${this.javaClass.name}")
override fun close() {
httpClient.close()
}
}

View file

@ -83,11 +83,14 @@ import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.maven
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.registerIfAbsent
class Paperweight : Plugin<Project> {
override fun apply(target: Project) {
target.extensions.create(Constants.EXTENSION, PaperweightExtension::class.java, target.objects, target.layout)
val downloadService = target.gradle.sharedServices.registerIfAbsent("download", DownloadService::class) {}
target.tasks.register<Delete>("cleanCache") {
group = "Paper"
description = "Delete the project setup cache and task outputs."
@ -104,16 +107,16 @@ class Paperweight : Plugin<Project> {
maven("https://hub.spigotmc.org/nexus/content/groups/public/")
}
target.createTasks()
target.createTasks(downloadService)
}
private fun Project.createTasks() {
private fun Project.createTasks(downloadService: Provider<DownloadService>) {
val extension = ext
val initialTasks = createInitialTasks()
val generalTasks = createGeneralTasks()
val mcpTasks = createMcpTasks(initialTasks, generalTasks)
val spigotTasks = createSpigotTasks(initialTasks, generalTasks, mcpTasks)
val initialTasks = createInitialTasks(downloadService)
val generalTasks = createGeneralTasks(downloadService)
val mcpTasks = createMcpTasks(downloadService, initialTasks, generalTasks)
val spigotTasks = createSpigotTasks(downloadService, initialTasks, generalTasks, mcpTasks)
createPatchRemapTasks(initialTasks, generalTasks, mcpTasks, spigotTasks)
@ -194,13 +197,15 @@ class Paperweight : Plugin<Project> {
val mergeGeneratedAts: TaskProvider<MergeAccessTransforms>
)
private fun Project.createInitialTasks(): InitialTasks {
private fun Project.createInitialTasks(downloadService: Provider<DownloadService>): InitialTasks {
val cache: File = layout.cache
val extension: PaperweightExtension = ext
val downloadMcManifest by tasks.registering<DownloadTask> {
url.set(Constants.MC_MANIFEST_URL)
outputFile.set(cache.resolve(Constants.MC_MANIFEST))
downloader.set(downloadService)
}
val mcManifest = downloadMcManifest.flatMap { it.outputFile }.map { gson.fromJson<MinecraftManifest>(it) }
@ -210,6 +215,8 @@ class Paperweight : Plugin<Project> {
manifest.versions.first { it.id == version }.url
})
outputFile.set(cache.resolve(Constants.VERSION_JSON))
downloader.set(downloadService)
}
val versionManifest = downloadMcVersionManifest.flatMap { it.outputFile }.map { gson.fromJson<JsonObject>(it) }
@ -231,6 +238,8 @@ class Paperweight : Plugin<Project> {
configZip.set(cache.resolve(Constants.MCP_ZIPS_PATH).resolve("McpConfig.zip"))
mappingsZip.set(cache.resolve(Constants.MCP_ZIPS_PATH).resolve("McpMappings.zip"))
downloader.set(downloadService)
}
val extractMcpConfig by tasks.registering<ExtractMcp> {
@ -249,6 +258,8 @@ class Paperweight : Plugin<Project> {
forgeFlowerFile.set(toolsPath.resolve("ForgeFlower.jar"))
mcInjectorFile.set(toolsPath.resolve("McInjector.jar"))
specialSourceFile.set(toolsPath.resolve("SpecialSource.jar"))
downloader.set(downloadService)
}
return InitialTasks(
@ -259,7 +270,7 @@ class Paperweight : Plugin<Project> {
)
}
private fun Project.createGeneralTasks(): GeneralTasks {
private fun Project.createGeneralTasks(downloadService: Provider<DownloadService>): GeneralTasks {
val buildDataInfo: Provider<BuildDataInfo> = contents(ext.craftBukkit.buildDataInfo) {
gson.fromJson(it)
}
@ -267,6 +278,8 @@ class Paperweight : Plugin<Project> {
val downloadServerJar by tasks.registering<DownloadServerJar> {
downloadUrl.set(buildDataInfo.map { it.serverUrl })
hash.set(buildDataInfo.map { it.minecraftHash })
downloader.set(downloadService)
}
val filterVanillaJar by tasks.registering<Filter> {
@ -277,7 +290,11 @@ class Paperweight : Plugin<Project> {
return GeneralTasks(buildDataInfo, downloadServerJar, filterVanillaJar)
}
private fun Project.createMcpTasks(initialTasks: InitialTasks, generalTasks: GeneralTasks): McpTasks {
private fun Project.createMcpTasks(
downloadService: Provider<DownloadService>,
initialTasks: InitialTasks,
generalTasks: GeneralTasks
): McpTasks {
val filterVanillaJar: TaskProvider<Filter> = generalTasks.filterVanillaJar
val cache: File = layout.cache
val extension: PaperweightExtension = ext
@ -331,6 +348,8 @@ class Paperweight : Plugin<Project> {
mcLibrariesFile.set(initialTasks.setupMcLibraries.flatMap { it.outputFile })
mcRepo.set(Constants.MC_LIBRARY_URL)
outputDir.set(cache.resolve(Constants.MINECRAFT_JARS_PATH))
downloader.set(downloadService)
}
val writeLibrariesFile by tasks.registering<WriteLibrariesFile> {
@ -354,7 +373,12 @@ class Paperweight : Plugin<Project> {
return McpTasks(generateSrgs, remapVanillaJarSrg, applyMcpPatches)
}
private fun Project.createSpigotTasks(initialTasks: InitialTasks, generalTasks: GeneralTasks, mcpTasks: McpTasks): SpigotTasks {
private fun Project.createSpigotTasks(
downloadService: Provider<DownloadService>,
initialTasks: InitialTasks,
generalTasks: GeneralTasks,
mcpTasks: McpTasks
): SpigotTasks {
val cache: File = layout.cache
val extension: PaperweightExtension = ext
@ -388,7 +412,6 @@ class Paperweight : Plugin<Project> {
srgToSpigot.set(cache.resolve(Constants.SRG_TO_SPIGOT))
mcpToSpigot.set(cache.resolve(Constants.MCP_TO_SPIGOT))
notchToSpigot.set(cache.resolve(Constants.NOTCH_TO_SPIGOT))
atlasTest.set(cache.resolve("atlasTest.jar"))
}
val remapVanillaJarSpigot by tasks.registering<RemapVanillaJarSpigot> {
@ -455,6 +478,8 @@ class Paperweight : Plugin<Project> {
apiPom.set(patchSpigotApi.flatMap { it.outputDir.file("pom.xml") })
serverPom.set(patchSpigotServer.flatMap { it.outputDir.file("pom.xml") })
outputDir.set(cache.resolve(Constants.SPIGOT_JARS_PATH))
downloader.set(downloadService)
}
val remapSpigotAt by tasks.registering<RemapSpigotAt> {

View file

@ -32,6 +32,7 @@ import org.cadixdev.mercury.at.AccessTransformerRewriter
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.InputFile
import org.gradle.kotlin.dsl.submit
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkerExecutor
@ -65,7 +66,7 @@ abstract class ApplySourceAt : ZippedTask() {
forkOptions.jvmArgs("-Xmx2G")
}
queue.submit(AtAction::class.java) {
queue.submit(AtAction::class) {
classpath.add(vanillaJar.file)
classpath.add(vanillaRemappedSrgJar.file)

View file

@ -22,14 +22,15 @@
package io.papermc.paperweight.tasks
import io.papermc.paperweight.DownloadService
import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.util.defaultOutput
import io.papermc.paperweight.util.download
import java.math.BigInteger
import java.security.MessageDigest
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
@ -43,6 +44,9 @@ abstract class DownloadServerJar : BaseTask() {
@get:OutputFile
abstract val outputJar: RegularFileProperty
@get:Internal
abstract val downloader: Property<DownloadService>
override fun init() {
outputJar.convention(defaultOutput())
}
@ -51,7 +55,7 @@ abstract class DownloadServerJar : BaseTask() {
fun run() {
val file = outputJar.asFile.get()
download(downloadUrl, outputJar)
downloader.get().download(downloadUrl, outputJar)
val digest = MessageDigest.getInstance("MD5")

View file

@ -47,6 +47,7 @@ import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.nio.file.Files
abstract class GenerateSpigotSrgs : DefaultTask() {
@ -65,7 +66,7 @@ abstract class GenerateSpigotSrgs : DefaultTask() {
abstract val extraSpigotSrgMappings: RegularFileProperty
@get:InputFile
abstract val loggerFields: RegularFileProperty
// test
@get:InputFile
abstract val vanillaJar: RegularFileProperty
@ -81,9 +82,6 @@ abstract class GenerateSpigotSrgs : DefaultTask() {
abstract val mcpToSpigot: RegularFileProperty
@get:OutputFile
abstract val notchToSpigot: RegularFileProperty
// tet
@get:OutputFile
abstract val atlasTest: RegularFileProperty
@TaskAction
fun run() {
@ -112,12 +110,15 @@ abstract class GenerateSpigotSrgs : DefaultTask() {
).merge()
// notch <-> spigot is incomplete here, it would result in inheritance issues to work with this incomplete set.
// so we use it once to remap some jar
println("running atlas to complete mappings...")
Atlas().apply {
install { ctx -> JarEntryRemappingTransformer(LorenzRemapper(notchToSpigotSet, ctx.inheritanceProvider())) }
run(vanillaJar.path, atlasTest.path)
close()
// so we use it once to remap some jar, which fills out the inheritance data
val atlasOut = Files.createTempFile("paperweight", "jar")
try {
Atlas().use { atlas ->
atlas.install { ctx -> JarEntryRemappingTransformer(LorenzRemapper(notchToSpigotSet, ctx.inheritanceProvider())) }
atlas.run(vanillaJar.path, atlasOut)
}
} finally {
Files.deleteIfExists(atlasOut)
}
val srgToMcpSet = MappingFormats.TSRG.createReader(srgToMcp.file.toPath()).use { it.read() }

View file

@ -22,11 +22,11 @@
package io.papermc.paperweight.tasks
import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.util.Constants.paperTaskOutput
import io.papermc.paperweight.util.cache
import io.papermc.paperweight.util.defaultOutput
import io.papermc.paperweight.util.runJar
import io.papermc.paperweight.util.wrapException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
@ -86,7 +86,7 @@ abstract class RemapVanillaJarSpigot : BaseTask() {
val work = layout.projectDirectory.file(workDirName.get())
try {
wrapException("Failed to apply class mappings") {
try {
val logFile = layout.cache.resolve(paperTaskOutput("class.log"))
logFile.delete()
runJar(
@ -98,8 +98,11 @@ abstract class RemapVanillaJarSpigot : BaseTask() {
it != "-e"
}
)
} catch (e: Exception) {
throw PaperweightException("Failed to apply class mappings", e)
}
wrapException("Failed to apply member mappings") {
try {
val logFile = layout.cache.resolve(paperTaskOutput("member.log"))
logFile.delete()
runJar(
@ -108,8 +111,11 @@ abstract class RemapVanillaJarSpigot : BaseTask() {
logFile = logFile,
args = *doReplacements(memberMapCommand.get(), classJarPath, memberMappingsPath, membersJarPath)
)
} catch (e: Exception) {
throw PaperweightException("Failed to apply member mappings", e)
}
wrapException("Failed to create remapped jar") {
try {
val logFile = layout.cache.resolve(paperTaskOutput("final.log"))
logFile.delete()
runJar(
@ -118,6 +124,8 @@ abstract class RemapVanillaJarSpigot : BaseTask() {
logFile = logFile,
args = *doReplacements(finalMapCommand.get(), membersJarPath, accessTransformersPath, packageMappingsPath, outputJarPath)
)
} catch (e: Exception) {
throw PaperweightException("Failed to create remapped jar", e)
}
} finally {
classJarFile.delete()

View file

@ -22,11 +22,12 @@
package io.papermc.paperweight.tasks
import io.papermc.paperweight.DownloadService
import io.papermc.paperweight.util.Constants
import io.papermc.paperweight.util.MavenArtifact
import io.papermc.paperweight.util.McpConfig
import io.papermc.paperweight.util.McpJvmCommand
import io.papermc.paperweight.util.decompile
import io.papermc.paperweight.util.download
import io.papermc.paperweight.util.file
import io.papermc.paperweight.util.fromJson
import io.papermc.paperweight.util.gson
@ -43,11 +44,14 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.submit
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkQueue
import org.gradle.workers.WorkerExecutor
import org.w3c.dom.Element
@ -59,8 +63,11 @@ abstract class DownloadTask : DefaultTask() {
@get:OutputFile
abstract val outputFile: RegularFileProperty
@get:Internal
abstract val downloader: Property<DownloadService>
@TaskAction
fun run() = download(url, outputFile)
fun run() = downloader.get().download(url, outputFile)
}
abstract class DownloadMcpFiles : DefaultTask() {
@ -79,6 +86,9 @@ abstract class DownloadMcpFiles : DefaultTask() {
@get:OutputFile
abstract val mappingsZip: RegularFileProperty
@get:Internal
abstract val downloader: Property<DownloadService>
@TaskAction
fun run() {
val repo = listOf(Constants.FORGE_MAVEN_URL)
@ -88,14 +98,14 @@ abstract class DownloadMcpFiles : DefaultTask() {
artifact = "mcp_config",
version = mcpMinecraftVersion.get() + "-" + mcpConfigVersion.get(),
extension = "zip"
).downloadToFile(configZip.file, repo)
).downloadToFile(downloader.get(), configZip.file, repo)
MavenArtifact(
group = "de.oceanlabs.mcp",
artifact = "mcp_${mcpMappingsChannel.get()}",
version = mcpMappingsVersion.get(),
extension = "zip"
).downloadToFile(mappingsZip.file, repo)
).downloadToFile(downloader.get(), mappingsZip.file, repo)
}
}
@ -111,6 +121,9 @@ abstract class DownloadMcpTools : DefaultTask() {
@get:OutputFile
abstract val specialSourceFile: RegularFileProperty
@get:Internal
abstract val downloader: Property<DownloadService>
@get:Inject
abstract val workerExecutor: WorkerExecutor
@ -119,23 +132,19 @@ abstract class DownloadMcpTools : DefaultTask() {
val config = gson.fromJson<McpConfig>(configFile)
val queue = workerExecutor.noIsolation()
queue.submit(DownloadWorker::class.java) {
repos.add(config.functions.decompile.repo)
artifact.set(config.functions.decompile.version)
target.set(forgeFlowerFile.file)
downloadToDir.set(false)
}
queue.submit(DownloadWorker::class.java) {
repos.add(config.functions.mcinject.repo)
artifact.set(config.functions.mcinject.version)
target.set(mcInjectorFile.file)
downloadToDir.set(false)
}
queue.submit(DownloadWorker::class.java) {
repos.add(config.functions.rename.repo)
artifact.set(config.functions.rename.version)
target.set(specialSourceFile.file)
submitDownload(queue, config.functions.decompile, forgeFlowerFile)
submitDownload(queue, config.functions.mcinject, mcInjectorFile)
submitDownload(queue, config.functions.rename, specialSourceFile)
}
private fun submitDownload(queue: WorkQueue, cmd: McpJvmCommand, file: RegularFileProperty) {
queue.submit(DownloadWorker::class) {
repos.add(cmd.repo)
artifact.set(cmd.version)
target.set(file.file)
downloadToDir.set(false)
downloader.set(this@DownloadMcpTools.downloader)
}
}
}
@ -150,6 +159,9 @@ abstract class DownloadMcLibraries : DefaultTask() {
@get:OutputDirectory
abstract val outputDir: DirectoryProperty
@get:Internal
abstract val downloader: Property<DownloadService>
@get:Inject
abstract val workerExecutor: WorkerExecutor
@ -163,11 +175,12 @@ abstract class DownloadMcLibraries : DefaultTask() {
val queue = workerExecutor.noIsolation()
mcLibrariesFile.file.useLines { lines ->
lines.forEach { line ->
queue.submit(DownloadWorker::class.java) {
queue.submit(DownloadWorker::class) {
repos.set(mcRepos)
artifact.set(line)
target.set(out)
downloadToDir.set(true)
downloader.set(this@DownloadMcLibraries.downloader)
}
}
}
@ -184,6 +197,9 @@ abstract class DownloadSpigotDependencies : BaseTask() {
@get:OutputDirectory
abstract val outputDir: DirectoryProperty
@get:Internal
abstract val downloader: Property<DownloadService>
@get:Inject
abstract val workerExecutor: WorkerExecutor
@ -205,11 +221,12 @@ abstract class DownloadSpigotDependencies : BaseTask() {
val queue = workerExecutor.noIsolation()
for (art in artifacts) {
queue.submit(DownloadWorker::class.java) {
queue.submit(DownloadWorker::class) {
repos.set(spigotRepos)
artifact.set(art.toString())
target.set(out)
downloadToDir.set(true)
downloader.set(this@DownloadSpigotDependencies.downloader)
}
}
}
@ -295,6 +312,7 @@ interface DownloadParams : WorkParameters {
val artifact: Property<String>
val target: RegularFileProperty
val downloadToDir: Property<Boolean>
val downloader: Property<DownloadService>
}
abstract class DownloadWorker : WorkAction<DownloadParams> {
@get:Inject
@ -303,9 +321,9 @@ abstract class DownloadWorker : WorkAction<DownloadParams> {
override fun execute() {
val artifact = MavenArtifact.parse(parameters.artifact.get())
if (parameters.downloadToDir.get()) {
artifact.downloadToDir(parameters.target.file, parameters.repos.get())
artifact.downloadToDir(parameters.downloader.get(), parameters.target.file, parameters.repos.get())
} else {
artifact.downloadToFile(parameters.target.file, parameters.repos.get())
artifact.downloadToFile(parameters.downloader.get(), parameters.target.file, parameters.repos.get())
}
}
}

View file

@ -108,8 +108,6 @@ abstract class RemapPatches : BaseTask() {
into(sourceInputDir)
}
// tempInputDir.resolve(".git").deleteRecursively()
PatchSourceRemapWorker(
mappings,
listOf(*classpathFiles.toTypedArray(), tempApiDir.resolve("src/main/java")).map { it.toPath() },
@ -136,7 +134,7 @@ abstract class RemapPatches : BaseTask() {
// - not a loop yet cause it doesn't even work for the first patch
patches.forEach { patch ->
println("===========================")
println("attempting to remap " + patch)
println("attempting to remap $patch")
println("===========================")
remapper.remap() // Remap to to Spigot mappings TODO: verify this step produces correct results
patchApplier.applyPatch(patch) // Apply patch on Spigot mappings
@ -146,7 +144,7 @@ abstract class RemapPatches : BaseTask() {
patchApplier.commitChanges() // Commit the changes
patchApplier.checkoutOld() // Normal checkout back to Spigot mappings branch
println("===========================")
println("done remapping patch " + patch)
println("done remapping patch $patch")
println("===========================")
}
}
@ -196,11 +194,11 @@ abstract class RemapPatches : BaseTask() {
}
private fun createWorkDirByCloning(name: String, source: File): File {
val workdDir = layout.cache.resolve("paperweight")
return workdDir.resolve(name).apply {
val workDir = layout.cache.resolve("paperweight")
return workDir.resolve(name).apply {
deleteRecursively()
mkdirs()
Git(workdDir)("clone", source.absolutePath, this.absolutePath).executeSilently()
Git(workDir)("clone", source.absolutePath, this.absolutePath).executeSilently()
}
}
}

View file

@ -42,6 +42,7 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.kotlin.dsl.submit
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkerExecutor
@ -86,7 +87,7 @@ abstract class RemapSources : ZippedTask() {
forkOptions.jvmArgs("-Xmx2G")
}
queue.submit(RemapAction::class.java) {
queue.submit(RemapAction::class) {
classpath.add(vanillaJar.file)
classpath.add(vanillaRemappedSpigotJar.file)
classpath.add(spigotApiDir.dir("src/main/java").get().asFile)

View file

@ -22,6 +22,7 @@
package io.papermc.paperweight.util
import io.papermc.paperweight.DownloadService
import io.papermc.paperweight.PaperweightException
import java.io.File
@ -44,13 +45,13 @@ data class MavenArtifact(
val file: String
get() = "$artifact-$version$classifierText.$ext"
fun downloadToFile(targetFile: File, repos: List<String>) {
fun downloadToFile(downloadService: DownloadService, targetFile: File, repos: List<String>) {
targetFile.parentFile.mkdirs()
var thrown: Exception? = null
for (repo in repos) {
try {
download(addSlash(repo) + path, targetFile)
downloadService.download(addSlash(repo) + path, targetFile)
return
} catch (e: Exception) {
if (thrown != null) {
@ -63,9 +64,9 @@ data class MavenArtifact(
thrown?.let { throw PaperweightException("Failed to download artifact: $this. Checked repos: $repos", it) }
}
fun downloadToDir(targetDir: File, repos: List<String>): File {
fun downloadToDir(downloadService: DownloadService, targetDir: File, repos: List<String>): File {
val out = targetDir.resolve(file)
downloadToFile(targetDir.resolve(file), repos)
downloadToFile(downloadService, targetDir.resolve(file), repos)
return out
}

View file

@ -48,6 +48,8 @@ import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.TaskProvider
import java.net.URI
import java.net.URL
val gson: Gson = Gson()
@ -83,14 +85,6 @@ object UselessOutputStream : OutputStream() {
}
}
inline fun wrapException(msg: String, func: () -> Unit) {
try {
func()
} catch (e: Exception) {
throw PaperweightException(msg, e)
}
}
fun getCsvReader(file: File) = CSVReader(
file.reader(),
CSVParser.DEFAULT_SEPARATOR,
@ -110,6 +104,16 @@ fun Any.convertToFile(): File {
}
}
fun Any.convertToUrl(): URL {
return when (this) {
is URL -> this
is URI -> this.toURL()
is String -> URI.create(this).toURL()
is Provider<*> -> this.get().convertToUrl()
else -> throw PaperweightException("Unknown URL type: ${this.javaClass.name}")
}
}
fun ensureParentExists(vararg files: Any) {
for (file in files) {
val parent = file.convertToFile().parentFile
@ -161,7 +165,7 @@ inline fun <reified T> Project.contents(contentFile: Any, crossinline convert: (
.map { convert(it) }
}
// We have to create our own delegate because the ones Gradle provides don't work in plugin dev environments
// We have to create our own task delegate because the ones Gradle provides don't work in plugin dev environments
inline fun <reified T : Task> TaskContainer.registering(noinline configure: T.() -> Unit): TaskDelegateProvider<T> {
return TaskDelegateProvider(this, T::class, configure)
}