Skip to content

Commit e2050cd

Browse files
committed
Update to new plugin system
1 parent ba8a4aa commit e2050cd

File tree

5 files changed

+105
-58
lines changed

5 files changed

+105
-58
lines changed

example/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies {
1111
// And Kotlin is best dependency <3
1212
implementation(kotlin("stdlib", "2.0.21"))
1313
implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.7.3")
14+
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.9.0")
1415
}
1516

1617
java {

src/main/kotlin/LavalinkExtension.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,29 @@ interface LavalinkExtension {
4040
/**
4141
* The plugins root package (if different to [Project.getGroup]).
4242
*/
43+
@Deprecated("This property is no longer required")
4344
val path: Property<String>
4445

46+
/**
47+
* The version of Lavalink this plugin requires (if different to [apiVersion]).
48+
*/
49+
val requires: Property<String>
50+
51+
/**
52+
* An optional description of the plugin.
53+
*/
54+
val description: Property<String>
55+
56+
/**
57+
* An optional mention of the plugin's author.
58+
*/
59+
val provider: Property<String>
60+
61+
/**
62+
* An optional license of the plugin.
63+
*/
64+
val license: Property<String>
65+
4566
/**
4667
* Whether to configure publishing automatically or nor.
4768
*/

src/main/kotlin/LavalinkGradlePlugin.kt

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import dev.arbjerg.lavalink.gradle.tasks.*
44
import org.gradle.api.Plugin
55
import org.gradle.api.Project
66
import org.gradle.api.artifacts.Dependency
7+
import org.gradle.api.artifacts.ModuleVersionIdentifier
78
import org.gradle.api.provider.Provider
89
import org.gradle.api.publish.PublishingExtension
910
import org.gradle.api.publish.maven.MavenPublication
1011
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
1112
import org.gradle.api.tasks.Copy
1213
import org.gradle.api.tasks.SourceSetContainer
13-
import org.gradle.jvm.tasks.Jar
14+
import org.gradle.api.tasks.bundling.Jar
15+
import org.gradle.api.tasks.bundling.Zip
1416
import org.gradle.kotlin.dsl.*
17+
import org.gradle.language.base.plugins.LifecycleBasePlugin
1518

1619
private const val lavalinkExtensionName = "lavalinkPlugin"
1720

@@ -22,10 +25,10 @@ class LavalinkGradlePlugin : Plugin<Project> {
2225
override fun apply(target: Project) {
2326
with(target) {
2427
check(plugins.hasPlugin("org.gradle.java")) { "Please apply the Java/Kotlin plugin before Lavalink" }
25-
configureExtension()
28+
val extension = configureExtension()
2629
configurePublishing()
2730
val serverDependency = configureDependencies()
28-
configureTasks(serverDependency)
31+
configureTasks(extension, serverDependency)
2932
configureSourceSets()
3033
}
3134
}
@@ -36,12 +39,14 @@ class LavalinkGradlePlugin : Plugin<Project> {
3639
}
3740

3841
private fun Project.configureExtension(): LavalinkExtension {
42+
@Suppress("DEPRECATION")
3943
return extensions.create<LavalinkExtension>(lavalinkExtensionName).apply {
4044
version.convention(provider { project.version.toString() })
4145
name.convention(project.name)
4246
path.convention(provider { project.group.toString() })
4347
serverVersion.convention(apiVersion)
4448
configurePublishing.convention(true)
49+
requires.convention(serverVersion)
4550
}
4651
}
4752

@@ -75,6 +80,7 @@ private fun Project.configurePublishing() {
7580
publications {
7681
create<MavenPublication>("maven") {
7782
from(components["java"])
83+
artifact(tasks.named("assemblePlugin"))
7884
}
7985
}
8086
}
@@ -92,67 +98,79 @@ private fun Project.configureSourceSets() {
9298
}
9399
}
94100

95-
private fun Project.configureTasks(serverDependency: Provider<Dependency>) {
101+
private fun Project.configureTasks(extension: LavalinkExtension, serverDependency: Provider<Dependency>) {
96102
tasks {
97103
val generatePluginProperties by registering(GeneratePluginPropertiesTask::class)
98104
named("processResources") {
99105
dependsOn(generatePluginProperties)
100106
}
101107

102-
val jar by named<Jar>("jar") {
103-
configurations.getByName("runtimeClasspath")
104-
.incoming
105-
.artifactView {
106-
// componentFilter { it !is ProjectComponentIdentifier }
107-
}.artifacts
108-
.forEach {
109-
from(zipTree(it.file)) {
110-
exclude("META-INF/**")
108+
val jar by getting(Jar::class)
109+
val collectPluginDependencies by registering(Copy::class) {
110+
val destinationDirectory = layout.buildDirectory.dir("dependencies")
111+
delete(destinationDirectory) // Delete old data
112+
113+
from({
114+
val dependency =
115+
dependencies.create("dev.arbjerg.lavalink:Lavalink-Server:${extension.serverVersion.get()}") {
116+
// Old sedmelluq artifacts are still referenced at some places
117+
// but do not resolve anymore since jcenter is dead
118+
exclude(group = "com.sedmelluq")
111119
}
112-
}
113120

114-
// configurations.getByName("runtimeClasspath")
115-
// .allDependencies
116-
// .filterIsInstance<ProjectDependency>()
117-
// .forEach { dependency ->
118-
// val project = dependency.dependencyProject
119-
// if (project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
120-
// val compilationName = provider {
121-
// project.extensions.getByType<KotlinMultiplatformExtension>()
122-
// .targets
123-
// .first { it is KotlinJvmTarget }
124-
// .name
125-
// }
126-
// dependsOn(compilationName.flatMap { project.tasks.named("${it}MainClasses") })
127-
// from(compilationName.flatMap { targetName -> project.layout.buildDirectory.file("classes/kotlin/$targetName/main") }) {
128-
// include("**/*.class")
129-
// }
130-
// } else {
131-
// dependsOn(project.tasks.named("classes"))
132-
// from(project.layout.buildDirectory.dir("classes")) {
133-
// include("**/main/**/*.class")
134-
// eachFile {
135-
// path = path.substringAfter("main/")
136-
// }
137-
// }
138-
// }
139-
// }
121+
// Collect all dependencies lavalink depends on
122+
val serverDependencies = configurations
123+
.detachedConfiguration(dependency)
124+
.resolvedConfiguration
125+
.resolvedArtifacts
126+
.map { it.moduleVersion.id.dependencyNotation }
127+
128+
// Remove them from the jar, to avoid conflicts
129+
configurations.getByName("runtimeClasspath")
130+
.resolvedConfiguration
131+
.resolvedArtifacts
132+
.asSequence()
133+
.filter { it.moduleVersion.id.dependencyNotation !in serverDependencies }
134+
.mapNotNull { it.file }
135+
.toList()
136+
137+
})
138+
into(destinationDirectory)
140139
}
141140

142-
val installPlugin by registering(Copy::class) {
143-
from(jar)
144-
into(project.testServerFolder)
145-
// This always deletes old versions of the plugin in the test server
146-
// So we don't install the same plugin twice
147-
rename { "plugin.jar" }
141+
register<Zip>("assemblePlugin") {
142+
group = LifecycleBasePlugin.BUILD_GROUP
143+
destinationDirectory = layout.buildDirectory.dir("distributions")
144+
archiveBaseName = extension.name.map { "plugin-$it" }
145+
146+
dependsOn(jar)
147+
148+
into("classes") {
149+
with(jar)
150+
exclude("plugin.properties")
151+
// Do not include legacy manifest
152+
exclude("lavalink-plugins/**")
153+
}
154+
155+
into("lib") {
156+
from(collectPluginDependencies)
157+
}
158+
159+
from(generatePluginProperties)
148160
}
149161

150162
val downloadLavalink by registering(DownloadLavalinkTask::class) {
151163
dependencyProvider = serverDependency
152164
}
153165

166+
val classes by existing
167+
val processResources by existing
168+
154169
register<RunLavalinkTask>("runLavaLink") {
155-
dependsOn(installPlugin, downloadLavalink)
170+
dependsOn(downloadLavalink, classes, processResources)
156171
}
157172
}
158173
}
174+
175+
val ModuleVersionIdentifier.dependencyNotation: String
176+
get() = "$group:$name"

src/main/kotlin/tasks/GeneratePluginPropertiesTask.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ abstract class GeneratePluginPropertiesTask : DefaultTask() {
2222
init {
2323
group = LifecycleBasePlugin.BUILD_GROUP
2424
val extension = project.extension
25+
@Suppress("DEPRECATION")
2526
inputs.properties(
2627
"version" to extension.version,
2728
"name" to extension.name,
2829
"path" to extension.path,
30+
"requires" to extension.requires,
31+
"provider" to extension.provider.orElse(""),
32+
"license" to extension.license.orElse(""),
2933
)
3034

3135
outputs.dir(project.generatedPluginManifest)
@@ -37,15 +41,23 @@ abstract class GeneratePluginPropertiesTask : DefaultTask() {
3741
@TaskAction
3842
fun generateTask() {
3943
val properties = Properties().apply {
40-
set("version", extension.version.get())
41-
set("name", extension.name.get())
42-
set("path", extension.path.get())
44+
set("plugin.id", extension.name.get())
45+
set("plugin.version", extension.version.get())
46+
set("plugin.requires", extension.requires.get())
47+
setIfPresent("plugin.provider", extension.provider)
48+
setIfPresent("plugin.license", extension.license)
4349
}
4450

45-
val file = generatedPluginManifest.get().asFile.toPath() / "lavalink-plugins" / "${extension.name.get()}.properties"
51+
val file = generatedPluginManifest.get().asFile.toPath() / "plugin.properties"
4652
file.parent.createDirectories()
4753
file.bufferedWriter(options = arrayOf(StandardOpenOption.CREATE)).use { writer ->
4854
properties.store(writer, null)
4955
}
5056
}
5157
}
58+
59+
private fun Properties.setIfPresent(name: String, value: Provider<String>) {
60+
if (value.isPresent) {
61+
setProperty(name, value.get())
62+
}
63+
}

src/main/kotlin/tasks/RunLavalinkTask.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,25 @@ package dev.arbjerg.lavalink.gradle.tasks
33
import dev.arbjerg.lavalink.gradle.LavalinkGradlePlugin
44
import org.gradle.api.Project
55
import org.gradle.api.tasks.JavaExec
6-
import org.gradle.api.tasks.OutputDirectory
76
import org.gradle.api.tasks.TaskAction
87
import org.gradle.kotlin.dsl.assign
98
import org.gradle.kotlin.dsl.environment
109

11-
internal val Project.testServerFolder
12-
get() = project.layout.buildDirectory.dir("lavalink-test-server-plugins")
13-
1410
abstract class RunLavalinkTask : JavaExec() {
1511
init {
1612
mainClass = "org.springframework.boot.loader.JarLauncher"
1713
group = LavalinkGradlePlugin.TASK_GROUP_NAME
1814
outputs.upToDateWhen { false }
1915
}
2016

21-
private val workingDir = project.rootDir
22-
private val testServerFolder = project.testServerFolder
17+
private val workingDir = project.layout.projectDirectory
2318
private val lavalinkJar = project.lavalinkJar.map { project.files(it) }
2419

2520
@TaskAction
2621
override fun exec() {
2722
workingDir(workingDir)
2823
configureClassPath()
29-
environment("lavalink.pluginsDir" to testServerFolder.get())
24+
environment("lavalink.plugins.developmentMode" to true)
3025
super.exec()
3126
}
3227

0 commit comments

Comments
 (0)