Skip to content

Commit

Permalink
chore: update scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixBa committed Nov 25, 2021
1 parent 9efd71b commit 8e6e6de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.0.7"
version = "3.1.2"

runner.dialect = scala213
maxColumn = 120
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tools/src/main/resources/default.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=2.7.5
23 changes: 19 additions & 4 deletions tools/src/main/scala/caliban/tools/Formatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -16,16 +17,30 @@ 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(_))
}

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) }
Expand Down

0 comments on commit 8e6e6de

Please sign in to comment.