blob: f9c7e9b48f40e1a15260e0daeb67bdf955b87e07 (
plain) (
blame)
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
58
59
60
61
62
63
64
|
package mill.scalalib.scalafmt
import java.nio.file.{Paths => JPaths}
import mill._
import mill.define.{Discover, ExternalModule, Worker}
import mill.api.Ctx
import org.scalafmt.interfaces.Scalafmt
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)(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)
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)(implicit ctx: Ctx) = {
val scalafmt =
Scalafmt
.create(this.getClass.getClassLoader)
.withRespectVersion(false)
val configPath =
if (os.exists(config))
config.toNIO
else
JPaths.get(getClass.getResource("default.scalafmt.conf").toURI)
toFormat.foreach { pathToFormat =>
val code = os.read(pathToFormat)
val formatteCode = scalafmt.format(configPath, pathToFormat.toNIO, code)
os.write.over(pathToFormat, formatteCode)
}
}
}
|