Skip to content

Commit e181599

Browse files
committed
fix: PluginDependency and PluginDescriptor should implement java.io.Serializable
1 parent 8b57065 commit e181599

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ It is pretty simple to use this plugin. Just add the following code to your `bui
1818
```kt
1919
plugins {
2020
id("java-library")
21-
id("org.allaymc.gradle.plugin").version("0.1.1")
21+
id("org.allaymc.gradle.plugin").version("0.2.0")
2222
}
2323

2424
group = "org.allaymc.gradle.sample"

plugin/src/main/kotlin/org/allaymc/gradle/plugin/AllayPlugin.kt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class AllayPlugin : Plugin<Project> {
5353
}
5454

5555
val shadowJarTask = if (
56-
project.plugins.hasPlugin("com.gradleup.shadow")
57-
|| project.plugins.hasPlugin("com.github.johnrengelman.shadow")
56+
project.plugins.hasPlugin("com.gradleup.shadow") ||
57+
project.plugins.hasPlugin("com.github.johnrengelman.shadow")
5858
) {
5959
project.tasks.named("shadowJar", Jar::class.java)
6060
} else {
@@ -68,22 +68,23 @@ class AllayPlugin : Plugin<Project> {
6868
}
6969

7070
if (extension.generatePluginDescriptor.get()) {
71-
val generatePluginDescriptorTask = project.tasks.register<GeneratePluginDescriptorTask>("generatePluginDescriptor") {
72-
outputFile.set(project.layout.buildDirectory.file("resources/main/plugin.json"))
71+
val generatePluginDescriptorTask =
72+
project.tasks.register<GeneratePluginDescriptorTask>("generatePluginDescriptor") {
73+
outputFile.set(project.layout.buildDirectory.file("resources/main/plugin.json"))
7374

74-
pluginEntrance.set(extension.plugin.entrance)
75-
pluginName.set(extension.plugin.name)
76-
pluginVersion.set(extension.plugin.version)
77-
pluginAuthors.set(extension.plugin.authors)
78-
pluginApiVersion.set(extension.plugin.apiVersion)
79-
pluginDescription.set(extension.plugin.description)
80-
pluginDependencies.set(extension.plugin.dependencies)
81-
pluginWebsite.set(extension.plugin.website)
75+
pluginEntrance.set(extension.plugin.entrance)
76+
pluginName.set(extension.plugin.name)
77+
pluginVersion.set(extension.plugin.version)
78+
pluginAuthors.set(extension.plugin.authors)
79+
pluginApiVersion.set(extension.plugin.apiVersion)
80+
pluginDescription.set(extension.plugin.description)
81+
pluginDependencies.set(extension.plugin.dependencies)
82+
pluginWebsite.set(extension.plugin.website)
8283

83-
projectName.set(project.name)
84-
projectVersion.set(project.version.toString())
85-
projectDescription.set(project.description)
86-
}
84+
projectName.set(project.name)
85+
projectVersion.set(project.version.toString())
86+
projectDescription.set(project.description)
87+
}
8788

8889
project.tasks.named("processResources") {
8990
dependsOn(generatePluginDescriptorTask)

plugin/src/main/kotlin/org/allaymc/gradle/plugin/data/PluginDependency.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package org.allaymc.gradle.plugin.data
33
import kotlinx.serialization.Serializable
44

55
@Serializable
6-
data class PluginDependency(val name: String, val version: String?, val optional: Boolean?)
6+
data class PluginDependency(val name: String, val version: String?, val optional: Boolean?) : java.io.Serializable

plugin/src/main/kotlin/org/allaymc/gradle/plugin/data/PluginDescriptor.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ data class PluginDescriptor(
1414
val description: String?,
1515
val dependencies: List<PluginDependency>?,
1616
val website: String?,
17-
) {
18-
}
17+
) : java.io.Serializable

plugin/src/main/kotlin/org/allaymc/gradle/plugin/dsl/PluginDsl.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.allaymc.gradle.plugin.dsl
33
import org.allaymc.gradle.plugin.data.PluginDependency
44
import org.gradle.api.model.ObjectFactory
55
import org.gradle.api.provider.ListProperty
6-
import org.gradle.api.provider.MapProperty
76
import org.gradle.api.provider.Property
87
import javax.inject.Inject
98

@@ -16,24 +15,21 @@ abstract class PluginDsl @Inject constructor(objects: ObjectFactory) {
1615
val authors: ListProperty<String> = objects.listProperty(String::class.java).convention(emptyList())
1716
fun authors(vararg names: String) = authors.addAll(names.toList())
1817

18+
/* Optional Fields */
19+
1920
val apiVersion: Property<String> = objects.property(String::class.java)
21+
2022
/** Alias for [apiVersion]. */
2123
val api = apiVersion
2224

2325
val description: Property<String> = objects.property(String::class.java)
2426

25-
val dependencies: ListProperty<PluginDependency> =
26-
objects.listProperty(PluginDependency::class.java).convention(emptyList())
27+
val dependencies: ListProperty<PluginDependency> = objects.listProperty(PluginDependency::class.java)
2728
fun dependencies(vararg plugins: PluginDependency) = dependencies.addAll(plugins.toList())
2829
fun dependency(name: String, version: String? = null, optional: Boolean = false) =
29-
PluginDependency(name, version, optional)
30+
dependencies.add(PluginDependency(name, version, optional))
3031

3132
val website: Property<String> = objects.property(String::class.java)
3233

33-
operator fun <K : Any, V : Any> MapProperty<K, V>.set(key: K, value: V?) {
34-
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
35-
if (value != null) put(key, value)
36-
}
37-
3834
operator fun <T : Any> ListProperty<T>.plusAssign(item: T) = addAll(item)
3935
}

plugin/src/main/kotlin/org/allaymc/gradle/plugin/tasks/GeneratePluginDescriptorTask.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.allaymc.gradle.plugin.tasks
22

33
import kotlinx.serialization.ExperimentalSerializationApi
4-
import kotlinx.serialization.InternalSerializationApi
54
import kotlinx.serialization.builtins.MapSerializer
65
import kotlinx.serialization.builtins.serializer
76
import kotlinx.serialization.json.Json
@@ -10,7 +9,6 @@ import kotlinx.serialization.json.jsonObject
109
import org.allaymc.gradle.plugin.data.PluginDependency
1110
import org.allaymc.gradle.plugin.data.PluginDescriptor
1211
import org.gradle.api.DefaultTask
13-
import org.gradle.api.Project
1412
import org.gradle.api.file.RegularFileProperty
1513
import org.gradle.api.provider.ListProperty
1614
import org.gradle.api.provider.Property
@@ -25,7 +23,6 @@ private val Serialization = Json {
2523
prettyPrint = true
2624
}
2725

28-
@OptIn(InternalSerializationApi::class)
2926
abstract class GeneratePluginDescriptorTask : DefaultTask() {
3027
@get:OutputFile
3128
abstract val outputFile: RegularFileProperty
@@ -82,9 +79,12 @@ abstract class GeneratePluginDescriptorTask : DefaultTask() {
8279

8380
private fun createDescriptor(): PluginDescriptor {
8481
return PluginDescriptor(
85-
pluginEntrance.get().ensureEntrance(project),
82+
pluginEntrance.get().takeIf { it.isNotEmpty() }
83+
?.let { if (it.startsWith(".")) "${project.group}$it" else it }
84+
?: error("Entrance is not defined!"),
8685
pluginName.orNull ?: projectName.get().takeUnless { it == "unspecified" } ?: error("Name is not defined!"),
87-
(pluginVersion.orNull ?: projectVersion.get().takeUnless { it == "unspecified" } ?: error("Version is not defined!"))
86+
(pluginVersion.orNull ?: projectVersion.get().takeUnless { it == "unspecified" }
87+
?: error("Version is not defined!"))
8888
.takeIf { it.matches(semVerRegex) } ?: error("Version is invalid! (Please check https://semver.org/)"),
8989
pluginAuthors.getOrElse(emptyList()),
9090
pluginApiVersion.orNull,
@@ -94,15 +94,10 @@ abstract class GeneratePluginDescriptorTask : DefaultTask() {
9494
)
9595
}
9696

97-
private fun String?.ensureEntrance(project: Project) = this
98-
?.takeIf { it.isNotEmpty() }
99-
?.let { if (it.startsWith(".")) "${project.group}$it" else it }
100-
?: error("Entrance is not defined!")
101-
10297
private val semVerRegex =
10398
Regex("^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$")
10499

105-
internal class GenDescriptorException(message: String) : Exception(message)
100+
internal class GeneratePluginDescriptorException(message: String) : Exception(message)
106101

107-
internal fun error(message: String): Nothing = throw GenDescriptorException(message)
102+
internal fun error(message: String): Nothing = throw GeneratePluginDescriptorException(message)
108103
}

sample/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ allay {
1212
plugin {
1313
name = "TestPlugin"
1414
entrance = ".TestPlugin"
15-
authors += "Cdm2883"
15+
authors = listOf("Cdm2883", "daoge_cmd")
1616
website = "https://github.com/AllayMC/AllayGradle"
1717
}
1818
}

0 commit comments

Comments
 (0)