Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support including project dependencies in the dev bundle #163

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = io.papermc.paperweight
version = 1.3.9-SNAPSHOT
version = 1.4.0-SNAPSHOT

org.gradle.caching = true
org.gradle.parallel = true
3 changes: 2 additions & 1 deletion paperweight-core/src/main/kotlin/PaperweightCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ class PaperweightCore : Plugin<Project> {
tasks.extractFromBundler.map { it.serverLibrariesList.path },
tasks.downloadServerJar.map { it.outputJar.path },
tasks.mergeAdditionalAts.map { it.outputFile.path },
tasks.extractFromBundler.map { it.versionJson.path }.convertToFileProvider(layout, providers)
tasks.extractFromBundler.map { it.versionJson.path }.convertToFileProvider(layout, providers),
ext.devBundle,
) {
vanillaJarIncludes.set(ext.vanillaJarIncludes)
reobfMappingsFile.set(tasks.patchReobfMappings.flatMap { it.outputMappings })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package io.papermc.paperweight.core.extension

import io.papermc.paperweight.extension.DevBundleExtension
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import java.util.Locale
Expand Down Expand Up @@ -74,4 +75,12 @@ open class PaperweightCoreExtension(project: Project, objects: ObjectFactory, la
fun paper(action: Action<in PaperExtension>) {
action.execute(paper)
}

@Suppress("MemberVisibilityCanBePrivate")
val devBundle = DevBundleExtension(project, objects)

@Suppress("unused")
fun devBundle(action: Action<in DevBundleExtension>) {
action.execute(devBundle)
}
}
56 changes: 56 additions & 0 deletions paperweight-lib/src/main/kotlin/extension/DevBundleExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2021 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.tasks.*
import org.gradle.api.Project
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.kotlin.dsl.*

@Suppress("unused")
class DevBundleExtension(
private val rootProject: Project,
objects: ObjectFactory
) {
val libraryRepositories: ListProperty<String> = objects.listProperty()

/**
* Registers a project dependency to have its publication included in the dev bundle, and it's coordinates
* depended on by the server artifact. Paper registers `paper-api` and `paper-mojangapi` using this method.
*/
fun registerProjectPublication(project: Project, publicationName: String, coordinates: String) {
rootProject.registerProjectPublicationForDevBundle(project, publicationName, coordinates)
}

private fun Project.registerProjectPublicationForDevBundle(
project: Project,
publicationName: String,
coordinates: String,
) {
val archive = project.archivePublication(publicationName)
tasks.named<GenerateDevBundle>("generateDevelopmentBundle") {
projectArchivedPublication(project, archive, coordinates)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package io.papermc.paperweight.taskcontainers

import io.papermc.paperweight.extension.DevBundleExtension
import io.papermc.paperweight.extension.RelocationExtension
import io.papermc.paperweight.taskcontainers.BundlerJarTasks.Companion.registerVersionArtifact
import io.papermc.paperweight.tasks.*
Expand Down Expand Up @@ -72,6 +73,7 @@ class DevBundleTasks(
vanillaBundlerJarFile: Provider<Path?>,
accessTransformFile: Provider<Path?>,
versionJsonFile: Provider<RegularFile>,
devBundleExtension: DevBundleExtension,
devBundleConfiguration: GenerateDevBundle.() -> Unit
) {
serverBundlerForDevBundle {
Expand Down Expand Up @@ -102,7 +104,6 @@ class DevBundleTasks(
}
)

serverVersion.set(serverProj.version.toString())
serverCoordinates.set(GenerateDevBundle.createCoordinatesFor(serverProj))
serverProject.set(serverProj)
runtimeConfiguration.set(serverProj.configurations.named(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME))
Expand All @@ -111,6 +112,8 @@ class DevBundleTasks(
decompiledJar.pathProvider(decompileJar)
atFile.pathProvider(accessTransformFile)

libraryRepositories.addAll(devBundleExtension.libraryRepositories)

devBundleConfiguration(this)
}
}
Expand Down
72 changes: 72 additions & 0 deletions paperweight-lib/src/main/kotlin/tasks/ArchivePublication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2021 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.tasks

import io.papermc.paperweight.util.*
import java.nio.file.Path
import kotlin.io.path.*
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.*
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "Not worth caching")
abstract class ArchivePublication : ZippedTask() {
@get:InputDirectory
abstract val repo: DirectoryProperty

override fun run(rootDir: Path) {
repo.path.copyRecursivelyTo(rootDir)
}
}

fun Project.archivePublication(publicationName: String): TaskProvider<ArchivePublication> {
val repoName = "archive${publicationName.capitalize()}PublicationTempRepo"
val repoDir = layout.buildDirectory.dir("tmp/$repoName")
the<PublishingExtension>().repositories {
maven {
name = repoName
setUrl(repoDir)
}
}
val cleanTask = tasks.register<Delete>("clean${repoName.capitalize()}") {
delete(repoDir)
doLast {
repoDir.get().path.createDirectories()
}
}
val archiveTask = tasks.register<ArchivePublication>("archive${publicationName.capitalize()}Publication") {
repo.set(repoDir)
}
tasks.all {
if (name == "publish${publicationName.capitalize()}PublicationTo${repoName.capitalize()}Repository") {
dependsOn(cleanTask)
archiveTask.get().dependsOn(this)
}
}
return archiveTask
}
6 changes: 3 additions & 3 deletions paperweight-lib/src/main/kotlin/tasks/CreateBundlerJar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract class CreateBundlerJar : ZippedTask() {
abstract val mainClass: Property<String>

@get:Nested
val versionArtifacts: NamedDomainObjectContainer<VersionArtifact> = createVersionArtifactContainer()
val versionArtifacts: NamedDomainObjectContainer<VersionArtifact> = createNamedDomainObjectContainer()

@get:Classpath
@get:Optional
Expand All @@ -75,8 +75,8 @@ abstract class CreateBundlerJar : ZippedTask() {
@get:OutputFile
abstract val libraryChangesJson: RegularFileProperty

private fun createVersionArtifactContainer(): NamedDomainObjectContainer<VersionArtifact> =
objects.domainObjectContainer(VersionArtifact::class) { objects.newInstance(it) }
private inline fun <reified T : Any> createNamedDomainObjectContainer(): NamedDomainObjectContainer<T> =
objects.domainObjectContainer(T::class.java) { objects.newInstance(it) }

override fun init() {
super.init()
Expand Down
73 changes: 51 additions & 22 deletions paperweight-lib/src/main/kotlin/tasks/GenerateDevBundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,14 @@ import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Path
import java.util.Locale
import javax.inject.Inject
import kotlin.io.path.*
import org.apache.tools.ant.types.selectors.SelectorUtils
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedArtifactResult
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.ListProperty
Expand All @@ -52,10 +49,12 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskProvider

abstract class GenerateDevBundle : DefaultTask() {
abstract class GenerateDevBundle : BaseTask() {

@get:InputFile
abstract val decompiledJar: RegularFileProperty
Expand All @@ -69,18 +68,9 @@ abstract class GenerateDevBundle : DefaultTask() {
@get:InputFile
abstract val mojangMappedPaperclipFile: RegularFileProperty

@get:Input
abstract val serverVersion: Property<String>

@get:Input
abstract val serverCoordinates: Property<String>

@get:Input
abstract val apiCoordinates: Property<String>

@get:Input
abstract val mojangApiCoordinates: Property<String>

@get:Input
abstract val vanillaJarIncludes: ListProperty<String>

Expand Down Expand Up @@ -126,8 +116,36 @@ abstract class GenerateDevBundle : DefaultTask() {
@get:OutputFile
abstract val devBundleFile: RegularFileProperty

@get:Inject
abstract val layout: ProjectLayout
@get:Nested
abstract val archivedMavenPublications: ListProperty<ArchivedPublication>

data class ArchivedPublication(
@get:Input
val name: String,
@get:InputFile
val file: RegularFileProperty,
@get:Input
val coordinates: String,
)

fun archivedPublication(
name: String,
archivedPublication: TaskProvider<ArchivePublication>,
coordinates: String
) {
val publication = ArchivedPublication(
name,
objects.fileProperty().convention(archivedPublication.flatMap { it.outputZip }),
coordinates,
)
archivedMavenPublications.add(publication)
}

fun projectArchivedPublication(
project: Project,
archivedPublication: TaskProvider<ArchivePublication>,
coordinates: String
) = archivedPublication(project.name, archivedPublication, coordinates)

@TaskAction
fun run() {
Expand All @@ -141,7 +159,8 @@ abstract class GenerateDevBundle : DefaultTask() {

val dataDir = "data"
val patchesDir = "patches"
val config = createBundleConfig(dataDir, patchesDir)
val archivedDir = "$dataDir/archivedMavenPublications"
val config = createBundleConfig(dataDir, patchesDir, archivedDir)

devBundle.writeZip().use { zip ->
zip.getPath("config.json").bufferedWriter(Charsets.UTF_8).use { writer ->
Expand All @@ -155,6 +174,12 @@ abstract class GenerateDevBundle : DefaultTask() {
mojangMappedPaperclipFile.path.copyTo(dataZip.resolve(mojangMappedPaperclipFileName))
atFile.path.copyTo(dataZip.resolve(atFileName))

archivedMavenPublications.get().forEach { archived ->
val pubs = zip.getPath(archivedDir)
pubs.createDirectories()
archived.file.path.copyTo(pubs.resolve("${archived.name}.zip"))
}

val patchesZip = zip.getPath(patchesDir)
tempPatchDir.copyRecursivelyTo(patchesZip)
}
Expand Down Expand Up @@ -298,12 +323,16 @@ abstract class GenerateDevBundle : DefaultTask() {
}

@Suppress("SameParameterValue")
private fun createBundleConfig(dataTargetDir: String, patchTargetDir: String): DevBundleConfig {
private fun createBundleConfig(
dataTargetDir: String,
patchTargetDir: String,
archivedDir: String,
): DevBundleConfig {
return DevBundleConfig(
minecraftVersion = minecraftVersion.get(),
mappedServerCoordinates = serverCoordinates.get(),
apiCoordinates = "${apiCoordinates.get()}:${serverVersion.get()}",
mojangApiCoordinates = "${mojangApiCoordinates.get()}:${serverVersion.get()}",
archivedPublications = archivedMavenPublications.get().associate { it.name to it.coordinates },
archivedPublicationsDir = archivedDir,
buildData = createBuildDataConfig(dataTargetDir),
decompile = createDecompileRunner(),
remapper = createRemapDep(),
Expand Down Expand Up @@ -386,8 +415,8 @@ abstract class GenerateDevBundle : DefaultTask() {
data class DevBundleConfig(
val minecraftVersion: String,
val mappedServerCoordinates: String,
val apiCoordinates: String,
val mojangApiCoordinates: String,
val archivedPublications: Map<String, String>,
val archivedPublicationsDir: String,
val buildData: BuildData,
val decompile: Runner,
val remapper: MavenDep,
Expand Down Expand Up @@ -416,7 +445,7 @@ abstract class GenerateDevBundle : DefaultTask() {
const val mojangMappedPaperclipFileName = "paperclip-$DEOBF_NAMESPACE.jar"

// Should be bumped when the dev bundle config/contents changes in a way which will require users to update paperweight
const val currentDataVersion = 3
const val currentDataVersion = 4

fun createCoordinatesFor(project: Project): String =
sequenceOf(project.group, project.name.toLowerCase(Locale.ENGLISH), "userdev-" + project.version).joinToString(":")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const val FINAL_DECOMPILE_JAR = "$TASK_CACHE/decompileJar.jar"
const val MC_DEV_SOURCES_DIR = "$PAPER_PATH/mc-dev-sources"

const val IVY_REPOSITORY = "$PAPER_PATH/ivyRepository"
const val MAVEN_REPOSITORY = "$PAPER_PATH/mavenRepository"

const val RELOCATION_EXTENSION = "relocation"

Expand Down
3 changes: 2 additions & 1 deletion paperweight-patcher/src/main/kotlin/PaperweightPatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ class PaperweightPatcher : Plugin<Project> {
upstreamData.map { it.serverLibrariesList },
upstreamData.map { it.vanillaJar },
upstreamData.map { it.accessTransform },
upstreamData.map { it.bundlerVersionJson }.convertToFileProvider(layout, providers)
upstreamData.map { it.bundlerVersionJson }.convertToFileProvider(layout, providers),
patcher.devBundle,
) {
vanillaJarIncludes.set(upstreamData.map { it.vanillaIncludes })
reobfMappingsFile.set(patchReobfMappings.flatMap { it.outputMappings })
Expand Down
Loading