build.gradle.kts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.dokka.base.DokkaBase
  12. import org.jetbrains.dokka.base.DokkaBaseConfiguration
  13. import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
  14. import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
  15. import java.time.LocalDateTime
  16. buildscript {
  17. repositories {
  18. if (System.getProperty("use.maven.local") == "true") {
  19. mavenLocal()
  20. }
  21. mavenCentral()
  22. gradlePluginPortal()
  23. google()
  24. }
  25. dependencies {
  26. classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
  27. classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.atomicFU}")
  28. classpath("org.jetbrains.dokka:dokka-base:${Versions.dokka}")
  29. }
  30. }
  31. plugins {
  32. kotlin("jvm") // version Versions.kotlinCompiler
  33. kotlin("plugin.serialization") version Versions.kotlinCompiler
  34. id("org.jetbrains.dokka") version Versions.dokka
  35. id("me.him188.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
  36. id("me.him188.kotlin-dynamic-delegation") version Versions.dynamicDelegation
  37. id("com.gradle.plugin-publish") version "0.12.0" apply false
  38. id("org.jetbrains.kotlinx.binary-compatibility-validator") version Versions.binaryValidator apply false
  39. }
  40. GpgSigner.setup(project)
  41. analyzes.CompiledCodeVerify.run { registerAllVerifyTasks() }
  42. allprojects {
  43. group = "net.mamoe"
  44. version = Versions.project
  45. repositories {
  46. if (System.getProperty("use.maven.local") == "true") {
  47. mavenLocal()
  48. }
  49. mavenCentral()
  50. gradlePluginPortal()
  51. google()
  52. }
  53. preConfigureJvmTarget()
  54. afterEvaluate {
  55. configureJvmTarget()
  56. configureMppShadow()
  57. configureEncoding()
  58. configureKotlinTestSettings()
  59. configureKotlinExperimentalUsages()
  60. runCatching {
  61. blockingBridge {
  62. unitCoercion = me.him188.kotlin.jvm.blocking.bridge.compiler.UnitCoercion.COMPATIBILITY
  63. }
  64. }
  65. // useIr()
  66. if (isKotlinJvmProject) {
  67. configureFlattenSourceSets()
  68. }
  69. configureJarManifest()
  70. substituteDependenciesUsingExpectedVersion()
  71. if (System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING") != null) {
  72. project.tasks.filterIsInstance<ShadowJar>().forEach { shadow ->
  73. shadow.enabled = false // they are too big
  74. }
  75. logger.info("Disabled all shadow tasks.")
  76. }
  77. }
  78. }
  79. subprojects {
  80. afterEvaluate {
  81. if (project.path == ":mirai-core-api") configureDokka()
  82. if (project.path == ":mirai-console") configureDokka()
  83. }
  84. }
  85. rootProject.configureDokka()
  86. tasks.register("cleanExceptIntellij") {
  87. group = "build"
  88. allprojects.forEach { proj ->
  89. if (proj.name != "mirai-console-intellij") {
  90. // Type mismatch
  91. // proj.tasks.findByName("clean")?.let(::dependsOn)
  92. proj.tasks.findByName("clean")?.let { dependsOn(it) }
  93. }
  94. }
  95. }
  96. extensions.findByName("buildScan")?.withGroovyBuilder {
  97. setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  98. setProperty("termsOfServiceAgree", "yes")
  99. }
  100. fun Project.useIr() {
  101. kotlinCompilations?.forEach { kotlinCompilation ->
  102. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  103. }
  104. }
  105. fun Project.configureDokka() {
  106. val isRoot = this@configureDokka == rootProject
  107. if (!isRoot) {
  108. apply(plugin = "org.jetbrains.dokka")
  109. }
  110. tasks.withType<org.jetbrains.dokka.gradle.AbstractDokkaTask>().configureEach {
  111. pluginConfiguration<DokkaBase, DokkaBaseConfiguration> {
  112. this.footerMessage = """Copyright 2019-${
  113. LocalDateTime.now().year
  114. } <a href="https://github.com/mamoe">Mamoe Technologies</a> and contributors.
  115. Source code:
  116. <a href="https://github.com/mamoe/mirai">GitHub</a>
  117. """.trimIndent()
  118. this.customAssets = listOf(
  119. rootProject.projectDir.resolve("mirai-dokka/frontend/ext.js"),
  120. )
  121. }
  122. }
  123. tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
  124. dokkaSourceSets.configureEach {
  125. perPackageOption {
  126. matchingRegex.set("net\\.mamoe\\.mirai\\.*")
  127. skipDeprecated.set(true)
  128. }
  129. for (suppressedPackage in arrayOf(
  130. """net.mamoe.mirai.internal""",
  131. """net.mamoe.mirai.internal.message""",
  132. """net.mamoe.mirai.internal.network""",
  133. """net.mamoe.mirai.console.internal""",
  134. """net.mamoe.mirai.console.compiler.common"""
  135. )) {
  136. perPackageOption {
  137. matchingRegex.set(suppressedPackage.replace(".", "\\."))
  138. suppress.set(true)
  139. }
  140. }
  141. }
  142. }
  143. if (isRoot) {
  144. tasks.named<org.jetbrains.dokka.gradle.AbstractDokkaTask>("dokkaHtmlMultiModule").configure {
  145. outputDirectory.set(
  146. rootProject.projectDir.resolve("mirai-dokka/pages/snapshot")
  147. )
  148. }
  149. }
  150. }
  151. fun Project.configureMppShadow() {
  152. val kotlin =
  153. runCatching {
  154. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  155. }.getOrNull() ?: return
  156. if (project.configurations.findByName("jvmRuntimeClasspath") != null) {
  157. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  158. group = "mirai"
  159. archiveClassifier.set("-all")
  160. val compilations =
  161. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  162. .map { it.compilations["main"] }
  163. compilations.forEach {
  164. dependsOn(it.compileKotlinTask)
  165. from(it.output)
  166. }
  167. from(project.configurations.findByName("jvmRuntimeClasspath"))
  168. this.exclude { file ->
  169. file.name.endsWith(".sf", ignoreCase = true)
  170. }
  171. /*
  172. this.manifest {
  173. this.attributes(
  174. "Manifest-Version" to 1,
  175. "Implementation-Vendor" to "Mamoe Technologies",
  176. "Implementation-Title" to this.name.toString(),
  177. "Implementation-Version" to this.version.toString()
  178. )
  179. }*/
  180. }
  181. }
  182. }