Skip to content

Commit

Permalink
Update to new plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Nov 12, 2024
1 parent ba8a4aa commit e2050cd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 58 deletions.
1 change: 1 addition & 0 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
// And Kotlin is best dependency <3
implementation(kotlin("stdlib", "2.0.21"))
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.7.3")
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.9.0")
}

java {
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/LavalinkExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,29 @@ interface LavalinkExtension {
/**
* The plugins root package (if different to [Project.getGroup]).
*/
@Deprecated("This property is no longer required")
val path: Property<String>

/**
* The version of Lavalink this plugin requires (if different to [apiVersion]).
*/
val requires: Property<String>

/**
* An optional description of the plugin.
*/
val description: Property<String>

/**
* An optional mention of the plugin's author.
*/
val provider: Property<String>

/**
* An optional license of the plugin.
*/
val license: Property<String>

/**
* Whether to configure publishing automatically or nor.
*/
Expand Down
112 changes: 65 additions & 47 deletions src/main/kotlin/LavalinkGradlePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import dev.arbjerg.lavalink.gradle.tasks.*
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.provider.Provider
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.jvm.tasks.Jar
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.bundling.Zip
import org.gradle.kotlin.dsl.*
import org.gradle.language.base.plugins.LifecycleBasePlugin

private const val lavalinkExtensionName = "lavalinkPlugin"

Expand All @@ -22,10 +25,10 @@ class LavalinkGradlePlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
check(plugins.hasPlugin("org.gradle.java")) { "Please apply the Java/Kotlin plugin before Lavalink" }
configureExtension()
val extension = configureExtension()
configurePublishing()
val serverDependency = configureDependencies()
configureTasks(serverDependency)
configureTasks(extension, serverDependency)
configureSourceSets()
}
}
Expand All @@ -36,12 +39,14 @@ class LavalinkGradlePlugin : Plugin<Project> {
}

private fun Project.configureExtension(): LavalinkExtension {
@Suppress("DEPRECATION")
return extensions.create<LavalinkExtension>(lavalinkExtensionName).apply {
version.convention(provider { project.version.toString() })
name.convention(project.name)
path.convention(provider { project.group.toString() })
serverVersion.convention(apiVersion)
configurePublishing.convention(true)
requires.convention(serverVersion)
}
}

Expand Down Expand Up @@ -75,6 +80,7 @@ private fun Project.configurePublishing() {
publications {
create<MavenPublication>("maven") {
from(components["java"])
artifact(tasks.named("assemblePlugin"))
}
}
}
Expand All @@ -92,67 +98,79 @@ private fun Project.configureSourceSets() {
}
}

