MiraiConsoleExtension.kt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. @file:Suppress("unused", "MemberVisibilityCanBePrivate")
  10. package net.mamoe.mirai.console.gradle
  11. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  12. import org.gradle.api.JavaVersion
  13. import org.gradle.api.XmlProvider
  14. import org.gradle.api.plugins.PluginContainer
  15. import org.gradle.api.publish.maven.MavenPublication
  16. import org.gradle.api.tasks.JavaExec
  17. /**
  18. * ```
  19. * mirai {
  20. * // 配置
  21. * }
  22. * ```
  23. */
  24. // must be open
  25. public open class MiraiConsoleExtension {
  26. /**
  27. * 为 `true` 时不自动添加 mirai-core-api 的依赖
  28. *
  29. * 默认: `false`
  30. */
  31. public var noCoreApi: Boolean = false
  32. /**
  33. * 为 `true` 时不自动为 test 模块添加 mirai-core 的依赖.
  34. *
  35. * 默认: `false`
  36. */
  37. public var noTestCore: Boolean = false
  38. /**
  39. * 为 `true` 时不自动添加 mirai-console 的依赖.
  40. *
  41. * 默认: `false`
  42. */
  43. public var noConsole: Boolean = false
  44. /**
  45. * 自动添加的 mirai-core 和 mirai-core-api 的版本.
  46. *
  47. * 默认: 与本 Gradle 插件编译时的 mirai-core 版本相同. [VersionConstants.CORE_VERSION]
  48. */
  49. public var coreVersion: String = VersionConstants.CORE_VERSION
  50. /**
  51. * 自动添加的 mirai-console 后端和前端的版本.
  52. *
  53. * 默认: 与本 Gradle 插件版本相同. [VersionConstants.CONSOLE_VERSION]
  54. */
  55. public var consoleVersion: String = VersionConstants.CONSOLE_VERSION
  56. /**
  57. * 自动为 test 模块添加的前端依赖名称
  58. *
  59. * 为 `null` 时不自动为 test 模块添加前端依赖.
  60. *
  61. * 默认: [MiraiConsoleFrontEndKind.TERMINAL]
  62. */
  63. public var useTestConsoleFrontEnd: MiraiConsoleFrontEndKind? = MiraiConsoleFrontEndKind.TERMINAL
  64. /**
  65. * Java 和 Kotlin 编译目标. 至少为 [JavaVersion.VERSION_1_8].
  66. *
  67. * 一般人不需要修改此项.
  68. *
  69. * 默认: [JavaVersion.VERSION_1_8]
  70. */
  71. public var jvmTarget: JavaVersion = JavaVersion.VERSION_1_8
  72. /**
  73. * 默认会配置 Kotlin 编译器参数 "-Xjvm-default=all". 将此项设置为 `false` 可避免配置.
  74. *
  75. * 一般人不需要修改此项.
  76. *
  77. * 默认: `false`
  78. */
  79. public var dontConfigureKotlinJvmDefault: Boolean = false
  80. /**
  81. * 配置 gradle task runConsole. 将此项设置为 `false` 时不会配置测试环境
  82. */
  83. public var consoleTestRuntime: Boolean = true
  84. internal val shadowConfigurations: MutableList<ShadowJar.() -> Unit> = mutableListOf()
  85. internal val excludedDependencies: MutableSet<ExcludedDependency> = mutableSetOf()
  86. internal val consoleTestRuntimeConf: MutableList<JavaExec.() -> Unit> = mutableListOf()
  87. public fun setupConsoleTestRuntime(configure: JavaExec.() -> Unit) {
  88. consoleTestRuntimeConf.add(configure)
  89. }
  90. internal data class ExcludedDependency(
  91. val group: String,
  92. val name: String
  93. )
  94. /**
  95. * 配置 [ShadowJar] (即打包插件)
  96. */
  97. public fun configureShadow(configure: ShadowJar.() -> Unit) {
  98. shadowConfigurations.add(configure)
  99. }
  100. /**
  101. * 在插件打包时忽略一个依赖
  102. *
  103. * @param notation 格式为 "groupId:name". 如 "org.jetbrains.kotlin:kotlin-stdlib"
  104. */
  105. public fun excludeDependency(notation: String) {
  106. requireNotNull(notation.count { it == ':' } == 1) { "Invalid dependency notation $notation." }
  107. excludedDependencies.add(ExcludedDependency(notation.substringBefore(':'), notation.substringAfter(':')))
  108. }
  109. /**
  110. * 在插件打包时忽略一个依赖
  111. *
  112. * @param group 如 "org.jetbrains.kotlin"
  113. * @param name 如 "kotlin-stdlib"
  114. */
  115. public fun excludeDependency(group: String, name: String) {
  116. excludedDependencies.add(ExcludedDependency(group, name))
  117. }
  118. /**
  119. * Bintray 插件成品 JAR 发布 配置.
  120. *
  121. * @see PluginPublishing
  122. * @since 1.1
  123. */
  124. public val publishing: PluginPublishing = PluginPublishing()
  125. /**
  126. * 控制自动配置 Bintray 发布. 默认为 `false`, 表示不自动配置发布.
  127. *
  128. * 开启后将会:
  129. * - 创建名为 "mavenJava" 的 [MavenPublication]
  130. * - [应用][PluginContainer.apply] [BintrayPlugin], 配置 Bintray 相关参数
  131. * - 创建 task "publishPlugin"
  132. *
  133. * @since 1.1
  134. */
  135. public var publishingEnabled: Boolean = false
  136. /**
  137. * 开启自动配置 Bintray 插件成品 JAR 发布, 并以 [configure] 配置 [PluginPublishing].
  138. *
  139. * @see [PluginPublishing]
  140. * @see publishingEnabled
  141. * @since 1.1
  142. */
  143. public inline fun publishing(crossinline configure: PluginPublishing.() -> Unit) {
  144. publishingEnabled = true
  145. publishing.run(configure)
  146. }
  147. /**
  148. * 开启自动配置 Bintray 插件成品 JAR 发布.
  149. *
  150. * @see [PluginPublishing]
  151. * @see publishingEnabled
  152. * @since 1.1
  153. */
  154. public fun publishing() {
  155. publishingEnabled = true
  156. }
  157. /**
  158. * Bintray 插件成品 JAR 发布 配置.
  159. *
  160. * 对于一个属性 PROP, 会按以下顺序依次尝试读取:
  161. * 1. Gradle 参数
  162. * - "gradle.properties"
  163. * - ext
  164. * - Gradle -P 启动参数
  165. * 2. [System.getProperty] "PROP"
  166. * 3. 当前和所有父 project 根目录下 "PROP" 文件的内容
  167. * 4. [System.getenv] "PROP"
  168. *
  169. * @see publishing
  170. * @see publishingEnabled
  171. * @since 1.1
  172. */
  173. public class PluginPublishing internal constructor() {
  174. ///////////////////////////////////////////////////////////////////////////
  175. // Required arguments
  176. ///////////////////////////////////////////////////////////////////////////
  177. /**
  178. * Bintray 账户名. 必须.
  179. * 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.user"
  180. *
  181. * @see [PluginPublishing]
  182. */
  183. public var user: String? = null
  184. /**
  185. * Bintray 账户 key. 必须.
  186. * 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.key"
  187. */
  188. public var key: String? = null
  189. /**
  190. * 目标仓库名称. 必须.
  191. * 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.repo"
  192. */
  193. public var repo: String? = null
  194. /**
  195. * 目标仓库名称. 必须.
  196. * 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.package"
  197. */
  198. public var packageName: String? = null
  199. ///////////////////////////////////////////////////////////////////////////
  200. // Optional arguments
  201. ///////////////////////////////////////////////////////////////////////////
  202. // Artifact
  203. /**
  204. * 发布的 artifact id. 默认为 `project.name`.
  205. *
  206. * artifact id 是类似于 "net.mamoe:mirai-console:1.1.0" 中的 "mirai-console"
  207. */
  208. public var artifactId: String? = null
  209. /**
  210. * 发布的 group id. 默认为 `project.group`.
  211. *
  212. * group id 是类似于 "net.mamoe:mirai-console:1.1.0" 中的 "net.mamoe"
  213. */
  214. public var groupId: String? = null
  215. /**
  216. * 发布的版本号, 默认为 `project.version`
  217. *
  218. * 版本号是类似于 "net.mamoe:mirai-console:1.1.0" 中的 "1.1.0"
  219. */
  220. public var version: String? = null
  221. /**
  222. * 发布的描述, 默认为 `project.description`
  223. */
  224. public var description: String? = null
  225. // Bintray
  226. /**
  227. * Bintray organization 名. 可选.
  228. * 若为 `null`, 将会以 [PluginPublishing] 中描述的步骤获取 "bintray.org".
  229. * 仍然无法获取时发布到 [user] 账号下的仓库 [repo], 否则发布到指定 [org] 下的仓库 [repo].
  230. */
  231. public var org: String? = null
  232. /**
  233. * 上传后自动发布. 默认 `true`.
  234. */
  235. public var publish: Boolean = true
  236. /**
  237. * 当文件冲突时覆盖. 默认 `false`.
  238. */
  239. public var override: Boolean = false
  240. // Custom configurations
  241. internal val mavenPomConfigs = mutableListOf<XmlProvider.() -> Unit>()
  242. internal val mavenPublicationConfigs = mutableListOf<MavenPublication.() -> Unit>()
  243. /**
  244. * 自定义配置 maven pom.xml [XmlProvider]
  245. */
  246. public fun mavenPom(configure: XmlProvider.() -> Unit) {
  247. mavenPomConfigs.add(configure)
  248. }
  249. /**
  250. * 自定义配置 [MavenPublication]
  251. */
  252. public fun mavenPublication(configure: MavenPublication.() -> Unit) {
  253. mavenPublicationConfigs.add(configure)
  254. }
  255. }
  256. }
  257. /**
  258. * @see MiraiConsoleExtension.useTestConsoleFrontEnd
  259. */
  260. public enum class MiraiConsoleFrontEndKind {
  261. TERMINAL,
  262. }