Skip to content

Commit 52d5f6c

Browse files
committed
NIT refactor code so that isSonatype is calculated only once
1 parent 8e008da commit 52d5f6c

File tree

1 file changed

+50
-64
lines changed
  • modules/cli/src/main/scala/scala/cli/commands/publish

1 file changed

+50
-64
lines changed

modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -736,51 +736,50 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
736736

737737
val ec = builds.head.options.finalCache.ec
738738

739-
def authOpt(repo: String): Either[BuildException, Option[Authentication]] = either {
740-
val isHttps = {
741-
val uri = new URI(repo)
742-
uri.getScheme == "https"
743-
}
744-
val hostOpt = Option.when(isHttps)(new URI(repo).getHost)
745-
val maybeCredentials: Either[BuildException, Option[PublishCredentials]] = hostOpt match {
746-
case None => Right(None)
747-
case Some(host) =>
748-
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
749-
credListOpt.flatMap { credList =>
750-
credList.find { cred =>
751-
cred.host == host &&
752-
(isHttps || cred.httpsOnly.contains(false))
739+
def authOpt(repo: String, isSonatype: Boolean): Either[BuildException, Option[Authentication]] =
740+
either {
741+
val isHttps = {
742+
val uri = new URI(repo)
743+
uri.getScheme == "https"
744+
}
745+
val hostOpt = Option.when(isHttps)(new URI(repo).getHost)
746+
val maybeCredentials: Either[BuildException, Option[PublishCredentials]] = hostOpt match {
747+
case None => Right(None)
748+
case Some(host) =>
749+
configDb().get(Keys.publishCredentials).wrapConfigException.map { credListOpt =>
750+
credListOpt.flatMap { credList =>
751+
credList.find { cred =>
752+
cred.host == host &&
753+
(isHttps || cred.httpsOnly.contains(false))
754+
}
753755
}
754756
}
755-
}
756-
}
757-
val isSonatype =
758-
hostOpt.exists(host => host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org"))
759-
val passwordOpt = publishOptions.contextual(isCi).repoPassword match {
760-
case None => value(maybeCredentials).flatMap(_.password)
761-
case other => other.map(_.toConfig)
762-
}
763-
passwordOpt.map(_.get()) match {
764-
case None => None
765-
case Some(password) =>
766-
val userOpt = publishOptions.contextual(isCi).repoUser match {
767-
case None => value(maybeCredentials).flatMap(_.user)
768-
case other => other.map(_.toConfig)
769-
}
770-
val realmOpt = publishOptions.contextual(isCi).repoRealm match {
771-
case None =>
772-
value(maybeCredentials)
773-
.flatMap(_.realm)
774-
.orElse {
775-
if (isSonatype) Some("Sonatype Nexus Repository Manager")
776-
else None
777-
}
778-
case other => other
779-
}
780-
val auth = Authentication(userOpt.fold("")(_.get().value), password.value)
781-
Some(realmOpt.fold(auth)(auth.withRealm))
757+
}
758+
val passwordOpt = publishOptions.contextual(isCi).repoPassword match {
759+
case None => value(maybeCredentials).flatMap(_.password)
760+
case other => other.map(_.toConfig)
761+
}
762+
passwordOpt.map(_.get()) match {
763+
case None => None
764+
case Some(password) =>
765+
val userOpt = publishOptions.contextual(isCi).repoUser match {
766+
case None => value(maybeCredentials).flatMap(_.user)
767+
case other => other.map(_.toConfig)
768+
}
769+
val realmOpt = publishOptions.contextual(isCi).repoRealm match {
770+
case None =>
771+
value(maybeCredentials)
772+
.flatMap(_.realm)
773+
.orElse {
774+
if (isSonatype) Some("Sonatype Nexus Repository Manager")
775+
else None
776+
}
777+
case other => other
778+
}
779+
val auth = Authentication(userOpt.fold("")(_.get().value), password.value)
780+
Some(realmOpt.fold(auth)(auth.withRealm))
781+
}
782782
}
783-
}
784783

785784
val repoParams = {
786785

@@ -812,32 +811,28 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
812811
}
813812
}
814813

814+
val isSonatype: Boolean = {
815+
val uri = new URI(repoParams.repo.snapshotRepo.root)
816+
val hostOpt = Option.when(uri.getScheme == "https")(uri.getHost)
817+
818+
hostOpt.exists(host => host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org"))
819+
}
820+
815821
val now = Instant.now()
816822
val (fileSet0, modVersionOpt) = value {
817823
it
818824
// TODO Allow to add test JARs to the main build artifacts
819825
.filter(_._1.scope != Scope.Test)
820826
.map {
821827
case (build, docBuildOpt) =>
822-
val isSonatype = {
823-
val hostOpt = {
824-
val repo = repoParams.repo.snapshotRepo.root
825-
val uri = new URI(repo)
826-
if (uri.getScheme == "https") Some(uri.getHost)
827-
else None
828-
}
829-
hostOpt.exists(host =>
830-
host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org")
831-
)
832-
}
833828
buildFileSet(
834829
build,
835830
docBuildOpt,
836831
workingDir,
837832
now,
838833
isIvy2LocalLike = repoParams.isIvy2LocalLike,
839834
isCi = isCi,
840-
isSonatype = isSonatype,
835+
isSonatype,
841836
logger
842837
)
843838
}
@@ -1013,7 +1008,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
10131008
else fileSet2.order(ec).unsafeRun()(ec)
10141009

10151010
val isSnapshot0 = modVersionOpt.exists(_._2.endsWith("SNAPSHOT"))
1016-
val authOpt0 = value(authOpt(repoParams.repo.repo(isSnapshot0).root))
1011+
val authOpt0 = value(authOpt(repoParams.repo.repo(isSnapshot0).root, isSonatype))
10171012
if (repoParams.shouldAuthenticate && authOpt0.isEmpty)
10181013
logger.diagnostic(
10191014
"Publishing to a repository that needs authentication, but no credentials are available.",
@@ -1038,15 +1033,6 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
10381033
}
10391034
}
10401035

1041-
val isHttps = {
1042-
val uri = new URI(repoParams.repo.repo(isSnapshot0).root)
1043-
uri.getScheme == "https"
1044-
}
1045-
val hostOpt = Option.when(isHttps)(new URI(repoParams.repo.repo(isSnapshot0).root).getHost)
1046-
1047-
val isSonatype =
1048-
hostOpt.exists(host => host == "oss.sonatype.org" || host.endsWith(".oss.sonatype.org"))
1049-
10501036
val retainedRepo = hooksDataOpt match {
10511037
case None => // dummy mode
10521038
repoParams0.repo.repo(isSnapshot0)

0 commit comments

Comments
 (0)