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

AbsoluteFile: move some methods from FileOps #2859

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.scalafmt.cli

import java.io.OutputStreamWriter

import org.scalafmt.util.{AbsoluteFile, FileOps}
import org.scalafmt.util.AbsoluteFile

trait ScalafmtRunner {
private[cli] def run(
Expand Down Expand Up @@ -65,7 +65,7 @@ trait ScalafmtRunner {
filesWithFetch(
options.files,
if (m == GitFiles) options.gitOps.lsTree
else FileOps.listFiles
else _.listFiles
)

case DiffFiles(branch) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.scalafmt.util
import java.io.File
import java.nio.file.Path

import scala.io.Codec

/** Wrapper around java.io.File with an absolute path. */
sealed abstract case class AbsoluteFile(jfile: File) {
def path: String = jfile.getPath
Expand All @@ -18,6 +20,11 @@ sealed abstract case class AbsoluteFile(jfile: File) {
@inline
def isRegularFile: Boolean = FileOps.isRegularFile(asPath)

@inline def listFiles: Seq[AbsoluteFile] = FileOps.listFiles(jfile).map(/)
@inline def readFile(implicit codec: Codec): String = FileOps.readFile(asPath)
@inline def writeFile(content: String)(implicit codec: Codec): Unit =
FileOps.writeFile(asPath, content)

override def toString(): String = path
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ object FileOps {
listFiles(new File(path))
}

def listFiles(file: AbsoluteFile): Vector[AbsoluteFile] = {
listFiles(file.jfile).map(file./)
}

def listFiles(file: File): Vector[String] = {
if (file.isFile) {
Vector(file.getAbsolutePath)
Expand Down Expand Up @@ -50,23 +46,13 @@ object FileOps {
}
}

def readFile(file: AbsoluteFile)(implicit codec: Codec): String = {
readFile(file.asPath)
}

def readFile(file: Path)(implicit codec: Codec): String = {
new String(Files.readAllBytes(file), codec.charSet)
}

def getFile(path: String*): Path =
Paths.get(path.head, path.tail: _*)

def writeFile(file: AbsoluteFile, content: String)(implicit
codec: Codec
): Unit = {
writeFile(file.asPath, content)
}

def writeFile(path: Path, content: String)(implicit codec: Codec): Unit = {
val bytes = content.getBytes(codec.charSet)
Files.write(path, bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.meta.testkit._
import org.scalafmt.CompatCollections.ParConverters._
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.util.AbsoluteFile
import org.scalafmt.util.FileOps
import org.scalafmt.util.FormatAssertions
import org.scalameta.logger
import munit.FunSuite
Expand Down Expand Up @@ -102,10 +101,7 @@ class ScalafmtProps extends FunSuite with FormatAssertions {
logger.elem(summary)
logger.elem(report)
logger.elem(summary)
FileOps.writeFile(
AbsoluteFile.userDir / "target" / "scalafmt-props.md",
report
)
(AbsoluteFile.userDir / "target" / "scalafmt-props.md").writeFile(report)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ trait CliTestBehavior { this: AbstractCliTest =>
}
val config = Cli.getConfig(Array("foo.scala"), options).get
Cli.run(config)
val obtained = FileOps.readFile(workingDir / "foo.scala")
val obtained = (workingDir / "foo.scala").readFile
assertNoDiff(obtained, expected)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.scalafmt.cli

import org.scalafmt.util.AbsoluteFile
import org.scalafmt.util.FileOps
import org.scalafmt.util.GitOps

class FakeGitOps(root: AbsoluteFile) extends GitOps {
override def lsTree(dir: AbsoluteFile): Vector[AbsoluteFile] =
FileOps.listFiles(dir)
override def lsTree(dir: AbsoluteFile): Seq[AbsoluteFile] = dir.listFiles
override def rootDir: Option[AbsoluteFile] = Some(root)
override def status: Seq[AbsoluteFile] = lsTree(root)
override def diff(
Expand Down