Allow setting paperweights default java launcher separate from the projects

This commit is contained in:
Jason Penilla 2023-02-24 15:59:27 -07:00
parent 90384770ba
commit 1257a31235
No known key found for this signature in database
GPG key ID: 0E75A301420E48F8
18 changed files with 192 additions and 56 deletions

View file

@ -33,18 +33,23 @@ import io.papermc.paperweight.tasks.patchremap.RemapPatches
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import java.io.File
import javax.inject.Inject
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
class PaperweightCore : Plugin<Project> {
abstract class PaperweightCore : Plugin<Project> {
@get:Inject
abstract val javaToolchainService: JavaToolchainService
override fun apply(target: Project) {
Git.checkForGit()
val ext = target.extensions.create(PAPERWEIGHT_EXTENSION, PaperweightCoreExtension::class, target)
val ext = target.extensions.create(PAPERWEIGHT_EXTENSION, PaperweightCoreExtension::class, target, javaToolchainService)
target.gradle.sharedServices.registerIfAbsent("download", DownloadService::class) {}

View file

@ -22,6 +22,7 @@
package io.papermc.paperweight.core.extension
import io.papermc.paperweight.extension.AbstractJavaLauncherHolder
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import java.util.Locale
@ -32,9 +33,15 @@ import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
open class PaperweightCoreExtension(project: Project, objects: ObjectFactory, layout: ProjectLayout) {
abstract class PaperweightCoreExtension(
project: Project,
objects: ObjectFactory,
layout: ProjectLayout,
javaToolchainService: JavaToolchainService
) : AbstractJavaLauncherHolder(project, javaToolchainService) {
@Suppress("MemberVisibilityCanBePrivate")
val workDir: DirectoryProperty = objects.dirWithDefault(layout, "work")

View file

@ -0,0 +1,40 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package io.papermc.paperweight.extension
import io.papermc.paperweight.util.*
import org.gradle.api.Project
import org.gradle.jvm.toolchain.JavaToolchainService
abstract class AbstractJavaLauncherHolder(
project: Project,
javaToolchainService: JavaToolchainService
) : JavaLauncherHolder {
init {
init(project, javaToolchainService)
}
private fun init(project: Project, javaToolchainService: JavaToolchainService) {
javaLauncher.convention(javaToolchainService.defaultJavaLauncher(project))
}
}

View file

@ -0,0 +1,30 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package io.papermc.paperweight.extension
import org.gradle.api.provider.Property
import org.gradle.jvm.toolchain.JavaLauncher
interface JavaLauncherHolder {
val javaLauncher: Property<JavaLauncher>
}

View file

@ -23,23 +23,13 @@
package io.papermc.paperweight.tasks
import io.papermc.paperweight.util.*
import javax.inject.Inject
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Nested
import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService
private fun JavaLauncherTaskBase.defaultJavaLauncher(project: Project): Provider<JavaLauncher> =
javaToolchainService.defaultJavaLauncher(project)
interface JavaLauncherTaskBase {
@get:Nested
val launcher: Property<JavaLauncher>
@get:Inject
val javaToolchainService: JavaToolchainService
}
abstract class JavaLauncherTask : BaseTask(), JavaLauncherTaskBase {
@ -47,7 +37,7 @@ abstract class JavaLauncherTask : BaseTask(), JavaLauncherTaskBase {
override fun init() {
super.init()
launcher.convention(defaultJavaLauncher(project))
launcher.convention(project.defaultJavaLauncher())
}
}
@ -56,6 +46,6 @@ abstract class JavaLauncherZippedTask : ZippedTask(), JavaLauncherTaskBase {
override fun init() {
super.init()
launcher.convention(defaultJavaLauncher(project))
launcher.convention(project.defaultJavaLauncher())
}
}

View file

@ -27,6 +27,7 @@ import com.google.gson.*
import dev.denwav.hypo.model.ClassProviderRoot
import io.papermc.paperweight.DownloadService
import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.extension.JavaLauncherHolder
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.constants.*
import java.io.File
@ -268,6 +269,10 @@ fun toHex(hash: ByteArray): String {
return sb.toString()
}
// This works because we don't expect or support people applying more than one of userdev/core/patcher in a single project
fun Project.defaultJavaLauncher(): Provider<JavaLauncher> =
extensions.getByType(JavaLauncherHolder::class.java).javaLauncher
fun JavaToolchainService.defaultJavaLauncher(project: Project): Provider<JavaLauncher> {
return launcherFor(project.extensions.getByType<JavaPluginExtension>().toolchain).orElse(
launcherFor {

View file

@ -35,6 +35,7 @@ import io.papermc.paperweight.util.constants.*
import io.papermc.paperweight.util.data.*
import java.io.File
import java.util.concurrent.atomic.AtomicReference
import javax.inject.Inject
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
@ -42,15 +43,18 @@ import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Jar
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
import org.gradle.kotlin.dsl.registering
class PaperweightPatcher : Plugin<Project> {
abstract class PaperweightPatcher : Plugin<Project> {
@get:Inject
abstract val javaToolchainService: JavaToolchainService
override fun apply(target: Project) {
Git.checkForGit()
val patcher = target.extensions.create(PAPERWEIGHT_EXTENSION, PaperweightPatcherExtension::class, target)
val patcher = target.extensions.create(PAPERWEIGHT_EXTENSION, PaperweightPatcherExtension::class, target, javaToolchainService)
target.gradle.sharedServices.registerIfAbsent("download", DownloadService::class) {}

View file

@ -22,6 +22,7 @@
package io.papermc.paperweight.patcher
import io.papermc.paperweight.extension.AbstractJavaLauncherHolder
import io.papermc.paperweight.patcher.upstream.DefaultPaperRepoPatcherUpstream
import io.papermc.paperweight.patcher.upstream.DefaultPatcherUpstream
import io.papermc.paperweight.patcher.upstream.DefaultRepoPatcherUpstream
@ -43,10 +44,17 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskContainer
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
@Suppress("unused", "MemberVisibilityCanBePrivate")
open class PaperweightPatcherExtension(project: Project, private val objects: ObjectFactory, layout: ProjectLayout, tasks: TaskContainer) {
abstract class PaperweightPatcherExtension(
project: Project,
private val objects: ObjectFactory,
layout: ProjectLayout,
tasks: TaskContainer,
javaToolchainService: JavaToolchainService
) : AbstractJavaLauncherHolder(project, javaToolchainService) {
val serverProject: Property<Project> = objects.property()

View file

@ -82,13 +82,13 @@ abstract class PaperweightUser : Plugin<Project> {
}
val userdev = target.extensions.create(
PAPERWEIGHT_EXTENSION,
PaperweightUserExtension::class,
PAPERWEIGHT_EXTENSION,
PaperweightUserExtensionImpl::class,
target,
workerExecutor,
javaToolchainService,
target.provider { userdevSetup },
target.objects
target.provider { userdevSetup }
)
target.dependencies.extensions.create(
@ -253,5 +253,5 @@ abstract class PaperweightUser : Plugin<Project> {
}
private fun createContext(project: Project): SetupHandler.Context =
SetupHandler.Context(project, workerExecutor, javaToolchainService)
SetupHandler.Context(project, workerExecutor)
}

View file

@ -22,40 +22,28 @@
package io.papermc.paperweight.userdev
import io.papermc.paperweight.userdev.internal.setup.SetupHandler
import io.papermc.paperweight.util.*
import org.gradle.api.Project
import io.papermc.paperweight.extension.JavaLauncherHolder
import org.gradle.api.file.RegularFile
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
import org.gradle.workers.WorkerExecutor
import org.gradle.jvm.toolchain.JavaLauncher
/**
* Extension exposing configuration and other APIs for paperweight userdev.
*/
abstract class PaperweightUserExtension(
project: Project,
workerExecutor: WorkerExecutor,
javaToolchainService: JavaToolchainService,
setup: Provider<SetupHandler>,
objects: ObjectFactory
) {
interface PaperweightUserExtension : JavaLauncherHolder {
/**
* Whether to inject the Paper maven repository for use by the dev bundle configuration.
*
* True by default to allow easily resolving Paper development bundles.
*/
val injectPaperRepository: Property<Boolean> = objects.property<Boolean>().convention(true)
val injectPaperRepository: Property<Boolean>
/**
* The [ReobfArtifactConfiguration] is responsible for setting the input and output jars for `reobfJar`,
* as well as changing the classifiers of other jars (i.e. `jar` or `shadowJar`).
*/
val reobfArtifactConfiguration: Property<ReobfArtifactConfiguration> = objects.property<ReobfArtifactConfiguration>()
.convention(ReobfArtifactConfiguration.REOBF_PRODUCTION)
val reobfArtifactConfiguration: Property<ReobfArtifactConfiguration>
/**
* Provides a runnable Mojang mapped server jar, extracted from the current dev bundle.
@ -65,14 +53,17 @@ abstract class PaperweightUserExtension(
replaceWith = ReplaceWith("project.configurations.mojangMappedServerRuntime"),
level = DeprecationLevel.WARNING
)
val mojangMappedServerJar: Provider<RegularFile> = objects.fileProperty().pathProvider(
setup.map { it.serverJar(SetupHandler.Context(project, workerExecutor, javaToolchainService)) }
).withDisallowChanges().withDisallowUnsafeRead()
val mojangMappedServerJar: Provider<RegularFile>
/**
* Provides the Minecraft version of the current dev bundle.
*/
val minecraftVersion: Provider<String> = objects.property<String>().value(
setup.map { it.minecraftVersion }
).withDisallowChanges().withDisallowUnsafeRead()
val minecraftVersion: Provider<String>
/**
* The default [JavaLauncher] for paperweight.
*
* Used for executing setup, and as the default launcher for `reobfJar`.
*/
override val javaLauncher: Property<JavaLauncher>
}

View file

@ -0,0 +1,58 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package io.papermc.paperweight.userdev
import io.papermc.paperweight.extension.AbstractJavaLauncherHolder
import io.papermc.paperweight.userdev.internal.setup.SetupHandler
import io.papermc.paperweight.util.*
import org.gradle.api.Project
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.kotlin.dsl.*
import org.gradle.workers.WorkerExecutor
abstract class PaperweightUserExtensionImpl(
project: Project,
projectLayout: ProjectLayout,
workerExecutor: WorkerExecutor,
javaToolchainService: JavaToolchainService,
setup: Provider<SetupHandler>
) : PaperweightUserExtension, AbstractJavaLauncherHolder(project, javaToolchainService) {
@Suppress("OVERRIDE_DEPRECATION")
override val mojangMappedServerJar: Provider<RegularFile> = projectLayout.file(
setup.map { it.serverJar(SetupHandler.Context(project, workerExecutor)).toFile() }
)
override val minecraftVersion: Provider<String> = setup.map { it.minecraftVersion }
init {
init()
}
private fun init() {
reobfArtifactConfiguration.convention(ReobfArtifactConfiguration.REOBF_PRODUCTION).finalizeValueOnRead()
injectPaperRepository.convention(true).finalizeValueOnRead()
}
}

View file

@ -32,7 +32,6 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.DependencySet
import org.gradle.api.artifacts.repositories.IvyArtifactRepository
import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.workers.WorkerExecutor
interface SetupHandler {
@ -64,11 +63,10 @@ interface SetupHandler {
data class Context(
val project: Project,
val workerExecutor: WorkerExecutor,
val javaToolchainService: JavaToolchainService
val workerExecutor: WorkerExecutor
) {
val defaultJavaLauncher: JavaLauncher
get() = javaToolchainService.defaultJavaLauncher(project).get()
val javaLauncher: JavaLauncher
get() = project.defaultJavaLauncher().get()
}
companion object {

View file

@ -42,7 +42,7 @@ class AccessTransformMinecraft(
outputJarPath = outputJar,
atFilePath = at,
workerExecutor = context.workerExecutor,
launcher = context.defaultJavaLauncher
launcher = context.javaLauncher
).await()
}
}

View file

@ -52,7 +52,7 @@ class DecompileMinecraft(
inputJar = inputJar,
libraries = minecraftLibraryJars(),
outputJar = outputJar,
javaLauncher = context.defaultJavaLauncher
javaLauncher = context.javaLauncher
)
}

View file

@ -40,7 +40,7 @@ class FixMinecraftJar(
override fun run(context: SetupHandler.Context) {
fixJar(
workerExecutor = context.workerExecutor,
launcher = context.defaultJavaLauncher,
launcher = context.javaLauncher,
vanillaJarPath = vanillaServerJar,
inputJarPath = inputJar,
outputJarPath = outputJar,

View file

@ -49,7 +49,7 @@ class GenerateMappingsStep(
paramMappingsPath = paramMappings,
outputMappingsPath = outputMappings,
workerExecutor = context.workerExecutor,
launcher = context.defaultJavaLauncher
launcher = context.javaLauncher
).await()
}

View file

@ -55,7 +55,7 @@ class RemapMinecraft(
remapClasspath = minecraftLibraryJars(),
remapper = remapper,
outputJar = outputJar,
launcher = context.defaultJavaLauncher,
launcher = context.javaLauncher,
workingDir = cache
)
}

View file

@ -71,7 +71,7 @@ class RunPaperclip(
cache.createDirectories()
mojangJar.copyTo(cache.resolve("mojang_$minecraftVersion.jar"))
context.defaultJavaLauncher.runJar(
context.javaLauncher.runJar(
classpath = context.project.files(paperclip),
workingDir = work,
logFile = logFile,