Skip to content

Commit

Permalink
adding possibility to specify custom repositories for coursier (scala…
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Slivka committed Dec 6, 2019
1 parent bb31868 commit a724e32
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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._
Expand All @@ -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,
Expand All @@ -30,6 +31,7 @@ final case class ScalafmtDynamic(

def this() = this(
ConsoleScalafmtReporter,
Nil,
true,
true,
BuildInfo.stable,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -235,6 +237,9 @@ final case class ScalafmtDynamic(
None
}
}

override def withRepositories(repositories: String*): Scalafmt =
copy(repositories = repositories.map(MavenRepository.of).toList)
}

object ScalafmtDynamic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -14,6 +13,7 @@ import scala.util.Try

class ScalafmtDynamicDownloader(
downloadProgressWriter: OutputStreamWriter,
customRepositories: List[Repository],
ttl: Option[Duration] = None
) {

Expand Down Expand Up @@ -86,7 +86,10 @@ 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
.toArray ++ Array(
MavenRepository.of(
"https://oss.sonatype.org/content/repositories/snapshots"
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,6 +73,11 @@ public interface Scalafmt {
*/
Scalafmt withReporter(ScalafmtReporter reporter);

/**
* Use this repositories to resolve dependencies.
*/
Scalafmt withRepositories(String ... repositories);

/**
* Clear internal caches such as classloaded Scalafmt instances.
*/
Expand Down

0 comments on commit a724e32

Please sign in to comment.