diff options
| author | Jiuyang Liu | 2021-06-08 02:55:03 +0800 |
|---|---|---|
| committer | GitHub | 2021-06-08 02:55:03 +0800 |
| commit | eec65c8ac658783ab599218818e907285619c7ad (patch) | |
| tree | 0b66dc45744592af9f4297ab22cb2ca6893b92d0 /src | |
| parent | 8053514d051eb1d8a37099144948e9e1a2806115 (diff) | |
| parent | e769d0e4861b4c17bd6c97e717e440b479e26120 (diff) | |
Merge pull request #2256 from sinofp/nomorejavaio-fileutils
Remove java.io in FileUtils
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/firrtl/FileUtils.scala | 58 |
1 files changed, 46 insertions, 12 deletions
diff --git a/src/main/scala/firrtl/FileUtils.scala b/src/main/scala/firrtl/FileUtils.scala index f92d50cc..5f825a38 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,7 +30,17 @@ 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 } + } } /** @@ -40,7 +49,8 @@ object FileUtils { * * @param file: a directory hierarchy to delete */ - def deleteDirectoryHierarchy(file: File, atTop: Boolean = true): Boolean = { + @deprecated("Use os-lib instead, this function will be removed in FIRRTL 1.6", "FIRRTL 1.5") + def deleteDirectoryHierarchy(file: java.io.File, atTop: Boolean = true): Boolean = { if ( file.getPath.split("/").last.isEmpty || file.getAbsolutePath == "/" || @@ -99,14 +109,21 @@ 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 * * @param file a java File to be read */ - def getLines(file: File): Seq[String] = { + @deprecated("Use os-lib instead, this function will be removed in FIRRTL 1.6", "FIRRTL 1.5") + def getLines(file: java.io.File): Seq[String] = { val source = scala.io.Source.fromFile(file) val lines = source.getLines().toList source.close() @@ -118,14 +135,22 @@ 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 * * @param file a java File to be read */ - def getText(file: File): String = { + @deprecated("Use os-lib instead, this function will be removed in FIRRTL 1.6", "FIRRTL 1.5") + def getText(file: java.io.File): String = { val source = scala.io.Source.fromFile(file) val text = source.mkString source.close() @@ -159,4 +184,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 + } } |
