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

Config: use nio.Path instead of io.File #2851

Merged
merged 1 commit into from
Nov 6, 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
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.Paths

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 = Paths.get(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