DeployTask.kt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import org.gradle.api.DefaultTask
  2. import org.gradle.api.tasks.Input
  3. import org.gradle.api.tasks.TaskAction
  4. @Suppress("DEPRECATION")
  5. open class DeployTask : DefaultTask() {
  6. @Input
  7. var versionName: String = ""
  8. @Input
  9. var appName: String = ""
  10. @Input
  11. var nexusUrl: String = ""
  12. @Input
  13. var nexusUsername: String = ""
  14. @Input
  15. var nexusPassword: String = ""
  16. @Input
  17. var tagPrefix: String = ""
  18. get() = field.lowercase()
  19. private val buildType: String
  20. get() = if (tagPrefix == "development") "debug" else "release"
  21. @TaskAction
  22. fun upload() {
  23. val apkPath = project.layout
  24. .projectDirectory
  25. .file("build/outputs/apk/$buildType/$appName-$versionName-$buildType.apk")
  26. val changelogPath = project.layout
  27. .projectDirectory
  28. .file("build/outputs/apk/$buildType/CHANGELOG-$appName-$versionName-$tagPrefix.md")
  29. val apkFile = apkPath.asFile
  30. val changelogFile = changelogPath.asFile
  31. when {
  32. !apkFile.exists() -> {
  33. throw RuntimeException("APK file does not exist: ${apkFile.absolutePath}")
  34. }
  35. !changelogFile.exists() -> {
  36. throw RuntimeException("Changelog file does not exist: ${changelogFile.absolutePath}")
  37. }
  38. nexusUrl.isBlank() || nexusUsername.isBlank() || nexusPassword.isBlank() -> {
  39. throw RuntimeException("Nexus credentials are not set")
  40. }
  41. else -> {
  42. val targetUrl = "$nexusUrl/kompakt-$appName/$versionName/$tagPrefix"
  43. project.exec {
  44. commandLine(
  45. "curl",
  46. "-v",
  47. "-u",
  48. "$nexusUsername:$nexusPassword",
  49. "--upload-file",
  50. apkFile.absolutePath,
  51. "$targetUrl/${apkFile.name}"
  52. )
  53. }
  54. project.exec {
  55. commandLine(
  56. "curl",
  57. "-v",
  58. "-u",
  59. "$nexusUsername:$nexusPassword",
  60. "--upload-file",
  61. changelogFile.absolutePath,
  62. "$targetUrl/${changelogFile.name}"
  63. )
  64. }
  65. }
  66. }
  67. }
  68. }