Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support newer scalafmt versions which require a config file #1757

Merged
merged 3 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object Deps {
val sbtTestInterface = ivy"org.scala-sbt:test-interface:1.0"
val scalaCheck = ivy"org.scalacheck::scalacheck:1.15.4"
def scalaCompiler(scalaVersion: String) = ivy"org.scala-lang:scala-compiler:${scalaVersion}"
val scalafmtDynamic = ivy"org.scalameta::scalafmt-dynamic:3.0.8"
val scalafmtDynamic = ivy"org.scalameta::scalafmt-dynamic:3.4.3"
val scalametaTrees = ivy"org.scalameta::trees:4.5.0"
def scalaReflect(scalaVersion: String) = ivy"org.scala-lang:scala-reflect:${scalaVersion}"
def scalacScoveragePlugin = ivy"org.scoverage:::scalac-scoverage-plugin:1.4.11"
Expand Down Expand Up @@ -428,7 +428,8 @@ object scalalib extends MillModule {
Seq(
"-Djna.nosys=true",
"-DMILL_BUILD_LIBRARIES=" + genIdeaArgs.map(_.path).mkString(","),
"-DMILL_SCALA_LIB=" + runClasspath().map(_.path).mkString(",")
"-DMILL_SCALA_LIB=" + runClasspath().map(_.path).mkString(","),
s"-DTEST_SCALAFMT_VERSION=${Deps.scalafmtDynamic.dep.version}"
)
}
object backgroundwrapper extends MillPublishModule {
Expand Down
25 changes: 21 additions & 4 deletions scalalib/src/scalafmt/ScalafmtModule.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mill.scalalib.scalafmt

import mill._
import mill.api.Result
import mill.define._
import mill.scalalib._

Expand All @@ -11,7 +12,7 @@ trait ScalafmtModule extends JavaModule {
.worker()
.reformat(
filesToFormat(sources()),
scalafmtConfig().head
resolvedScalafmtConfig()
)
}

Expand All @@ -20,12 +21,28 @@ trait ScalafmtModule extends JavaModule {
.worker()
.checkFormat(
filesToFormat(sources()),
scalafmtConfig().head
resolvedScalafmtConfig()
)
}

def scalafmtConfig: Sources = T.sources(T.workspace / ".scalafmt.conf")

// TODO: Do we want provide some defaults or write a default file?
private[ScalafmtModule] def resolvedScalafmtConfig: Task[PathRef] = T.task {
val locs = scalafmtConfig()
locs.find(p => os.exists(p.path)) match {
case None => Result.Failure(s"None of the specified `scalafmtConfig` locations exist. Searched in: ${locs.map(_.path).mkString(", ")}")
case Some(c) if (os.read.lines.stream(c.path).find(_.trim.startsWith("version")).isEmpty) =>
Result.Failure(
s"""Found scalafmtConfig file does not specify to scalafmt version to use.
lefou marked this conversation as resolved.
Show resolved Hide resolved
|Please specify the scalafmt version in ${c}
lefou marked this conversation as resolved.
Show resolved Hide resolved
|Example:
|version = "2.4.3"
|""".stripMargin)
case Some(c) => Result.Success(c)
}
}

protected def filesToFormat(sources: Seq[PathRef]) = {
for {
pathRef <- sources if os.exists(pathRef.path)
Expand All @@ -52,7 +69,7 @@ object ScalafmtModule extends ExternalModule with ScalafmtModule {
.worker()
.reformat(
files,
scalafmtConfig().head
resolvedScalafmtConfig()
)
}

Expand All @@ -63,7 +80,7 @@ object ScalafmtModule extends ExternalModule with ScalafmtModule {
.worker()
.checkFormat(
files,
scalafmtConfig().head
resolvedScalafmtConfig()
)
}

Expand Down
29 changes: 1 addition & 28 deletions scalalib/src/scalafmt/ScalafmtWorker.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
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
import mill.api.Result
import java.nio.file.Files
import scala.util.Try
import java.nio.file.Path
import scala.util.Failure
import scala.util.Success

object ScalafmtWorkerModule extends ExternalModule {
def worker: Worker[ScalafmtWorker] = T.worker { new ScalafmtWorker() }
Expand Down Expand Up @@ -66,24 +59,8 @@ private[scalafmt] class ScalafmtWorker {

val scalafmt = Scalafmt
.create(this.getClass.getClassLoader)
.withRespectVersion(false)

def readDefaultScalafmtConfig(): Try[Path] = {
Try(JPaths.get(getClass.getResource("default.scalafmt.conf").toURI))
}

val (configPath, tmpFileCreated) =
if (os.exists(scalafmtConfig.path))
(scalafmtConfig.path.toNIO, false)
else {
readDefaultScalafmtConfig() match {
case Success(defaultConfig) => (defaultConfig, false)
case Failure(exception) => {
ctx.log.debug(s"Read default scalafmt config fail, create a temp one")
(JPaths.get(Files.createTempFile("temp", ".scalafmt.conf").toUri), true)
}
}
}
val configPath = scalafmtConfig.path.toNIO

// keeps track of files that are misformatted
val misformatted = mutable.ListBuffer.empty[PathRef]
Expand All @@ -109,10 +86,6 @@ private[scalafmt] class ScalafmtWorker {

}

if (tmpFileCreated) {
Files.delete(configPath)
}

configSig = scalafmtConfig.sig
misformatted.toList
} else {
Expand Down
8 changes: 8 additions & 0 deletions scalalib/test/src/scalafmt/ScalafmtTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import utest.framework.TestPath

object ScalafmtTests extends TestSuite {

val scalafmtTestVersion = sys.props.getOrElse("TEST_SCALAFMT_VERSION", ???)

trait TestBase extends TestUtil.BaseModule {
def millSourcePath =
TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
Expand Down Expand Up @@ -43,6 +45,12 @@ object ScalafmtTests extends TestSuite {
os.remove.all(eval.outPath)
os.makeDir.all(m.millSourcePath / os.up)
os.copy(resourcePath, m.millSourcePath)
os.write(
m.millSourcePath / ".scalafmt.conf",
s"""version = $scalafmtTestVersion
|runner.dialect = scala213
|""".stripMargin
)
t(eval)
}

Expand Down