-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.gradle.kts
299 lines (246 loc) · 8.2 KB
/
settings.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import Settings_gradle.LoadTime.POSTWORLD
import java.io.File.separator
import java.lang.System.setProperty
import java.net.URI
import kotlin.io.OnErrorAction.SKIP
rootProject.name = "Kotlin-Plugin-Template"
projectSettings {
activeModules {
}
proxyPluginProperties {
description = "A simple Template-Plugin using Kotlin as main language"
dependencies {
add("KotlinProvider")
}
}
serverPluginProperties {
authors {
}
mainClass = "TemplatePlugin"
description = "A simple Template-Plugin using Kotlin as main language"
dependencies {
add("CoreControl")
}
}
}
// ####################################################################################################################
// ####################################################################################################################
// ####################################################################################################################
// Following code is to get above working or general settings that should not be touched unless you know what you're doing
setProperty("kotlin.code.style", "official")
setProperty("kotlin.incremental", "true")
dependencyResolutionManagement {
pluginManagement.repositories {
maven {
name = "Bit-Build | Artifactory"
url = URI("https://artifactory.bit-build.de/artifactory/public")
}
}
}
// ####################################################################################################################
// Project Settings DSL
private fun projectSettings(block: ProjectSettings.() -> Unit) {
ProjectSettings().block()
}
private class ProjectSettings {
private val serverPluginProperties = ServerPluginProperties()
private val proxyPluginProperties = ProxyPluginProperties()
/**
* Possible activations:
* ```kotlin
* folia()
* paper()
* velocity()
* utils {}
* ```
*/
fun activeModules(block: ProjectModules.() -> Unit) {
ProjectModules().block()
}
fun serverPluginProperties(block: ServerPluginProperties.() -> Unit) {
serverPluginProperties.block()
}
fun proxyPluginProperties(block: ProxyPluginProperties.() -> Unit) {
proxyPluginProperties.block()
}
}
private class ProjectModules {
fun folia() = setupProject("folia")
fun paper() = setupProject("paper")
fun velocity() = setupProject("velocity")
/**
* To build a jar for publication:
* ```kotlin
* createUtilLibJar = true
* ```
*/
fun util(block: UtilSettings.() -> Unit) {
UtilSettings().block()
setupProject("util")
}
private fun setupProject(projectName: String) {
include(":$projectName")
project(":$projectName").run {
projectDir = File("modules$separator$projectName")
projectDir
}.also { projectDir ->
if (!projectDir.exists()) projectDir.mkdirs()
File("gradle${separator}templates$separator$projectName").copyRecursively(projectDir, onError = { _, _ -> SKIP })
val pluginYml = File("gradle${separator}templates${separator}plugin.yml")
File("${projectDir.path}${separator}src${separator}main${separator}kotlin").mkdirs()
File("${projectDir.path}${separator}src${separator}main${separator}resources").also {
it.mkdirs()
if (projectName !in listOf("util", "velocity"))
pluginYml.copyRecursively(it.resolve("plugin.yml"), onError = { _, _ -> SKIP })
}
File("${projectDir.path}${separator}src${separator}test${separator}kotlin").mkdirs()
File("${projectDir.path}${separator}src${separator}test${separator}resources").mkdirs()
}
}
fun publishTo(name: String, block: PublishRepository.() -> Unit) {
PublishRepository(name).block()
}
}
private class UtilSettings {
var createUtilLibJar = false
set(value) {
setProperty("project.create-util-lib-jar", "$value")
field = value
}
}
private class PublishRepository(
name: String
) {
init {
setProperty("project.publish.${name.lowercase()}.auth-type", "${PublishRepositoryAuthType.USER_ACCESS_TOKEN}")
}
val name = name.takeIf { !it.contains(".") }?.lowercase() ?: throw Exception("Name may not contain: .")
var url: String = ""
set(value) {
setProperty("project.publish.$name.url", value)
field = value
}
var authType: PublishRepositoryAuthType = PublishRepositoryAuthType.USER_ACCESS_TOKEN
set(value) {
setProperty("project.publish.$name.auth-type", "$value")
field = value
}
}
private enum class PublishRepositoryAuthType {
USER_ACCESS_TOKEN;
override fun toString(): String = name.uppercase()
}
private class ServerPluginProperties {
init {
setProperty("plugin.authors", "[]")
setProperty("plugin.dependencies", "[]")
setProperty("plugin.soft-dependencies", "[]")
setProperty("plugin.load", "$POSTWORLD")
setProperty("plugin.load-before", "[]")
setProperty("plugin.commands", "{}")
}
var mainClass: String = ""
set(value) {
setProperty("plugin.main-class", value)
field = value
}
var description: String = ""
set(value) {
setProperty("plugin.description", value)
field = value
}
var load: LoadTime = POSTWORLD
set(value) {
setProperty("plugin.load", "$value")
field = value
}
fun authors(block: PluginListProperty.() -> Unit) {
setProperty("plugin.authors", "${PluginListProperty(block)}")
}
fun dependencies(block: PluginListProperty.() -> Unit) {
setProperty("plugin.dependencies", "${PluginListProperty(block)}")
}
fun softDependencies(block: PluginListProperty.() -> Unit) {
setProperty("plugin.soft-dependencies", "${PluginListProperty(block)}")
}
fun loadBefore(block: PluginListProperty.() -> Unit) {
setProperty("plugin.load-before", "${PluginListProperty(block)}")
}
fun commands(block: Commands.() -> Unit) {
setProperty("plugin.commands", "${Commands().apply(block)}")
}
}
private enum class LoadTime {
STARTUP,
POSTWORLD;
override fun toString(): String = name.uppercase()
}
private class Commands {
private val commands: MutableMap<String, Command> = mutableMapOf()
fun add(name: String, block: Command.() -> Unit) {
commands[name] = Command().apply(block)
}
override fun toString(): String = commands.takeIf { it.isNotEmpty() }?.run {
"\n${map { (name, cmd) -> " $name:\n$cmd" }.joinToString("\n")}"
} ?: "{}"
}
private class Command {
var description: String? = null
private val aliases = PluginListProperty()
var permission: String? = null
var permissionMessage: String? = null
var usage: String? = null
fun aliases(block: PluginListProperty.() -> Unit) {
aliases.block()
}
override fun toString(): String = mutableMapOf(
"description" to description,
"aliases" to aliases.takeIf(PluginListProperty::isNotEmpty)?.toString(),
"permission" to permission,
"permission-message" to permissionMessage,
"usage" to usage
).filterValues { it != null }
.map { (key, value) -> " $key: $value" }
.joinToString("\n")
}
private class ProxyPluginProperties {
init {
setProperty("proxy-plugin.dependencies", "[]")
setProperty("proxy-plugin.soft-dependencies", "[]")
}
var description: String = ""
set(value) {
setProperty("proxy-plugin.description", value)
field = value
}
fun dependencies(block: ProxyDependenciesProperty.() -> Unit) {
setProperty("proxy-plugin.dependencies", "${ProxyDependenciesProperty(block)}")
}
}
private class PluginListProperty(block: PluginListProperty.() -> Unit = {}) : LinkedHashSet<String>() {
init {
block()
}
override fun toString(): String = yamlListOf()
private fun yamlListOf() =
takeIf { it.isNotEmpty() } // IF
?.toList()?.toString() // DO
?: "[]" // ELSE
}
private class ProxyDependenciesProperty(block: ProxyDependenciesProperty.() -> Unit) :
LinkedHashMap<String, Boolean>() {
init {
block()
}
fun add(dependency: String, optional: Boolean = false) {
put(dependency, optional)
}
override fun toString(): String = "[\n" +
map { (key, value) -> " Dependency(id = \"${key.lowercase()}\", optional = $value)" }.joinToString(",\n") +
"\n ]"
}
private fun <T : Any> T.applyIf(condition: Boolean, block: T.() -> Unit): T = if (condition) {
apply { block() }
} else {
this
}