build.gradle.kts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  49. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  50. jcenter()
  51. google()
  52. }
  53. }
  54. subprojects {
  55. afterEvaluate {
  56. apply(plugin = "com.github.johnrengelman.shadow")
  57. val kotlin =
  58. (this as ExtensionAware).extensions.getByName("kotlin") as? org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
  59. ?: return@afterEvaluate
  60. val shadowJvmJar by tasks.creating(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar::class) {
  61. group = "mirai"
  62. val compilations =
  63. kotlin.targets.filter { it.platformType == org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.jvm }
  64. .map { it.compilations["main"] }
  65. compilations.forEach {
  66. dependsOn(it.compileKotlinTask)
  67. }
  68. compilations.forEach {
  69. from(it.output)
  70. }
  71. configurations = compilations.map { it.compileDependencyFiles as Configuration }
  72. this.exclude { file ->
  73. file.name.endsWith(".sf", ignoreCase = true)
  74. .also { if (it) println("excluded ${file.name}") }
  75. }
  76. }
  77. val githubUpload by tasks.creating {
  78. group = "mirai"
  79. dependsOn(shadowJvmJar)
  80. doFirst {
  81. timeout.set(Duration.ofHours(3))
  82. findLatestFile()?.let { (_, file) ->
  83. val filename = file.name
  84. println("Uploading file $filename")
  85. runCatching {
  86. upload.GitHub.upload(
  87. file,
  88. "https://api.github.com/repos/mamoe/mirai-repo/contents/shadow/${project.name}/$filename",
  89. project
  90. )
  91. }.exceptionOrNull()?.let {
  92. System.err.println("GitHub Upload failed")
  93. it.printStackTrace() // force show stacktrace
  94. throw it
  95. }
  96. }
  97. }
  98. }
  99. val cuiCloudUpload by tasks.creating {
  100. group = "mirai"
  101. dependsOn(shadowJvmJar)
  102. doFirst {
  103. timeout.set(Duration.ofHours(3))
  104. findLatestFile()?.let { (_, file) ->
  105. val filename = file.name
  106. println("Uploading file $filename")
  107. runCatching {
  108. upload.CuiCloud.upload(
  109. file,
  110. project
  111. )
  112. }.exceptionOrNull()?.let {
  113. System.err.println("CuiCloud Upload failed")
  114. it.printStackTrace() // force show stacktrace
  115. throw it
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. fun Project.findLatestFile(): Map.Entry<String, File> {
  123. return File(projectDir, "build/libs").walk()
  124. .filter { it.isFile }
  125. .onEach { println("all files=$it") }
  126. .filter { it.name.matches(Regex("""${project.name}-[0-9][0-9]*(\.[0-9]*)*.*\.jar""")) }
  127. .onEach { println("matched file: ${it.name}") }
  128. .associateBy { it.nameWithoutExtension.substringAfterLast('-') }
  129. .onEach { println("versions: $it") }
  130. .maxBy { (version, file) ->
  131. version.split('.').let {
  132. if (it.size == 2) it + "0"
  133. else it
  134. }.reversed().foldIndexed(0) { index: Int, acc: Int, s: String ->
  135. acc + 100.0.pow(index).toInt() * (s.toIntOrNull() ?: 0)
  136. }
  137. } ?: error("cannot find any file to upload")
  138. }