All Downloads are FREE. Search and download functionalities are using the official Maven repository.

overflowdb.codegen.Formatter.scala Maven / Gradle / Ivy

There is a newer version: 2.110
Show newest version
package overflowdb.codegen

import better.files._
import java.nio.file.{Files, Path}
import org.scalafmt.interfaces.Scalafmt

object Formatter {
  val defaultScalafmtConfig = """
      |version=3.8.1
      |runner.dialect=scala3
      |align.preset=some
      |maxColumn=120
      |""".stripMargin

  def run(sourceFiles: Seq[File], scalafmtConfig: Option[File]): Unit = {
    println(s"invoking scalafmt on ${sourceFiles.size} files")
    val configFile: File = scalafmtConfig.getOrElse(
      Files
        .createTempFile("overflowdb-scalafmt", ".conf")
        .toFile
        .toScala
        .write(defaultScalafmtConfig)
    )
    
    val scalafmt = Scalafmt.create(getClass.getClassLoader)
    val scalafmtSession = scalafmt.createSession(configFile.path)

    sourceFiles.foreach { file =>
      val originalSource = file.lines.mkString("\n")
      val formattedSource = scalafmtSession.format(file.path, originalSource)
      file.writeText(formattedSource)
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy