build.gradle.kts 11 KB

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