build.gradle.kts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. @file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE")
  2. import java.time.Duration
  3. import java.util.*
  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. }
  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. runCatching {
  26. val keyProps = Properties().apply {
  27. file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
  28. }
  29. if (keyProps.getProperty("sdk.dir", "").isNotEmpty()) {
  30. project.ext.set("isAndroidSDKAvailable", true)
  31. } else {
  32. project.ext.set("isAndroidSDKAvailable", false)
  33. }
  34. }
  35. allprojects {
  36. group = "net.mamoe"
  37. version = Versions.Mirai.version
  38. repositories {
  39. maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  40. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  41. jcenter()
  42. google()
  43. }
  44. }
  45. subprojects {
  46. afterEvaluate {
  47. apply(plugin = "com.github.johnrengelman.shadow")
  48. val kotlin =
  49. (this as ExtensionAware).extensions.getByName("kotlin") as? org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
  50. ?: return@afterEvaluate
  51. val shadowJvmJar by tasks.creating(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar::class) {
  52. group = "mirai"
  53. val compilations =
  54. kotlin.targets.filter { it.platformType == org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.jvm }
  55. .map { it.compilations["main"] }
  56. compilations.forEach {
  57. dependsOn(it.compileKotlinTask)
  58. }
  59. compilations.forEach {
  60. from(it.output)
  61. }
  62. configurations = compilations.map { it.compileDependencyFiles as Configuration }
  63. this.exclude { file ->
  64. file.name.endsWith(".sf", ignoreCase = true)
  65. .also { if (it) println("excluded ${file.name}") }
  66. }
  67. }
  68. val githubUpload by tasks.creating {
  69. group = "mirai"
  70. dependsOn(shadowJvmJar)
  71. doFirst {
  72. timeout.set(Duration.ofHours(3))
  73. findLatestFile()?.let { (_, file) ->
  74. val filename = file.name
  75. println("Uploading file $filename")
  76. runCatching {
  77. upload.GitHub.upload(
  78. file,
  79. "https://api.github.com/repos/mamoe/mirai-repo/contents/shadow/${project.name}/$filename",
  80. project
  81. )
  82. }.exceptionOrNull()?.let {
  83. System.err.println("GitHub Upload failed")
  84. it.printStackTrace() // force show stacktrace
  85. throw it
  86. }
  87. }
  88. }
  89. }
  90. val cuiCloudUpload by tasks.creating {
  91. group = "mirai"
  92. dependsOn(shadowJvmJar)
  93. doFirst {
  94. timeout.set(Duration.ofHours(3))
  95. findLatestFile()?.let { (_, file) ->
  96. val filename = file.name
  97. println("Uploading file $filename")
  98. runCatching {
  99. upload.CuiCloud.upload(
  100. file,
  101. project
  102. )
  103. }.exceptionOrNull()?.let {
  104. System.err.println("CuiCloud Upload failed")
  105. it.printStackTrace() // force show stacktrace
  106. throw it
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. fun Project.findLatestFile(): Map.Entry<String, File>? {
  114. return File(projectDir, "build/libs").walk()
  115. .filter { it.isFile }
  116. .onEach { println("all files=$it") }
  117. .filter { it.name.matches(Regex("""${project.name}-([0-9]|\.)*\.jar""")) }
  118. .onEach { println("matched file: ${it.name}") }
  119. .associateBy { it.nameWithoutExtension.substringAfterLast('-') }
  120. .onEach { println("versions: $it") }
  121. .maxBy {
  122. it.key.split('.').foldRightIndexed(0) { index: Int, s: String, acc: Int ->
  123. acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0)
  124. }
  125. }
  126. }