build.gradle.kts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright 2019-2021 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/master/LICENSE
  8. */
  9. @file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
  10. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  11. import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
  12. import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
  13. buildscript {
  14. repositories {
  15. // mavenLocal()
  16. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  17. mavenCentral()
  18. google()
  19. jcenter()
  20. }
  21. dependencies {
  22. classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
  23. classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.atomicFU}")
  24. classpath("org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.binaryValidator}")
  25. }
  26. }
  27. plugins {
  28. kotlin("jvm") // version Versions.kotlinCompiler
  29. kotlin("plugin.serialization") version Versions.kotlinCompiler
  30. // id("org.jetbrains.dokka") version Versions.dokka
  31. id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
  32. id("com.jfrog.bintray") // version Versions.bintray
  33. id("com.gradle.plugin-publish") version "0.12.0" apply false
  34. }
  35. // https://github.com/kotlin/binary-compatibility-validator
  36. apply(plugin = "binary-compatibility-validator")
  37. configure<kotlinx.validation.ApiValidationExtension> {
  38. allprojects.forEach { subproject ->
  39. ignoredProjects.add(subproject.name)
  40. }
  41. ignoredProjects.remove("binary-compatibility-validator")
  42. ignoredProjects.remove("binary-compatibility-validator-android")
  43. // Enable validator for module `binary-compatibility-validator` and `-android` only.
  44. ignoredPackages.add("net.mamoe.mirai.internal")
  45. ignoredPackages.add("net.mamoe.mirai.console.internal")
  46. nonPublicMarkers.add("net.mamoe.mirai.MiraiInternalApi")
  47. nonPublicMarkers.add("net.mamoe.mirai.console.utils.ConsoleInternalApi")
  48. nonPublicMarkers.add("net.mamoe.mirai.console.utils.ConsoleExperimentalApi")
  49. nonPublicMarkers.add("net.mamoe.mirai.MiraiExperimentalApi")
  50. }
  51. GpgSigner.setup(project)
  52. tasks.register("publishMiraiCoreArtifactsToMavenLocal") {
  53. group = "mirai"
  54. dependsOn(
  55. project(":mirai-core-api").tasks.getByName("publishToMavenLocal"),
  56. project(":mirai-core-utils").tasks.getByName("publishToMavenLocal"),
  57. project(":mirai-core").tasks.getByName("publishToMavenLocal")
  58. )
  59. }
  60. allprojects {
  61. group = "net.mamoe"
  62. version = Versions.project
  63. repositories {
  64. // mavenLocal() // cheching issue cause compiler exception
  65. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  66. jcenter()
  67. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  68. maven(url = "https://kotlin.bintray.com/kotlinx")
  69. google()
  70. mavenCentral()
  71. }
  72. afterEvaluate {
  73. configureJvmTarget()
  74. configureMppShadow()
  75. configureEncoding()
  76. configureKotlinTestSettings()
  77. configureKotlinExperimentalUsages()
  78. runCatching {
  79. blockingBridge {
  80. unitCoercion = net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY
  81. }
  82. }
  83. // useIr()
  84. if (isKotlinJvmProject) {
  85. configureFlattenSourceSets()
  86. }
  87. }
  88. }
  89. subprojects {
  90. afterEvaluate {
  91. if (project.name == "mirai-core-api") configureDokka()
  92. if (project.name == "mirai-console") configureDokka()
  93. }
  94. }
  95. tasks.register("cleanExceptIntellij") {
  96. group = "build"
  97. allprojects.forEach { proj ->
  98. if (proj.name != "mirai-console-intellij") {
  99. // Type mismatch
  100. // proj.tasks.findByName("clean")?.let(::dependsOn)
  101. proj.tasks.findByName("clean")?.let { dependsOn(it) }
  102. }
  103. }
  104. }
  105. extensions.findByName("buildScan")?.withGroovyBuilder {
  106. setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  107. setProperty("termsOfServiceAgree", "yes")
  108. }
  109. fun Project.useIr() {
  110. kotlinCompilations?.forEach { kotlinCompilation ->
  111. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  112. }
  113. }
  114. fun Project.configureDokka() {
  115. // apply(plugin = "org.jetbrains.dokka")
  116. // tasks {
  117. // val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  118. // outputDirectory.set(buildDir.resolve("dokka"))
  119. // }
  120. // val dokkaGfm by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  121. // outputDirectory.set(buildDir.resolve("dokka-gfm"))
  122. // }
  123. // }
  124. // tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
  125. // dokkaSourceSets.configureEach {
  126. // perPackageOption {
  127. // matchingRegex.set("net\\.mamoe\\.mirai\\.*")
  128. // skipDeprecated.set(true)
  129. // }
  130. //
  131. // for (suppressedPackage in arrayOf(
  132. // """net.mamoe.mirai.internal""",
  133. // """net.mamoe.mirai.internal.message""",
  134. // """net.mamoe.mirai.internal.network""",
  135. // """net.mamoe.mirai.console.internal""",
  136. // """net.mamoe.mirai.console.compiler.common"""
  137. // )) {
  138. // perPackageOption {
  139. // matchingRegex.set(suppressedPackage.replace(".", "\\."))
  140. // suppress.set(true)
  141. // }
  142. // }
  143. // }
  144. // }
  145. }
  146. fun Project.configureMppShadow() {
  147. val kotlin =
  148. runCatching {
  149. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  150. }.getOrNull() ?: return
  151. if (project.configurations.findByName("jvmRuntimeClasspath") != null) {
  152. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  153. group = "mirai"
  154. archiveClassifier.set("-all")
  155. val compilations =
  156. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  157. .map { it.compilations["main"] }
  158. compilations.forEach {
  159. dependsOn(it.compileKotlinTask)
  160. from(it.output)
  161. }
  162. from(project.configurations.findByName("jvmRuntimeClasspath"))
  163. this.exclude { file ->
  164. file.name.endsWith(".sf", ignoreCase = true)
  165. }
  166. /*
  167. this.manifest {
  168. this.attributes(
  169. "Manifest-Version" to 1,
  170. "Implementation-Vendor" to "Mamoe Technologies",
  171. "Implementation-Title" to this.name.toString(),
  172. "Implementation-Version" to this.version.toString()
  173. )
  174. }*/
  175. }
  176. }
  177. fun Project.configureEncoding() {
  178. tasks.withType(JavaCompile::class.java) {
  179. options.encoding = "UTF8"
  180. }
  181. }
  182. fun Project.configureKotlinTestSettings() {
  183. tasks.withType(Test::class) {
  184. useJUnitPlatform()
  185. }
  186. when {
  187. isKotlinJvmProject -> {
  188. dependencies {
  189. testImplementation(kotlin("test-junit5"))
  190. testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
  191. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  192. }
  193. }
  194. isKotlinMpp -> {
  195. kotlinSourceSets?.forEach { sourceSet ->
  196. if (sourceSet.name.endsWith("test", ignoreCase = true)) {
  197. sourceSet.dependencies {
  198. api(kotlin("test-junit5"))
  199. api("org.junit.jupiter:junit-jupiter-api:5.2.0")
  200. runtimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }