Reformat directories recursively

This commit is contained in:
Joscha 2024-09-04 19:20:26 +02:00
parent 4b0a88447b
commit b1bf201a92

View file

@ -4,6 +4,7 @@ import de.plugh.asciiprooftree.file.Formatter
import org.rogach.scallop.* import org.rogach.scallop.*
import java.nio.file.{Files, Path} import java.nio.file.{Files, Path}
import scala.jdk.StreamConverters.*
class Conf(args: Seq[String]) extends ScallopConf(args): class Conf(args: Seq[String]) extends ScallopConf(args):
val path: ScallopOption[Path] = trailArg[Path]() val path: ScallopOption[Path] = trailArg[Path]()
@ -13,6 +14,15 @@ class Conf(args: Seq[String]) extends ScallopConf(args):
@main @main
def main(args: String*): Unit = def main(args: String*): Unit =
val conf = new Conf(args) val conf = new Conf(args)
val text = Files.readString(conf.path()) reformat(conf.path(), conf.marker())
val newText = Formatter.reformat(text, marker = conf.marker())
Files.writeString(conf.path(), newText) def reformat(path: Path, marker: String): Unit =
if Files.isDirectory(path) then
val files = Files.list(path).toScala(Seq)
for file <- files do reformat(file, marker)
else if Files.isRegularFile(path) then
val text = Files.readString(path)
val newText = Formatter.reformat(text, marker = marker)
if text != newText then
println(path)
Files.writeString(path, newText)