Skip to content

Commit

Permalink
Interfaces: add credentials to public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Aug 27, 2023
1 parent 8213992 commit 14ed347
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import java.net.URL
import scala.collection.JavaConverters._
import scala.util.Try

import org.scalafmt.interfaces.RepositoryCredential

import coursierapi.{Dependency => CoursierDependency, _}

class CoursierDependencyDownloader(
private class CoursierDependencyDownloader(
downloadProgressWriter: OutputStreamWriter,
customRepositories: Seq[Repository]
customRepositories: Seq[Repository],
customRepositoryCredentials: String
) extends DependencyDownloader {

import CoursierDependencyDownloader._

override def download(dependencies: Seq[Dependency]): Try[Seq[URL]] = Try {
val coursierDependencies = dependencies
.map(x => CoursierDependency.of(x.group, x.artifact, x.version))
Expand All @@ -22,7 +27,9 @@ class CoursierDependencyDownloader(
val settings = { Fetch.create().withCache(cache) }
.withRepositories(repositories: _*)
.withDependencies(coursierDependencies: _*)
settings.fetch().asScala.map(_.toURI.toURL).toList
withSysProp("coursier.credentials", customRepositoryCredentials) {
settings.fetch().asScala.map(_.toURI.toURL).toList
}
}

private def repositories: Seq[Repository] =
Expand All @@ -39,7 +46,30 @@ object CoursierDependencyDownloader extends DependencyDownloaderFactory {
override def create(properties: ScalafmtProperties): DependencyDownloader = {
val writer = properties.reporter.downloadOutputStreamWriter()
val repositories = properties.repositories.map(MavenRepository.of)
new CoursierDependencyDownloader(writer, repositories)
val credentials = getCoursierCredentials(properties.repositoryCredentials)
new CoursierDependencyDownloader(writer, repositories, credentials)
}

private def withSysProp[A](prop: String, value: String)(f: => A): A = {
val prevValue = System.setProperty(prop, value)
try f
finally
if (prevValue eq null)
System.clearProperty(prop)
else if (prevValue != value)
System.setProperty(prop, prevValue)
}

private def getCoursierCredentials(
credentials: Seq[RepositoryCredential]
): String = {
val credBuf = new StringBuffer()
credentials.foreach { x =>
credBuf.append('\n').append(x.host)
if (x.realm.nonEmpty) credBuf.append('(').append(x.realm).append(')')
credBuf.append(' ').append(x.username).append(':').append(x.password)
}
credBuf.append('\n').toString
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final case class ScalafmtDynamic(
moduleLoader: ScalafmtModuleLoader,
configLoader: ScalafmtConfigLoader
) extends Scalafmt
with RepositoryCredential.ScalafmtExtension
with ScalafmtSessionFactory {

def this() = this(
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {

Expand All @@ -20,6 +21,9 @@ 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 =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 {

/**
* Builder method.
*
* @param credentials maven repository credentials to use when resolving
* @return an updated interface instance
*/
Scalafmt withRepositoryCredentials(RepositoryCredential... credentials);

}

}

0 comments on commit 14ed347

Please sign in to comment.