From 5731480f031669d05a74cea1169404130c1efb66 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 27 Aug 2023 10:17:33 -0700 Subject: [PATCH] Interfaces: add credentials to public interface --- .../CoursierDependencyDownloader.scala | 12 +++++++--- .../scalafmt/dynamic/ScalafmtDynamic.scala | 6 +++++ .../scalafmt/dynamic/ScalafmtProperties.scala | 6 +++++ .../interfaces/RepositoryCredential.java | 24 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/RepositoryCredential.java diff --git a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/CoursierDependencyDownloader.scala b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/CoursierDependencyDownloader.scala index 3cc3c9fd1a..594c8775ab 100644 --- a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/CoursierDependencyDownloader.scala +++ b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/CoursierDependencyDownloader.scala @@ -1,14 +1,14 @@ package org.scalafmt.dynamic import java.io.OutputStreamWriter -import java.net.URL +import java.net.{URI, URL} import scala.collection.JavaConverters._ import scala.util.Try import coursierapi.{Dependency => CoursierDependency, _} -class CoursierDependencyDownloader( +private class CoursierDependencyDownloader( downloadProgressWriter: OutputStreamWriter, customRepositories: Seq[Repository] ) extends DependencyDownloader { @@ -38,7 +38,13 @@ object CoursierDependencyDownloader extends DependencyDownloaderFactory { override def create(properties: ScalafmtProperties): DependencyDownloader = { val writer = properties.reporter.downloadOutputStreamWriter() - val repositories = properties.repositories.map(MavenRepository.of) + val repositories = properties.repositories.map { x => + val host = new URI(x).getHost + val repo = MavenRepository.of(x) + properties.repositoryCredentials.find(_.host == host).fold(repo) { cred => + repo.withCredentials(Credentials.of(cred.username, cred.password)) + } + } new CoursierDependencyDownloader(writer, repositories) } diff --git a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamic.scala b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamic.scala index e011415ac0..f844306528 100644 --- a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamic.scala +++ b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamic.scala @@ -10,6 +10,7 @@ final case class ScalafmtDynamic( moduleLoader: ScalafmtModuleLoader, configLoader: ScalafmtConfigLoader ) extends Scalafmt + with RepositoryCredential.ScalafmtExtension with ScalafmtSessionFactory { def this() = this( @@ -40,6 +41,11 @@ final case class ScalafmtDynamic( override def withMavenRepositories(value: String*): Scalafmt = copy(properties = properties.withMavenRepositories(value)) + override def withRepositoryCredentials( + value: RepositoryCredential* + ): Scalafmt = + copy(properties = properties.withRepositoryCredentials(value)) + override def format(config: Path, file: Path, code: String): String = createSession(config).format(file, code) diff --git a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtProperties.scala b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtProperties.scala index 04dee3ebf8..46d73a6662 100644 --- a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtProperties.scala +++ b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtProperties.scala @@ -8,6 +8,7 @@ import org.scalafmt.interfaces._ final case class ScalafmtProperties( reporter: ScalafmtReporter = ConsoleScalafmtReporter, repositories: Seq[String] = Nil, + repositoryCredentials: Seq[RepositoryCredential] = Nil, respectExcludeFilters: Boolean = true ) { @@ -20,6 +21,11 @@ final case class ScalafmtProperties( def withMavenRepositories(value: Seq[String]): ScalafmtProperties = copy(repositories = value) + def withRepositoryCredentials( + value: Seq[RepositoryCredential] + ): ScalafmtProperties = + copy(repositoryCredentials = value) + def reportError(file: Path, error: ScalafmtDynamicError): Unit = error match { case _: ConfigMissingVersion => diff --git a/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/RepositoryCredential.java b/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/RepositoryCredential.java new file mode 100644 index 0000000000..6c8f87f529 --- /dev/null +++ b/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/RepositoryCredential.java @@ -0,0 +1,24 @@ +package org.scalafmt.interfaces; + +public final class RepositoryCredential { + public final String host; + public final String username; + public final String password; + public final String realm; + + public RepositoryCredential(String host, String username, String password) { + this(host, username, password, ""); + } + + public RepositoryCredential(String host, String username, String password, String realm) { + this.host = host; + this.username = username; + this.password = password; + this.realm = realm; + } + + public interface ScalafmtExtension { + Scalafmt withRepositoryCredentials(RepositoryCredential... credentials); + } + +}