Skip to content

Commit

Permalink
Config: use nio.Path instead of io.File
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 6, 2021
1 parent d524bf2 commit bc8aec5
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
10 changes: 5 additions & 5 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ object CliOptions {
)(candidate: PrintStream): PrintStream =
if (p) NoopOutputStream.printStream else candidate

private def tryGetConfigFile(dir: AbsoluteFile): Option[File] = {
val file = (dir / ".scalafmt.conf").jfile
if (file.isFile) Some(file) else None
private def tryGetConfigFile(dir: AbsoluteFile): Option[Path] = {
val file = dir / ".scalafmt.conf"
if (file.isRegularFile) Some(file.asPath) else None
}

}
Expand Down Expand Up @@ -133,10 +133,10 @@ case class CliOptions(
configPathOpt.get

private[cli] def configPathOpt: Option[Path] =
tempConfigPath.orElse(canonicalConfigFile.map(_.toPath))
tempConfigPath.orElse(canonicalConfigFile)

private lazy val canonicalConfigFile = config
.map(AbsoluteFile.fromFile(_, common.workingDirectory).jfile)
.map(AbsoluteFile.fromFile(_, common.workingDirectory).asPath)
.orElse(tryGetConfigFile(common.workingDirectory))
.orElse(gitOps.rootDir.flatMap(tryGetConfigFile))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ object Scalafmt {

// used by ScalafmtReflect.parseConfig
def parseHoconConfigFile(configPath: Path): Configured[ScalafmtConfig] =
Config.fromHoconFile(configPath.toFile, ScalafmtConfig.uncheckedDefault)
Config.fromHoconFile(configPath, ScalafmtConfig.uncheckedDefault)

// used by ScalafmtReflect.parseConfig
def parseHoconConfig(configString: String): Configured[ScalafmtConfig] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scalafmt.config

import java.io.File
import java.nio.file.Path

import metaconfig._
import org.scalafmt.config.PlatformConfig._
Expand All @@ -15,7 +15,7 @@ object Config {
def hoconStringToConf(input: String, path: Option[String]): Configured[Conf] =
Input.String(input).parse(path)

def hoconFileToConf(input: File, path: Option[String]): Configured[Conf] =
def hoconFileToConf(input: Path, path: Option[String]): Configured[Conf] =
Configured
.fromExceptionThrowing(Input.File(input))
.andThen(_.parse(path))
Expand All @@ -29,7 +29,7 @@ object Config {

/** Read ScalafmtConfig from String contents from an optional HOCON path. */
def fromHoconFile(
file: File,
file: Path,
default: ScalafmtConfig = ScalafmtConfig.default,
path: Option[String] = None
): Configured[ScalafmtConfig] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package org.scalafmt.util

import java.io.File
import java.nio.file.Path

/** Wrapper around java.io.File with an absolute path. */
sealed abstract case class AbsoluteFile(jfile: File) {
def path: String = jfile.getAbsolutePath
def exists: Boolean = jfile.exists()
def /(other: String) = new AbsoluteFile(new File(jfile, other)) {}

@inline
def asPath: Path = jfile.toPath

@inline
def isRegularFile: Boolean = FileOps.isRegularFile(jfile)

override def toString(): String = path
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ object FileOps {
if (file.isAbsolute) file
else new File(workingDir, file.getPath)

@inline
def getLastModifiedMsec(file: Path): Long =
Files.getLastModifiedTime(file, LinkOption.NOFOLLOW_LINKS).toMillis

@inline
def isRegularFile(file: File): Boolean =
Files.isRegularFile(file.toPath, LinkOption.NOFOLLOW_LINKS)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.scalafmt.util

import scala.collection.mutable

import java.io.File
import java.nio.file.Path

import metaconfig.Configured
import metaconfig.Configured.Ok
Expand All @@ -22,8 +22,8 @@ object StyleCache {
}

def getStyleForFileOrError(filename: String): Configured[ScalafmtConfig] = {
val file = new File(filename)
val lastModified = file.lastModified()
val file = Path.of(filename)
val lastModified = FileOps.getLastModifiedMsec(file)
val configUnchanged = timeStamps.get(filename).contains(lastModified)
timeStamps.update(filename, lastModified)
styleCache.get(filename) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CliOptionsTest extends FunSuite {
)
)
.configPath
val config = Config.fromHoconFile(path.toFile).get
val config = Config.fromHoconFile(path).get
assert(config.onTestFailure == expected)
}

Expand Down

0 comments on commit bc8aec5

Please sign in to comment.