|
| 1 | +package mill.contrib.sonatypecentral |
| 2 | + |
| 3 | +import com.lumidion.sonatype.central.client.core.{PublishingType, SonatypeCredentials} |
| 4 | +import mill._ |
| 5 | +import scalalib._ |
| 6 | +import define.{ExternalModule, Task} |
| 7 | +import mill.api.Result |
| 8 | +import mill.contrib.sonatypecentral.SonatypeCentralPublishModule.{ |
| 9 | + defaultAwaitTimeout, |
| 10 | + defaultConnectTimeout, |
| 11 | + defaultCredentials, |
| 12 | + defaultReadTimeout, |
| 13 | + getPublishingTypeFromReleaseFlag, |
| 14 | + getSonatypeCredentials |
| 15 | +} |
| 16 | +import mill.scalalib.publish.Artifact |
| 17 | +import mill.scalalib.publish.SonatypeHelpers.{ |
| 18 | + PASSWORD_ENV_VARIABLE_NAME, |
| 19 | + USERNAME_ENV_VARIABLE_NAME |
| 20 | +} |
| 21 | + |
| 22 | +trait SonatypeCentralPublishModule extends PublishModule { |
| 23 | + def sonatypeCentralGpgArgs: T[String] = Task { |
| 24 | + PublishModule.defaultGpgArgsForPassphrase(Task.env.get("MILL_PGP_PASSPHRASE")).mkString(",") |
| 25 | + } |
| 26 | + |
| 27 | + def sonatypeCentralConnectTimeout: T[Int] = Task { defaultConnectTimeout } |
| 28 | + |
| 29 | + def sonatypeCentralReadTimeout: T[Int] = Task { defaultReadTimeout } |
| 30 | + |
| 31 | + def sonatypeCentralAwaitTimeout: T[Int] = Task { defaultAwaitTimeout } |
| 32 | + |
| 33 | + def sonatypeCentralShouldRelease: T[Boolean] = Task { true } |
| 34 | + |
| 35 | + def publishSonatypeCentral( |
| 36 | + username: String = defaultCredentials, |
| 37 | + password: String = defaultCredentials |
| 38 | + ): define.Command[Unit] = |
| 39 | + Task.Command { |
| 40 | + val publishData = publishArtifacts() |
| 41 | + val fileMapping = publishData.withConcretePath._1 |
| 42 | + val artifact = publishData.meta |
| 43 | + val finalCredentials = getSonatypeCredentials(username, password)() |
| 44 | + PublishModule.pgpImportSecretIfProvided(Task.env) |
| 45 | + val publisher = new SonatypeCentralPublisher( |
| 46 | + credentials = finalCredentials, |
| 47 | + gpgArgs = sonatypeCentralGpgArgs().split(",").toIndexedSeq, |
| 48 | + connectTimeout = sonatypeCentralConnectTimeout(), |
| 49 | + readTimeout = sonatypeCentralReadTimeout(), |
| 50 | + log = Task.log, |
| 51 | + workspace = Task.workspace, |
| 52 | + env = Task.env, |
| 53 | + awaitTimeout = sonatypeCentralAwaitTimeout() |
| 54 | + ) |
| 55 | + publisher.publish( |
| 56 | + fileMapping, |
| 57 | + artifact, |
| 58 | + getPublishingTypeFromReleaseFlag(sonatypeCentralShouldRelease()) |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +object SonatypeCentralPublishModule extends ExternalModule { |
| 64 | + |
| 65 | + val defaultCredentials: String = "" |
| 66 | + val defaultReadTimeout: Int = 60000 |
| 67 | + val defaultConnectTimeout: Int = 5000 |
| 68 | + val defaultAwaitTimeout: Int = 120 * 1000 |
| 69 | + val defaultShouldRelease: Boolean = true |
| 70 | + |
| 71 | + def publishAll( |
| 72 | + publishArtifacts: mill.main.Tasks[PublishModule.PublishData], |
| 73 | + username: String = defaultCredentials, |
| 74 | + password: String = defaultCredentials, |
| 75 | + shouldRelease: Boolean = defaultShouldRelease, |
| 76 | + gpgArgs: String = "", |
| 77 | + readTimeout: Int = defaultReadTimeout, |
| 78 | + connectTimeout: Int = defaultConnectTimeout, |
| 79 | + awaitTimeout: Int = defaultAwaitTimeout, |
| 80 | + bundleName: String = "" |
| 81 | + ): Command[Unit] = Task.Command { |
| 82 | + |
| 83 | + val artifacts: Seq[(Seq[(os.Path, String)], Artifact)] = |
| 84 | + Task.sequence(publishArtifacts.value)().map { |
| 85 | + case data @ PublishModule.PublishData(_, _) => data.withConcretePath |
| 86 | + } |
| 87 | + |
| 88 | + val finalBundleName = if (bundleName.isEmpty) None else Some(bundleName) |
| 89 | + val finalCredentials = getSonatypeCredentials(username, password)() |
| 90 | + PublishModule.pgpImportSecretIfProvided(Task.env) |
| 91 | + val publisher = new SonatypeCentralPublisher( |
| 92 | + credentials = finalCredentials, |
| 93 | + gpgArgs = gpgArgs match { |
| 94 | + case "" => PublishModule.defaultGpgArgsForPassphrase(Task.env.get("MILL_PGP_PASSPHRASE")) |
| 95 | + case gpgArgs => gpgArgs.split(",").toIndexedSeq |
| 96 | + }, |
| 97 | + connectTimeout = connectTimeout, |
| 98 | + readTimeout = readTimeout, |
| 99 | + log = Task.log, |
| 100 | + workspace = Task.workspace, |
| 101 | + env = Task.env, |
| 102 | + awaitTimeout = awaitTimeout |
| 103 | + ) |
| 104 | + publisher.publishAll( |
| 105 | + getPublishingTypeFromReleaseFlag(shouldRelease), |
| 106 | + finalBundleName, |
| 107 | + artifacts: _* |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + private def getPublishingTypeFromReleaseFlag(shouldRelease: Boolean): PublishingType = { |
| 112 | + if (shouldRelease) { |
| 113 | + PublishingType.AUTOMATIC |
| 114 | + } else { |
| 115 | + PublishingType.USER_MANAGED |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private def getSonatypeCredential( |
| 120 | + credentialParameterValue: String, |
| 121 | + credentialName: String, |
| 122 | + envVariableName: String |
| 123 | + ): Task[String] = Task.Anon { |
| 124 | + if (credentialParameterValue.nonEmpty) { |
| 125 | + Result.Success(credentialParameterValue) |
| 126 | + } else { |
| 127 | + (for { |
| 128 | + credential <- Task.env.get(envVariableName) |
| 129 | + } yield { |
| 130 | + Result.Success(credential) |
| 131 | + }).getOrElse( |
| 132 | + Result.Failure( |
| 133 | + s"No $credentialName set. Consider using the $envVariableName environment variable or passing `$credentialName` argument" |
| 134 | + ) |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private def getSonatypeCredentials( |
| 140 | + usernameParameterValue: String, |
| 141 | + passwordParameterValue: String |
| 142 | + ): Task[SonatypeCredentials] = Task.Anon { |
| 143 | + val username = |
| 144 | + getSonatypeCredential(usernameParameterValue, "username", USERNAME_ENV_VARIABLE_NAME)() |
| 145 | + val password = |
| 146 | + getSonatypeCredential(passwordParameterValue, "password", PASSWORD_ENV_VARIABLE_NAME)() |
| 147 | + Result.Success(SonatypeCredentials(username, password)) |
| 148 | + } |
| 149 | + |
| 150 | + lazy val millDiscover: mill.define.Discover = mill.define.Discover[this.type] |
| 151 | +} |
0 commit comments