diff --git a/.scalafmt.conf b/.scalafmt.conf index b06f43c0c..8bfd2c774 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,4 +1,4 @@ -version = "3.0.7" +version = "3.1.2" runner.dialect = scala213 maxColumn = 120 diff --git a/build.sbt b/build.sbt index 287d3c75f..44d5be0fb 100644 --- a/build.sbt +++ b/build.sbt @@ -147,8 +147,8 @@ lazy val tools = project crossScalaVersions -= scala3, testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")), libraryDependencies ++= Seq( - "org.scalameta" %% "scalafmt-dynamic" % "3.0.7", - "org.scalameta" %% "scalafmt-core" % "3.0.7", + "org.scalameta" %% "scalafmt-dynamic" % "3.1.2", + "org.scalameta" %% "scalafmt-core" % "3.1.2", "com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % sttpVersion, "dev.zio" %% "zio-config" % zioConfigVersion, "dev.zio" %% "zio-config-magnolia" % zioConfigVersion, diff --git a/tools/src/main/resources/default.scalafmt.conf b/tools/src/main/resources/default.scalafmt.conf new file mode 100644 index 000000000..2881b7595 --- /dev/null +++ b/tools/src/main/resources/default.scalafmt.conf @@ -0,0 +1 @@ +version=2.7.5 \ No newline at end of file diff --git a/tools/src/main/scala/caliban/tools/Formatter.scala b/tools/src/main/scala/caliban/tools/Formatter.scala index 44fecee21..5acde7550 100644 --- a/tools/src/main/scala/caliban/tools/Formatter.scala +++ b/tools/src/main/scala/caliban/tools/Formatter.scala @@ -5,7 +5,8 @@ import org.scalafmt.interfaces.Scalafmt import zio.RIO import zio.blocking.{ effectBlocking, Blocking } -import java.nio.file.{ Files, Path, Paths } +import java.nio.file.{ Files, Path, Paths, StandardCopyOption } +import java.util.jar.JarFile object Formatter { @@ -16,7 +17,23 @@ object Formatter { effectBlocking { val config: Path = { @inline def defaultConfigPath = Paths.get(".scalafmt.conf") - @inline def defaultConfig = if (Files.exists(defaultConfigPath)) defaultConfigPath else Paths.get("") + @inline def defaultConfig = + if (Files.exists(defaultConfigPath)) defaultConfigPath + else { + val defaultScalafmtCalibanToolsFile = "default.scalafmt.conf" + val uri = this.getClass.getClassLoader.getResource(defaultScalafmtCalibanToolsFile).toURI + uri.getScheme match { + case "file" => Paths.get(uri) + case "jar" => + // scalafmt can't access a file inside a JAR so we'll copy the content into a temp file + val jar = new JarFile(this.getClass.getProtectionDomain.getCodeSource.getLocation.toURI.getPath) + val file = Files.createTempFile(null, null) + val scalafmtConfig = jar.getInputStream(jar.getEntry(defaultScalafmtCalibanToolsFile)) + Files.copy(scalafmtConfig, file, StandardCopyOption.REPLACE_EXISTING) + file + case _ => Paths.get("") + } + } fmtPath.fold(defaultConfig)(Paths.get(_)) } @@ -24,8 +41,6 @@ object Formatter { val scalafmt = Scalafmt .create(this.getClass.getClassLoader) - .withRespectVersion(false) - .withDefaultVersion("2.7.5") // For retro-compatibility .withReporter(new ConsoleScalafmtReporter(System.out)) // defaults prints everything to System.err val result = strs.map { case (name, code) => name -> scalafmt.format(config, Paths.get(s"$name.scala"), code) }