diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2019-05-19 11:23:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 11:23:51 +0800 |
commit | 1cb439fce4af7a61166f13a3c5045fa7c73b25de (patch) | |
tree | 82f7d64e787e2871e973d895f20bfc0fe95df0f0 /scalalib/src/scalafmt | |
parent | 64f477e36c33afe8dfd87a839ba263e9973b0669 (diff) | |
parent | 052af24a530f8bc0532b368a2d360ff30f67d7b8 (diff) | |
download | mill-1cb439fce4af7a61166f13a3c5045fa7c73b25de.tar.gz mill-1cb439fce4af7a61166f13a3c5045fa7c73b25de.tar.bz2 mill-1cb439fce4af7a61166f13a3c5045fa7c73b25de.zip |
Merge branch 'master' into docker
Diffstat (limited to 'scalalib/src/scalafmt')
-rw-r--r-- | scalalib/src/scalafmt/ScalafmtModule.scala | 16 | ||||
-rw-r--r-- | scalalib/src/scalafmt/ScalafmtWorker.scala | 37 |
2 files changed, 24 insertions, 29 deletions
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) + } + } } |