private fun Project.configureTasks(serverDependency: Provider<Dependency>) {
private fun Project.configureTasks(extension: LavalinkExtension, serverDependency: Provider<Dependency>) {
tasks {
val generatePluginProperties by registering(GeneratePluginPropertiesTask::class)
named("processResources") {
dependsOn(generatePluginProperties)
}

val jar by named<Jar>("jar") {
configurations.getByName("runtimeClasspath")
.incoming
.artifactView {
// componentFilter { it !is ProjectComponentIdentifier }
}.artifacts
.forEach {
from(zipTree(it.file)) {
exclude("META-INF/**")
val jar by getting(Jar::class)
val collectPluginDependencies by registering(Copy::class) {
val destinationDirectory = layout.buildDirectory.dir("dependencies")
delete(destinationDirectory) // Delete old data

from({
val dependency =
dependencies.create("dev.arbjerg.lavalink:Lavalink-Server:${extension.serverVersion.get()}") {
// Old sedmelluq artifacts are still referenced at some places
// but do not resolve anymore since jcenter is dead
exclude(group = "com.sedmelluq")
}
}

// configurations.getByName("runtimeClasspath")
// .allDependencies
// .filterIsInstance<ProjectDependency>()
// .forEach { dependency ->
// val project = dependency.dependencyProject
// if (project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
// val compilationName = provider {
// project.extensions.getByType<KotlinMultiplatformExtension>()
// .targets
// .first { it is KotlinJvmTarget }
// .name
// }
// dependsOn(compilationName.flatMap { project.tasks.named("${it}MainClasses") })
// from(compilationName.flatMap { targetName -> project.layout.buildDirectory.file("classes/kotlin/$targetName/main") }) {
// include("**/*.class")
// }
// } else {
// dependsOn(project.tasks.named("classes"))
// from(project.layout.buildDirectory.dir("classes")) {
// include("**/main/**/*.class")
// eachFile {
// path = path.substringAfter("main/")
// }
// }
// }
// }
// Collect all dependencies lavalink depends on
val serverDependencies = configurations
.detachedConfiguration(dependency)
.resolvedConfiguration
.resolvedArtifacts
.map { it.moduleVersion.id.dependencyNotation }

// Remove them from the jar, to avoid conflicts
configurations.getByName("runtimeClasspath")
.resolvedConfiguration
.resolvedArtifacts
.asSequence()
.filter { it.moduleVersion.id.dependencyNotation !in serverDependencies }
.mapNotNull { it.file }
.toList()

})
into(destinationDirectory)
}

val installPlugin by registering(Copy::class) {
from(jar)
into(project.testServerFolder)
// This always deletes old versions of the plugin in the test server
// So we don't install the same plugin twice
rename { "plugin.jar" }
register<Zip>("assemblePlugin") {
group = LifecycleBasePlugin.BUILD_GROUP
destinationDirectory = layout.buildDirectory.dir("distributions")
archiveBaseName = extension.name.map { "plugin-$it" }

dependsOn(jar)

into("classes") {
with(jar)
exclude("plugin.properties")
// Do not include legacy manifest
exclude("lavalink-plugins/**")
}

into("lib") {
from(collectPluginDependencies)
}

from(generatePluginProperties)
}

val downloadLavalink by registering(DownloadLavalinkTask::class) {
dependencyProvider = serverDependency
}

val classes by existing
val processResources by existing

register<RunLavalinkTask>("runLavaLink") {
dependsOn(installPlugin, downloadLavalink)
dependsOn(downloadLavalink, classes, processResources)
}
}
}

val ModuleVersionIdentifier.dependencyNotation: String
get() = "$group:$name"
20 changes: 16 additions & 4 deletions src/main/kotlin/tasks/GeneratePluginPropertiesTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ abstract class GeneratePluginPropertiesTask : DefaultTask() {
init {
group = LifecycleBasePlugin.BUILD_GROUP
val extension = project.extension
@Suppress("DEPRECATION")
inputs.properties(
"version" to extension.version,
"name" to extension.name,
"path" to extension.path,
"requires" to extension.requires,
"provider" to extension.provider.orElse(""),
"license" to extension.license.orElse(""),
)

outputs.dir(project.generatedPluginManifest)
Expand All @@ -37,15 +41,23 @@ abstract class GeneratePluginPropertiesTask : DefaultTask() {
@TaskAction
fun generateTask() {
val properties = Properties().apply {
set("version", extension.version.get())
set("name", extension.name.get())
set("path", extension.path.get())
set("plugin.id", extension.name.get())
set("plugin.version", extension.version.get())
set("plugin.requires", extension.requires.get())
setIfPresent("plugin.provider", extension.provider)
setIfPresent("plugin.license", extension.license)
}

val file = generatedPluginManifest.get().asFile.toPath() / "lavalink-plugins" / "${extension.name.get()}.properties"
val file = generatedPluginManifest.get().asFile.toPath() / "plugin.properties"
file.parent.createDirectories()
file.bufferedWriter(options = arrayOf(StandardOpenOption.CREATE)).use { writer ->
properties.store(writer, null)
}
}
}

private fun Properties.setIfPresent(name: String, value: Provider<String>) {
if (value.isPresent) {
setProperty(name, value.get())
}
}
9 changes: 2 additions & 7 deletions src/main/kotlin/tasks/RunLavalinkTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@ package dev.arbjerg.lavalink.gradle.tasks
import dev.arbjerg.lavalink.gradle.LavalinkGradlePlugin
import org.gradle.api.Project
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.environment

internal val Project.testServerFolder
get() = project.layout.buildDirectory.dir("lavalink-test-server-plugins")

abstract class RunLavalinkTask : JavaExec() {
init {
mainClass = "org.springframework.boot.loader.JarLauncher"
group = LavalinkGradlePlugin.TASK_GROUP_NAME
outputs.upToDateWhen { false }
}

private val workingDir = project.rootDir
private val testServerFolder = project.testServerFolder
private val workingDir = project.layout.projectDirectory
private val lavalinkJar = project.lavalinkJar.map { project.files(it) }

@TaskAction
override fun exec() {
workingDir(workingDir)
configureClassPath()
environment("lavalink.pluginsDir" to testServerFolder.get())
environment("lavalink.plugins.developmentMode" to true)
super.exec()
}

Expand Down

0 comments on commit e2050cd

Please sign in to comment.