2
0

build.gradle.kts 5.1 KB

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