forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiraiConsoleExtension.kt
306 lines (262 loc) · 9.09 KB
/
MiraiConsoleExtension.kt
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
300
301
302
303
304
305
306
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
package net.mamoe.mirai.console.gradle
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.JavaVersion
import org.gradle.api.XmlProvider
import org.gradle.api.plugins.PluginContainer
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.JavaExec
/**
* ```
* mirai {
* // 配置
* }
* ```
*/
// must be open
public open class MiraiConsoleExtension {
/**
* 为 `true` 时不自动添加 mirai-core-api 的依赖
*
* 默认: `false`
*/
public var noCoreApi: Boolean = false
/**
* 为 `true` 时不自动为 test 模块添加 mirai-core 的依赖.
*
* 默认: `false`
*/
public var noTestCore: Boolean = false
/**
* 为 `true` 时不自动添加 mirai-console 的依赖.
*
* 默认: `false`
*/
public var noConsole: Boolean = false
/**
* 自动添加的 mirai-core 和 mirai-core-api 的版本.
*
* 默认: 与本 Gradle 插件编译时的 mirai-core 版本相同. [VersionConstants.CORE_VERSION]
*/
public var coreVersion: String = VersionConstants.CORE_VERSION
/**
* 自动添加的 mirai-console 后端和前端的版本.
*
* 默认: 与本 Gradle 插件版本相同. [VersionConstants.CONSOLE_VERSION]
*/
public var consoleVersion: String = VersionConstants.CONSOLE_VERSION
/**
* 自动为 test 模块添加的前端依赖名称
*
* 为 `null` 时不自动为 test 模块添加前端依赖.
*
* 默认: [MiraiConsoleFrontEndKind.TERMINAL]
*/
public var useTestConsoleFrontEnd: MiraiConsoleFrontEndKind? = MiraiConsoleFrontEndKind.TERMINAL
/**
* Java 和 Kotlin 编译目标. 至少为 [JavaVersion.VERSION_1_8].
*
* 一般人不需要修改此项.
*
* 默认: [JavaVersion.VERSION_1_8]
*/
public var jvmTarget: JavaVersion = JavaVersion.VERSION_1_8
/**
* 默认会配置 Kotlin 编译器参数 "-Xjvm-default=all". 将此项设置为 `false` 可避免配置.
*
* 一般人不需要修改此项.
*
* 默认: `false`
*/
public var dontConfigureKotlinJvmDefault: Boolean = false
/**
* 配置 gradle task runConsole. 将此项设置为 `false` 时不会配置测试环境
*/
public var consoleTestRuntime: Boolean = true
internal val shadowConfigurations: MutableList<ShadowJar.() -> Unit> = mutableListOf()
internal val excludedDependencies: MutableSet<ExcludedDependency> = mutableSetOf()
internal val consoleTestRuntimeConf: MutableList<JavaExec.() -> Unit> = mutableListOf()
public fun setupConsoleTestRuntime(configure: JavaExec.() -> Unit) {
consoleTestRuntimeConf.add(configure)
}
internal data class ExcludedDependency(
val group: String,
val name: String
)
/**
* 配置 [ShadowJar] (即打包插件)
*/
public fun configureShadow(configure: ShadowJar.() -> Unit) {
shadowConfigurations.add(configure)
}
/**
* 在插件打包时忽略一个依赖
*
* @param notation 格式为 "groupId:name". 如 "org.jetbrains.kotlin:kotlin-stdlib"
*/
public fun excludeDependency(notation: String) {
requireNotNull(notation.count { it == ':' } == 1) { "Invalid dependency notation $notation." }
excludedDependencies.add(ExcludedDependency(notation.substringBefore(':'), notation.substringAfter(':')))
}
/**
* 在插件打包时忽略一个依赖
*
* @param group 如 "org.jetbrains.kotlin"
* @param name 如 "kotlin-stdlib"
*/
public fun excludeDependency(group: String, name: String) {
excludedDependencies.add(ExcludedDependency(group, name))
}
/**
* Bintray 插件成品 JAR 发布 配置.
*
* @see PluginPublishing
* @since 1.1
*/
public val publishing: PluginPublishing = PluginPublishing()
/**
* 控制自动配置 Bintray 发布. 默认为 `false`, 表示不自动配置发布.
*
* 开启后将会:
* - 创建名为 "mavenJava" 的 [MavenPublication]
* - [应用][PluginContainer.apply] [BintrayPlugin], 配置 Bintray 相关参数
* - 创建 task "publishPlugin"
*
* @since 1.1
*/
public var publishingEnabled: Boolean = false
/**
* 开启自动配置 Bintray 插件成品 JAR 发布, 并以 [configure] 配置 [PluginPublishing].
*
* @see [PluginPublishing]
* @see publishingEnabled
* @since 1.1
*/
public inline fun publishing(crossinline configure: PluginPublishing.() -> Unit) {
publishingEnabled = true
publishing.run(configure)
}
/**
* 开启自动配置 Bintray 插件成品 JAR 发布.
*
* @see [PluginPublishing]
* @see publishingEnabled
* @since 1.1
*/
public fun publishing() {
publishingEnabled = true
}
/**
* Bintray 插件成品 JAR 发布 配置.
*
* 对于一个属性 PROP, 会按以下顺序依次尝试读取:
* 1. Gradle 参数
* - "gradle.properties"
* - ext
* - Gradle -P 启动参数
* 2. [System.getProperty] "PROP"
* 3. 当前和所有父 project 根目录下 "PROP" 文件的内容
* 4. [System.getenv] "PROP"
*
* @see publishing
* @see publishingEnabled
* @since 1.1
*/
public class PluginPublishing internal constructor() {
///////////////////////////////////////////////////////////////////////////
// Required arguments
///////////////////////////////////////////////////////////////////////////
/**
* Bintray 账户名. 必须.
* 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.user"
*
* @see [PluginPublishing]
*/
public var user: String? = null
/**
* Bintray 账户 key. 必须.
* 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.key"
*/
public var key: String? = null
/**
* 目标仓库名称. 必须.
* 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.repo"
*/
public var repo: String? = null
/**
* 目标仓库名称. 必须.
* 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.package"
*/
public var packageName: String? = null
///////////////////////////////////////////////////////////////////////////
// Optional arguments
///////////////////////////////////////////////////////////////////////////
// Artifact
/**
* 发布的 artifact id. 默认为 `project.name`.
*
* artifact id 是类似于 "net.mamoe:mirai-console:1.1.0" 中的 "mirai-console"
*/
public var artifactId: String? = null
/**
* 发布的 group id. 默认为 `project.group`.
*
* group id 是类似于 "net.mamoe:mirai-console:1.1.0" 中的 "net.mamoe"
*/
public var groupId: String? = null
/**
* 发布的版本号, 默认为 `project.version`
*
* 版本号是类似于 "net.mamoe:mirai-console:1.1.0" 中的 "1.1.0"
*/
public var version: String? = null
/**
* 发布的描述, 默认为 `project.description`
*/
public var description: String? = null
// Bintray
/**
* Bintray organization 名. 可选.
* 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.org".
* 仍然无法获取时发布到 [user] 账号下的仓库 [repo], 否则发布到指定 [org] 下的仓库 [repo].
*/
public var org: String? = null
/**
* 上传后自动发布. 默认 `true`.
*/
public var publish: Boolean = true
/**
* 当文件冲突时覆盖. 默认 `false`.
*/
public var override: Boolean = false
// Custom configurations
internal val mavenPomConfigs = mutableListOf<XmlProvider.() -> Unit>()
internal val mavenPublicationConfigs = mutableListOf<MavenPublication.() -> Unit>()
/**
* 自定义配置 maven pom.xml [XmlProvider]
*/
public fun mavenPom(configure: XmlProvider.() -> Unit) {
mavenPomConfigs.add(configure)
}
/**
* 自定义配置 [MavenPublication]
*/
public fun mavenPublication(configure: MavenPublication.() -> Unit) {
mavenPublicationConfigs.add(configure)
}
}
}
/**
* @see MiraiConsoleExtension.useTestConsoleFrontEnd
*/
public enum class MiraiConsoleFrontEndKind {
TERMINAL,
}