From 31435f7ddcf6b0f7387301cad1aa057a0658e8d3 Mon Sep 17 00:00:00 2001 From: Miroslav Slivka Date: Wed, 4 Dec 2019 15:20:12 +0100 Subject: [PATCH] adding possibility to specify custom repositories for coursier (#1521) --- .../scala/org/scalafmt/dynamic/ScalafmtDynamic.scala | 11 ++++++++--- .../scalafmt/dynamic/ScalafmtDynamicDownloader.scala | 6 ++++-- .../main/java/org/scalafmt/interfaces/Scalafmt.java | 6 +++++- 3 files changed, 17 insertions(+), 6 deletions(-) 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 a26e35ec74..c8f70eff4a 100644 --- a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamic.scala +++ b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamic.scala @@ -1,10 +1,11 @@ package org.scalafmt.dynamic import java.net.URLClassLoader -import java.nio.file.attribute.FileTime import java.nio.file.{Files, Path} +import java.nio.file.attribute.FileTime import com.typesafe.config.{ConfigException, ConfigFactory} +import coursierapi.{MavenRepository, Repository} import org.scalafmt.dynamic.ScalafmtDynamic.{FormatEval, FormatResult} import org.scalafmt.dynamic.ScalafmtDynamicDownloader._ import org.scalafmt.dynamic.exceptions._ @@ -14,10 +15,10 @@ import org.scalafmt.interfaces._ import scala.concurrent.ExecutionContext import scala.util.Try import scala.util.control.NonFatal -import java.io.OutputStreamWriter final case class ScalafmtDynamic( reporter: ScalafmtReporter, + repositories: List[Repository], respectVersion: Boolean, respectExcludeFilters: Boolean, defaultVersion: String, @@ -30,6 +31,7 @@ final case class ScalafmtDynamic( def this() = this( ConsoleScalafmtReporter, + Nil, true, true, BuildInfo.stable, @@ -160,7 +162,7 @@ final case class ScalafmtDynamic( ): FormatEval[ScalafmtReflect] = { formatCache.getOrAddToCache(version) { () => val writer = reporter.downloadOutputStreamWriter() - val downloader = new ScalafmtDynamicDownloader(writer) + val downloader = new ScalafmtDynamicDownloader(writer, repositories) downloader .download(version) .left @@ -235,6 +237,9 @@ final case class ScalafmtDynamic( None } } + + override def withMavenRepositories(repositories: String*): Scalafmt = + copy(repositories = repositories.map(MavenRepository.of).toList) } object ScalafmtDynamic { diff --git a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamicDownloader.scala b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamicDownloader.scala index 1f28a2fabf..c0a206efff 100644 --- a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamicDownloader.scala +++ b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtDynamicDownloader.scala @@ -5,7 +5,6 @@ import java.net.URL import coursierapi._ import org.scalafmt.dynamic.ScalafmtDynamicDownloader._ -import org.scalafmt.dynamic.ScalafmtVersion import org.scalafmt.dynamic.ScalafmtVersion.InvalidVersionException import scala.collection.JavaConverters._ @@ -14,6 +13,7 @@ import scala.util.Try class ScalafmtDynamicDownloader( downloadProgressWriter: OutputStreamWriter, + customRepositories: List[Repository], ttl: Option[Duration] = None ) { @@ -86,7 +86,9 @@ class ScalafmtDynamicDownloader( private def repositories: Array[Repository] = { // Default repositories are ivy2local, central and also anything in COURSIER_REPOSITORIES overrides - Repository.defaults().asScala.toArray ++ Array( + customRepositories.toArray ++ Repository + .defaults() + .asScala ++ Array( MavenRepository.of( "https://oss.sonatype.org/content/repositories/snapshots" ), diff --git a/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/Scalafmt.java b/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/Scalafmt.java index 97ff56ab7f..69168f4d56 100644 --- a/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/Scalafmt.java +++ b/scalafmt-interfaces/src/main/java/org/scalafmt/interfaces/Scalafmt.java @@ -1,6 +1,5 @@ package org.scalafmt.interfaces; -import java.io.Writer; import java.nio.file.Path; import java.util.Iterator; import java.util.NoSuchElementException; @@ -74,6 +73,11 @@ public interface Scalafmt { */ Scalafmt withReporter(ScalafmtReporter reporter); + /** + * Use this repositories to resolve dependencies. + */ + Scalafmt withMavenRepositories(String ... repositories); + /** * Clear internal caches such as classloaded Scalafmt instances. */