summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Galy <guilgaly@users.noreply.github.com>2019-05-19 05:23:18 +0200
committerLi Haoyi <haoyi.sg@gmail.com>2019-05-19 11:23:18 +0800
commit052af24a530f8bc0532b368a2d360ff30f67d7b8 (patch)
treecd512450fbdf61a3928e3950c5caffb028aae0de
parentc4a65baab92890d2d5682329a87469bec605fe5d (diff)
downloadmill-052af24a530f8bc0532b368a2d360ff30f67d7b8.tar.gz
mill-052af24a530f8bc0532b368a2d360ff30f67d7b8.tar.bz2
mill-052af24a530f8bc0532b368a2d360ff30f67d7b8.zip
Switch from scalafmt-cli to scalafmt-dynamic (#600)
-rwxr-xr-xbuild.sc3
-rw-r--r--docs/pages/2 - Configuring Mill.md6
-rw-r--r--scalalib/resources/mill/scalalib/scalafmt/default.scalafmt.conf0
-rw-r--r--scalalib/src/scalafmt/ScalafmtModule.scala16
-rw-r--r--scalalib/src/scalafmt/ScalafmtWorker.scala37
5 files changed, 32 insertions, 30 deletions
diff --git a/build.sc b/build.sc
index 215e8d37..c51e8195 100755
--- a/build.sc
+++ b/build.sc
@@ -143,7 +143,8 @@ object scalalib extends MillModule {
def moduleDeps = Seq(main, scalalib.api)
def ivyDeps = Agg(
- ivy"org.scala-sbt:test-interface:1.0"
+ ivy"org.scala-sbt:test-interface:1.0",
+ ivy"org.scalameta::scalafmt-dynamic:2.0.0-RC6"
)
def genTask(m: ScalaModule) = T.task{
diff --git a/docs/pages/2 - Configuring Mill.md b/docs/pages/2 - Configuring Mill.md
index dbcbcee4..f6ca86a0 100644
--- a/docs/pages/2 - Configuring Mill.md
+++ b/docs/pages/2 - Configuring Mill.md
@@ -237,6 +237,12 @@ Now you can reformat code with `mill foo.reformat` command.
You can also reformat your project's code globally with `mill mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources` command.
It will reformat all sources that matches `__.sources` query.
+If you add a `.scalafmt.conf` file at the root of you project, it will be used
+to configure formatting. It can contain a `version` key to specify the scalafmt
+version used to format your code. See the
+[scalafmt configuration documentation](https://scalameta.org/scalafmt/docs/configuration.html)
+for details.
+
## Common Configuration
```scala
diff --git a/scalalib/resources/mill/scalalib/scalafmt/default.scalafmt.conf b/scalalib/resources/mill/scalalib/scalafmt/default.scalafmt.conf
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/scalalib/resources/mill/scalalib/scalafmt/default.scalafmt.conf
diff --git a/scalalib/src/scalafmt/ScalafmtModule.scala b/scalalib/src/scalafmt/ScalafmtModule.scala
index 6a81d975..ea254e6d 100644
--- a/scalalib/src/scalafmt/ScalafmtModule.scala
+++ b/scalalib/src/scalafmt/ScalafmtModule.scala
@@ -11,23 +11,12 @@ trait ScalafmtModule extends JavaModule {
.worker()
.reformat(
filesToFormat(sources()),
- scalafmtConfig().head,
- scalafmtDeps().map(_.path)
+ scalafmtConfig().head
)
}
- def scalafmtVersion: T[String] = "1.5.1"
-
def scalafmtConfig: Sources = T.sources(os.pwd / ".scalafmt.conf")
- def scalafmtDeps: T[Agg[PathRef]] = T {
- Lib.resolveDependencies(
- zincWorker.repositories,
- Lib.depToDependency(_, "2.12.4"),
- Seq(ivy"com.geirsson::scalafmt-cli:${scalafmtVersion()}")
- )
- }
-
protected def filesToFormat(sources: Seq[PathRef]) = {
for {
pathRef <- sources if os.exists(pathRef.path)
@@ -46,8 +35,7 @@ object ScalafmtModule extends ExternalModule with ScalafmtModule {
.worker()
.reformat(
files,
- scalafmtConfig().head,
- scalafmtDeps().map(_.path)
+ scalafmtConfig().head
)
}
diff --git a/scalalib/src/scalafmt/ScalafmtWorker.scala b/scalalib/src/scalafmt/ScalafmtWorker.scala
index 47d8375f..f9c7e9b4 100644
--- a/scalalib/src/scalafmt/ScalafmtWorker.scala
+++ b/scalalib/src/scalafmt/ScalafmtWorker.scala
@@ -1,9 +1,11 @@
package mill.scalalib.scalafmt
+import java.nio.file.{Paths => JPaths}
+
import mill._
import mill.define.{Discover, ExternalModule, Worker}
-import mill.modules.Jvm
import mill.api.Ctx
+import org.scalafmt.interfaces.Scalafmt
import scala.collection.mutable
@@ -18,8 +20,7 @@ private[scalafmt] class ScalafmtWorker {
private var configSig: Int = 0
def reformat(input: Seq[PathRef],
- scalafmtConfig: PathRef,
- scalafmtClasspath: Agg[os.Path])(implicit ctx: Ctx): Unit = {
+ scalafmtConfig: PathRef)(implicit ctx: Ctx): Unit = {
val toFormat =
if (scalafmtConfig.sig != configSig) input
else
@@ -28,8 +29,7 @@ private[scalafmt] class ScalafmtWorker {
if (toFormat.nonEmpty) {
ctx.log.info(s"Formatting ${toFormat.size} Scala sources")
reformatAction(toFormat.map(_.path),
- scalafmtConfig.path,
- scalafmtClasspath)
+ scalafmtConfig.path)
reformatted ++= toFormat.map { ref =>
val updRef = PathRef(ref.path)
updRef.path -> updRef.sig
@@ -43,15 +43,22 @@ private[scalafmt] class ScalafmtWorker {
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
- )
- }
+ 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)
+ }
+ }
}