Skip to content

Commit

Permalink
Cache Scalafix instances globally
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Sep 30, 2024
1 parent 54b8d07 commit c037bf0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions mill-scalafix/src/com/goyeau/mill/scalafix/ScalafixCache.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.goyeau.mill.scalafix

import com.goyeau.mill.scalafix.CoursierUtils
import coursier.core.Repository
import scalafix.interfaces.Scalafix

import scala.collection.mutable
import scala.ref.SoftReference
import scala.jdk.CollectionConverters._

private[scalafix] object ScalafixCache {

private val cache = mutable.Map.empty[(String, Seq[Repository]), SoftReference[Scalafix]]

def getOrElseCreate(scalaVersion: String, repositories: Seq[Repository]) =
cache.get((scalaVersion, repositories)) match {
case Some(SoftReference(value)) => value
case _ =>
val newResult =
Scalafix.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava)
cache.update((scalaVersion, repositories), SoftReference(newResult))
newResult
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ object ScalafixModule {
wd: os.Path
): Result[Unit] =
if (sources.nonEmpty) {
val scalafix = Scalafix
.fetchAndClassloadInstance(scalaVersion, repositories.map(CoursierUtils.toApiRepository).asJava)
val scalafix = ScalafixCache
.getOrElseCreate(scalaVersion, repositories)
.newArguments()
.withParsedArguments(args.asJava)
.withWorkingDirectory(wd.toNIO)
Expand Down

0 comments on commit c037bf0

Please sign in to comment.