2
0

build.gradle.kts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. @file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE")
  2. import java.time.Duration
  3. import kotlin.math.pow
  4. buildscript {
  5. repositories {
  6. mavenLocal()
  7. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  8. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  9. jcenter()
  10. google()
  11. }
  12. dependencies {
  13. classpath("com.github.jengelman.gradle.plugins:shadow:5.2.0")
  14. classpath("com.android.tools.build:gradle:${Versions.Android.androidGradlePlugin}")
  15. classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.Kotlin.stdlib}")
  16. classpath("org.jetbrains.kotlin:kotlin-serialization:${Versions.Kotlin.stdlib}")
  17. classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.Kotlin.atomicFU}")
  18. classpath("org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.Kotlin.binaryValidator}")
  19. }
  20. }
  21. plugins {
  22. id("org.jetbrains.dokka") version Versions.Kotlin.dokka apply false
  23. // id("com.jfrog.bintray") version Versions.Publishing.bintray apply false
  24. }
  25. // https://github.com/kotlin/binary-compatibility-validator
  26. //apply(plugin = "binary-compatibility-validator")
  27. project.ext.set("isAndroidSDKAvailable", false)
  28. // until
  29. // https://youtrack.jetbrains.com/issue/KT-37152,
  30. // are fixed.
  31. /*
  32. runCatching {
  33. val keyProps = Properties().apply {
  34. file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
  35. }
  36. if (keyProps.getProperty("sdk.dir", "").isNotEmpty()) {
  37. project.ext.set("isAndroidSDKAvailable", true)
  38. } else {
  39. project.ext.set("isAndroidSDKAvailable", false)
  40. }
  41. }.exceptionOrNull()?.run {
  42. project.ext.set("isAndroidSDKAvailable", false)
  43. }*/
  44. allprojects {
  45. group = "net.mamoe"
  46. version = Versions.Mirai.version
  47. repositories {
  48. mavenLocal()
  49. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  50. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  51. jcenter()
  52. google()
  53. }
  54. }
  55. subprojects {
  56. afterEvaluate {
  57. apply(plugin = "com.github.johnrengelman.shadow")
  58. val kotlin =
  59. (this as ExtensionAware).extensions.getByName("kotlin") as? org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
  60. ?: return@afterEvaluate
  61. val shadowJvmJar by tasks.creating(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar::class) {
  62. group = "mirai"
  63. val compilations =
  64. kotlin.targets.filter { it.platformType == org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.jvm }
  65. .map { it.compilations["main"] }
  66. compilations.forEach {
  67. dependsOn(it.compileKotlinTask)
  68. }
  69. compilations.forEach {
  70. from(it.output)
  71. }
  72. configurations = compilations.map { it.compileDependencyFiles as Configuration }
  73. this.exclude { file ->
  74. file.name.endsWith(".sf", ignoreCase = true)
  75. .also { if (it) println("excluded ${file.name}") }
  76. }
  77. }
  78. val githubUpload by tasks.creating {
  79. group = "mirai"
  80. dependsOn(shadowJvmJar)
  81. doFirst {
  82. timeout.set(Duration.ofHours(3))
  83. findLatestFile().let { (_, file) ->
  84. val filename = file.name
  85. println("Uploading file $filename")
  86. runCatching {
  87. upload.GitHub.upload(
  88. file,
  89. project,
  90. "mirai-repo",
  91. "shadow/${project.name}/$filename"
  92. )
  93. }.exceptionOrNull()?.let {
  94. System.err.println("GitHub Upload failed")
  95. it.printStackTrace() // force show stacktrace
  96. throw it
  97. }
  98. }
  99. }
  100. }
  101. apply(plugin = "org.jetbrains.dokka")
  102. this.tasks {
  103. val dokka by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  104. outputFormat = "html"
  105. outputDirectory = "$buildDir/dokka"
  106. }
  107. val dokkaMarkdown by creating(org.jetbrains.dokka.gradle.DokkaTask::class) {
  108. outputFormat = "markdown"
  109. outputDirectory = "$buildDir/dokka-markdown"
  110. }
  111. val dokkaGfm by creating(org.jetbrains.dokka.gradle.DokkaTask::class) {
  112. outputFormat = "gfm"
  113. outputDirectory = "$buildDir/dokka-gfm"
  114. }
  115. }
  116. val dokkaGitHubUpload by tasks.creating {
  117. group = "mirai"
  118. dependsOn(tasks.getByName("dokkaGfm"))
  119. doFirst {
  120. val baseDir = file("./build/dokka-gfm/${project.name}")
  121. timeout.set(Duration.ofHours(6))
  122. file("build/dokka-gfm/").walk()
  123. .filter { it.isFile }
  124. .map { old ->
  125. if (old.name == "index.md") File(old.parentFile, "README.md").also { new -> old.renameTo(new) }
  126. else old
  127. }
  128. .forEach { file ->
  129. if (file.endsWith(".md")) {
  130. file.writeText(
  131. file.readText().replace("index.md", "README.md", ignoreCase = true)
  132. .replace(Regex("""```\n([\s\S]*?)```""")) {
  133. "\n" + """
  134. ```kotlin
  135. $it
  136. ```
  137. """.trimIndent()
  138. })
  139. } /* else if (file.name == "README.md") {
  140. file.writeText(file.readText().replace(Regex("""(\n\n\|\s)""")) {
  141. "\n\n" + """"
  142. |||
  143. |:----------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  144. |
  145. """.trimIndent()
  146. })
  147. }*/
  148. val filename = file.toRelativeString(baseDir)
  149. println("Uploading file $filename")
  150. runCatching {
  151. upload.GitHub.upload(
  152. file,
  153. project,
  154. "mirai-doc",
  155. "${project.name}/${project.version}/$filename"
  156. )
  157. }.exceptionOrNull()?.let {
  158. System.err.println("GitHub Upload failed")
  159. it.printStackTrace() // force show stacktrace
  160. throw it
  161. }
  162. }
  163. }
  164. }
  165. val cuiCloudUpload by tasks.creating {
  166. group = "mirai"
  167. dependsOn(shadowJvmJar)
  168. doFirst {
  169. timeout.set(Duration.ofHours(3))
  170. findLatestFile().let { (_, file) ->
  171. val filename = file.name
  172. println("Uploading file $filename")
  173. runCatching {
  174. upload.CuiCloud.upload(
  175. file,
  176. project
  177. )
  178. }.exceptionOrNull()?.let {
  179. System.err.println("CuiCloud Upload failed")
  180. it.printStackTrace() // force show stacktrace
  181. throw it
  182. }
  183. }
  184. }
  185. }
  186. }
  187. afterEvaluate {
  188. tasks.filterIsInstance<org.jetbrains.dokka.gradle.DokkaTask>().forEach { task ->
  189. with(task) {
  190. configuration {
  191. perPackageOption {
  192. prefix = "net.mamoe.mirai"
  193. skipDeprecated = true
  194. }
  195. perPackageOption {
  196. prefix = "net.mamoe.mirai.internal"
  197. suppress = true
  198. }
  199. perPackageOption {
  200. prefix = "net.mamoe.mirai.event.internal"
  201. suppress = true
  202. }
  203. perPackageOption {
  204. prefix = "net.mamoe.mirai.utils.internal"
  205. suppress = true
  206. }
  207. perPackageOption {
  208. prefix = "net.mamoe.mirai.qqandroid.utils"
  209. suppress = true
  210. }
  211. perPackageOption {
  212. prefix = "net.mamoe.mirai.qqandroid.contact"
  213. suppress = true
  214. }
  215. perPackageOption {
  216. prefix = "net.mamoe.mirai.qqandroid.message"
  217. suppress = true
  218. }
  219. perPackageOption {
  220. prefix = "net.mamoe.mirai.qqandroid.network"
  221. suppress = true
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. fun Project.findLatestFile(): Map.Entry<String, File> {
  229. return File(projectDir, "build/libs").walk()
  230. .filter { it.isFile }
  231. .onEach { println("all files=$it") }
  232. .filter { it.name.matches(Regex("""${project.name}-[0-9][0-9]*(\.[0-9]*)*.*\.jar""")) }
  233. .onEach { println("matched file: ${it.name}") }
  234. .associateBy { it.nameWithoutExtension.substringAfterLast('-') }
  235. .onEach { println("versions: $it") }
  236. .maxBy { (version, _) ->
  237. version.split('.').let {
  238. if (it.size == 2) it + "0"
  239. else it
  240. }.reversed().foldIndexed(0) { index: Int, acc: Int, s: String ->
  241. acc + 100.0.pow(index).toInt() * (s.toIntOrNull() ?: 0)
  242. }
  243. } ?: error("cannot find any file to upload")
  244. }