Skip to content

Commit 929bcc3

Browse files
committed
feat: Add DSL for custom runtime dependency repositories
1 parent 20a1a91 commit 929bcc3

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

src/main/kotlin/cc/modlabs/kpapergradle/KPaperGradlePlugin.kt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ open class KPaperExtension(objects: ObjectFactory) {
1919
val javaVersion: Property<Int> = objects.property(Int::class.java).convention(21)
2020
// Base package used by RegisterManager to scan for commands/listeners
2121
val registrationBasePackage: Property<String> = objects.property(String::class.java).convention("cc.modlabs")
22+
23+
// Custom repositories for the runtime dependency loader (MavenLibraryResolver)
24+
internal val customRepositories = mutableListOf<Pair<String, String>>() // id to url
25+
2226
fun deliver(vararg deps: String) { deliverDependencies += deps }
27+
28+
// DSL: repository("https://repo1.maven.org/maven2/")
29+
fun repository(url: String) {
30+
val host = try { java.net.URI(url).host ?: url } catch (_: Exception) { url }
31+
val id = host.replace(Regex("[^a-zA-Z0-9-_]"), "-")
32+
customRepositories += id to url
33+
}
34+
35+
// DSL: repository("myRepo", "https://repo.example.com/maven/")
36+
fun repository(id: String, url: String) {
37+
customRepositories += id to url
38+
}
2339
}
2440

2541
class KPaperGradlePlugin : Plugin<Project> {
@@ -66,21 +82,36 @@ class KPaperGradlePlugin : Plugin<Project> {
6682
}
6783
}
6884
delivered += ext.deliverDependencies
69-
val depFile = File(project.layout.buildDirectory.asFile.get(), "generated-resources/.dependencies")
70-
depFile.parentFile.mkdirs()
85+
val buildDir = project.layout.buildDirectory.asFile.get()
86+
val genResDir = File(buildDir, "generated-resources")
87+
genResDir.mkdirs()
88+
val depFile = File(genResDir, ".dependencies")
7189
depFile.writeText(delivered.joinToString("\n"))
90+
91+
// Write repositories file for the runtime dependency loader
92+
val reposFile = File(genResDir, ".repositories")
93+
if (ext.customRepositories.isNotEmpty()) {
94+
val lines = ext.customRepositories.map { (id, url) -> "$id $url" }
95+
reposFile.writeText(lines.joinToString("\n"))
96+
} else {
97+
if (reposFile.exists()) reposFile.delete()
98+
}
7299
}
73100

74101
project.tasks.matching { it.name == "processResources" }.all { task ->
75102
task.dependsOn(generateDeps)
76103
task.doLast {
77104
val buildDir = project.layout.buildDirectory.asFile.get()
78105
val depFile = File(buildDir, "generated-resources/.dependencies")
106+
val reposFile = File(buildDir, "generated-resources/.repositories")
79107
val resourcesDir = File(buildDir, "resources/main")
108+
resourcesDir.mkdirs()
80109
if (depFile.exists()) {
81-
resourcesDir.mkdirs()
82110
depFile.copyTo(File(resourcesDir, ".dependencies"), overwrite = true)
83111
}
112+
if (reposFile.exists()) {
113+
reposFile.copyTo(File(resourcesDir, ".repositories"), overwrite = true)
114+
}
84115

85116
// Patch paper-plugin.yml to add loader/bootstrapper if they are not set
86117
val paperPlugin = File(resourcesDir, "paper-plugin.yml")

0 commit comments

Comments
 (0)