1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package mill.scalalib.scalafmt
import mill._
import mill.define.{Discover, ExternalModule, Worker}
import mill.modules.Jvm
import mill.api.Ctx
import scala.collection.mutable
object ScalafmtWorkerModule extends ExternalModule {
def worker: Worker[ScalafmtWorker] = T.worker { new ScalafmtWorker() }
lazy val millDiscover = Discover[this.type]
}
private[scalafmt] class ScalafmtWorker {
private val reformatted: mutable.Map[os.Path, Int] = mutable.Map.empty
private var configSig: Int = 0
def reformat(input: Seq[PathRef],
scalafmtConfig: PathRef,
scalafmtClasspath: Agg[os.Path])(implicit ctx: Ctx): Unit = {
val toFormat =
if (scalafmtConfig.sig != configSig) input
else
input.filterNot(ref => reformatted.get(ref.path).contains(ref.sig))
if (toFormat.nonEmpty) {
ctx.log.info(s"Formatting ${toFormat.size} Scala sources")
reformatAction(toFormat.map(_.path),
scalafmtConfig.path,
scalafmtClasspath)
reformatted ++= toFormat.map { ref =>
val updRef = PathRef(ref.path)
updRef.path -> updRef.sig
}
configSig = scalafmtConfig.sig
} else {
ctx.log.info(s"Everything is formatted already")
}
}
private val cliFlags = Seq("--non-interactive", "--quiet")
private def reformatAction(toFormat: Seq[os.Path],
config: os.Path,
classpath: Agg[os.Path])(implicit ctx: Ctx) = {
val configFlags =
if (os.exists(config)) Seq("--config", config.toString) else Seq.empty
Jvm.runSubprocess(
"org.scalafmt.cli.Cli",
classpath,
mainArgs = toFormat.map(_.toString) ++ configFlags ++ cliFlags
)
}
}
|