diff options
| author | sinofp | 2021-06-04 13:29:47 +0800 |
|---|---|---|
| committer | sinofp | 2021-06-04 13:29:47 +0800 |
| commit | f0b381eb5bb545455aeb295ec9d71bbb59c955a9 (patch) | |
| tree | 44662602bd60d70fcf17d9b92ccf9c2bdfc3aac6 | |
| parent | 62fdb87e0897e582bbbec1e29ee4598f40343d09 (diff) | |
Rm java.io in FileUtils
| -rw-r--r-- | src/main/scala/firrtl/FileUtils.scala | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/src/main/scala/firrtl/FileUtils.scala b/src/main/scala/firrtl/FileUtils.scala index f92d50cc..3668b374 100644 --- a/src/main/scala/firrtl/FileUtils.scala +++ b/src/main/scala/firrtl/FileUtils.scala @@ -2,8 +2,6 @@ package firrtl -import java.io.File - import firrtl.options.StageUtils import scala.collection.Seq @@ -16,11 +14,12 @@ object FileUtils { * @return true if the directory exists or if it was successfully created */ def makeDirectory(directoryName: String): Boolean = { - val dirFile = new File(directoryName) - if (dirFile.exists()) { - dirFile.isDirectory + val dirPath = getPath(directoryName) + if (os.exists(dirPath)) { + os.isDir(dirPath) } else { - dirFile.mkdirs() + os.makeDir.all(dirPath) + true } } @@ -31,16 +30,26 @@ object FileUtils { * @param directoryPathName a directory hierarchy to delete */ def deleteDirectoryHierarchy(directoryPathName: String): Boolean = { - deleteDirectoryHierarchy(new File(directoryPathName)) + os.FilePath(directoryPathName) match { + case path: os.Path => + StageUtils.dramaticError(s"delete directory $path will not delete absolute paths") + false + case rel: os.RelPath => + val path = os.pwd / rel + os.exists(path) && { os.remove.all(path); true } + case sub: os.SubPath => + val path = os.pwd / sub + os.exists(path) && { os.remove.all(path); true } + } } /** * recursively delete all directories in a relative path * DO NOT DELETE absolute paths - * + * @todo deprecate java.io.File * @param file: a directory hierarchy to delete */ - def deleteDirectoryHierarchy(file: File, atTop: Boolean = true): Boolean = { + def deleteDirectoryHierarchy(file: java.io.File, atTop: Boolean = true): Boolean = { if ( file.getPath.split("/").last.isEmpty || file.getAbsolutePath == "/" || @@ -99,14 +108,20 @@ object FileUtils { * * @param fileName The file to read */ - def getLines(fileName: String): Seq[String] = getLines(new File(fileName)) + def getLines(fileName: String): Seq[String] = getLines(getPath(fileName)) /** Read a text file and return it as a Seq of strings * Closes the file after read to avoid dangling file handles - * + * @param file an os.Path to be read + */ + def getLines(file: os.Path): Seq[String] = os.read.lines(file) + + /** Read a text file and return it as a Seq of strings + * Closes the file after read to avoid dangling file handles + * @todo deprecate java.io.File * @param file a java File to be read */ - def getLines(file: File): Seq[String] = { + def getLines(file: java.io.File): Seq[String] = { val source = scala.io.Source.fromFile(file) val lines = source.getLines().toList source.close() @@ -118,14 +133,21 @@ object FileUtils { * * @param fileName The file to read */ - def getText(fileName: String): String = getText(new File(fileName)) + def getText(fileName: String): String = getText(getPath(fileName)) /** Read a text file and return it as a single string * Closes the file after read to avoid dangling file handles * + * @param file an os.Path to be read + */ + def getText(file: os.Path): String = os.read(file) + + /** Read a text file and return it as a single string + * Closes the file after read to avoid dangling file handles + * @todo deprecate java.io.File * @param file a java File to be read */ - def getText(file: File): String = { + def getText(file: java.io.File): String = { val source = scala.io.Source.fromFile(file) val text = source.mkString source.close() @@ -159,4 +181,13 @@ object FileUtils { inputStream.close() text } + + /** Get os.Path from String + * @param pathName an absolute or relative path string + */ + def getPath(pathName: String): os.Path = os.FilePath(pathName) match { + case path: os.Path => path + case sub: os.SubPath => os.pwd / sub + case rel: os.RelPath => os.pwd / rel + } } |
