summaryrefslogtreecommitdiff
path: root/scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala
diff options
context:
space:
mode:
authorNikolay Tatarinov <5min4eq.unity@gmail.com>2018-05-06 11:26:21 +0300
committerGitHub <noreply@github.com>2018-05-06 11:26:21 +0300
commitf7a02a46f847d4433cd771840fd0b6cc314215d8 (patch)
tree612a1d01a63a75dedd1c53bbeccf3083b9f52a65 /scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala
parent32c7515d6507e1898562203ea7c9d13a53d933c1 (diff)
downloadmill-f7a02a46f847d4433cd771840fd0b6cc314215d8.tar.gz
mill-f7a02a46f847d4433cd771840fd0b6cc314215d8.tar.bz2
mill-f7a02a46f847d4433cd771840fd0b6cc314215d8.zip
Scalafmt support (#308)
* add scalafmt module, that formats all sources files on every run * scalafmt worker that internally chaches reformatted files * move jvm process call to helper method * use scala 2.12.4 to resolve scalafmt deps; check for config file existence; add quiet flags to scalafmt CLI * make a scalafmt worker a singleton * add tests for scalafmt module * add reformatAll command * tests for reformatAll command * add docs about scalafmt support
Diffstat (limited to 'scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala')
-rw-r--r--scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala58
1 files changed, 58 insertions, 0 deletions
diff --git a/scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala b/scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala
new file mode 100644
index 00000000..3b325b4c
--- /dev/null
+++ b/scalalib/src/mill/scalalib/scalafmt/ScalafmtModule.scala
@@ -0,0 +1,58 @@
+package mill.scalalib.scalafmt
+
+import ammonite.ops.{exists, ls, pwd}
+import mill._
+import mill.define._
+import mill.scalalib._
+
+trait ScalafmtModule extends JavaModule {
+
+ def reformat(): Command[Unit] = T.command {
+ ScalafmtWorkerModule
+ .worker()
+ .reformat(
+ filesToFormat(sources()),
+ scalafmtConfig().head,
+ scalafmtDeps().map(_.path)
+ )
+ }
+
+ def scalafmtVersion: T[String] = "1.5.1"
+
+ def scalafmtConfig: Sources = T.sources(pwd / ".scalafmt.conf")
+
+ def scalafmtDeps: T[Agg[PathRef]] = T {
+ Lib.resolveDependencies(
+ ScalaWorkerModule.repositories,
+ Lib.depToDependency(_, "2.12.4"),
+ Seq(ivy"com.geirsson::scalafmt-cli:${scalafmtVersion()}")
+ )
+ }
+
+ protected def filesToFormat(sources: Seq[PathRef]) = {
+ for {
+ pathRef <- sources if exists(pathRef.path)
+ file <- ls.rec(pathRef.path) if file.isFile && file.ext == "scala"
+ } yield PathRef(file)
+ }
+
+}
+
+object ScalafmtModule extends ExternalModule with ScalafmtModule {
+
+ def reformatAll(sources: mill.main.Tasks[Seq[PathRef]]): Command[Unit] =
+ T.command {
+ val files = Task.sequence(sources.value)().flatMap(filesToFormat)
+ ScalafmtWorkerModule
+ .worker()
+ .reformat(
+ files,
+ scalafmtConfig().head,
+ scalafmtDeps().map(_.path)
+ )
+ }
+
+ implicit def millScoptTargetReads[T] = new mill.main.Tasks.Scopt[T]()
+
+ lazy val millDiscover = Discover[this.type]
+}