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

chore: update scalafmt #1174

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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
AlixBa marked this conversation as resolved.
Show resolved Hide resolved
.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