From fe334907b39f63c6a010466f5b1f43fcee156756 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 30 Aug 2009 20:42:17 +0000 Subject: Large Path/File/Directory checkin. of the functionality that one can straightforwardly obtain from the capabilities of java.io.File in java 5, but written with an eye on the significantly more capable (if not significantly more appealing) nio2 API in openjdk. --- src/compiler/scala/tools/nsc/CompileServer.scala | 7 ++- src/compiler/scala/tools/nsc/CompileSocket.scala | 16 +++---- src/compiler/scala/tools/nsc/Interpreter.scala | 9 ---- src/compiler/scala/tools/nsc/ScriptRunner.scala | 58 ++++++++++++------------ 4 files changed, 38 insertions(+), 52 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/CompileServer.scala b/src/compiler/scala/tools/nsc/CompileServer.scala index 7cd4be214a..82d2eb04b9 100644 --- a/src/compiler/scala/tools/nsc/CompileServer.scala +++ b/src/compiler/scala/tools/nsc/CompileServer.scala @@ -55,7 +55,7 @@ class StandardCompileServer extends SocketServer } override def timeout() { - if (!compileSocket.portFile(port).file.exists()) + if (!compileSocket.portFile(port).exists) fatal("port file no longer exists; skipping cleanup") } @@ -142,11 +142,10 @@ class StandardCompileServer extends SocketServer } /** A directory holding redirected output */ - private val redirectDir = File(compileSocket.tmpDir) / "output-redirects" - redirectDir.mkdirs + private val redirectDir = (compileSocket.tmpDir / "output-redirects").createDirectory private def redirect(setter: PrintStream => Unit, filename: String): Unit = - setter(new PrintStream(redirectDir / filename bufferedOutput())) + setter(new PrintStream((redirectDir / filename).createFile.bufferedOutput())) def main(args: Array[String]) { redirect(System.setOut, "scala-compile-server-out.log") diff --git a/src/compiler/scala/tools/nsc/CompileSocket.scala b/src/compiler/scala/tools/nsc/CompileSocket.scala index fb4d4c50dc..eeec1e7bf4 100644 --- a/src/compiler/scala/tools/nsc/CompileSocket.scala +++ b/src/compiler/scala/tools/nsc/CompileSocket.scala @@ -12,7 +12,7 @@ import java.util.regex.Pattern import java.net._ import java.security.SecureRandom -import scala.io.File +import scala.io.{ File, Path } import scala.util.control.Exception.catching // class CompileChannel { } @@ -59,8 +59,7 @@ class CompileSocket { /** A temporary directory to use */ val tmpDir = { val udir = Option(Properties.userName) getOrElse "shared" - val f = (File(Properties.tmpDir) / "scala-devel" / udir).file - f.mkdirs() + val f = (Path(Properties.tmpDir) / "scala-devel" / udir).createDirectory() if (f.isDirectory && f.canWrite) { info("[Temp directory: " + f + "]") @@ -70,8 +69,7 @@ class CompileSocket { } /* A directory holding port identification files */ - val portsDir = File(tmpDir) / dirName - portsDir.mkdirs + val portsDir = (tmpDir / dirName).createDirectory /** Maximum number of polls for an available port */ private val MaxAttempts = 100 @@ -103,11 +101,11 @@ class CompileSocket { } /** The port identification file */ - def portFile(port: Int) = portsDir / port.toString + def portFile(port: Int) = portsDir / File(port.toString) /** Poll for a server port number; return -1 if none exists yet */ private def pollPort(): Int = - portsDir.iterator.toList match { + portsDir.list.toList match { case Nil => -1 case p :: xs => xs forall (_.delete()) @@ -141,7 +139,7 @@ class CompileSocket { try file writeAll List(secret) catch { case e @ (_: FileNotFoundException | _: SecurityException) => - fatal("Cannot create file: %s".format(file.absolutePath)) + fatal("Cannot create file: %s".format(file.path)) } } @@ -214,7 +212,7 @@ class CompileSocket { // allow some time for the server to start up def check = { Thread sleep 100 - ff.file.length() + ff.length } if (Iterator continually check take 50 find (_ > 0) isEmpty) { ff.delete() diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala index 3f44bcdf9b..d34c4a8640 100644 --- a/src/compiler/scala/tools/nsc/Interpreter.scala +++ b/src/compiler/scala/tools/nsc/Interpreter.scala @@ -957,15 +957,6 @@ object Interpreter { intLoop.closeInterpreter } - /** Delete a directory tree recursively. Use with care! */ - private[nsc] def deleteRecursively(path: File): Unit = - if (path.exists) { - if (path.isDirectory) - path.listFiles foreach deleteRecursively - - path.delete - } - /** Heuristically strip interpreter wrapper prefixes * from an interpreter output string. */ diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala index 0a7b0aeeec..f99ea24031 100644 --- a/src/compiler/scala/tools/nsc/ScriptRunner.scala +++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala @@ -12,7 +12,7 @@ import java.io.{ FileReader, InputStreamReader, PrintWriter, FileWriter, IOException } -import scala.io.File +import scala.io.{ Directory, File, Path } // import scala.io.arm.ManagedResource import java.io.{ File => JFile } import java.lang.reflect.InvocationTargetException @@ -71,12 +71,12 @@ object ScriptRunner } /** Choose a jar filename to hold the compiled version of a script. */ - private def jarFileFor(scriptFile: String): JFile = { + private def jarFileFor(scriptFile: String): File = { val name = if (scriptFile endsWith ".jar") scriptFile else scriptFile + ".jar" - File(name).file + File(name) } def copyStreams(in: InputStream, out: OutputStream) = { @@ -93,22 +93,22 @@ object ScriptRunner /** Try to create a jar file out of all the contents * of the directory sourcePath. */ - private def tryMakeJar(jarFile: JFile, sourcePath: JFile) = { - def addFromDir(jar: JarOutputStream, dir: JFile, prefix: String) { - def addFileToJar(entry: JFile) = { - jar putNextEntry new JarEntry(prefix + entry.getName) - copyStreams(new FileInputStream(entry), jar) + private def tryMakeJar(jarFile: File, sourcePath: Directory) = { + def addFromDir(jar: JarOutputStream, dir: Directory, prefix: String) { + def addFileToJar(entry: File) = { + jar putNextEntry new JarEntry(prefix + entry.name) + copyStreams(entry.inputStream, jar) jar.closeEntry } - dir.listFiles foreach { entry => - if (entry.isFile) addFileToJar(entry) - else addFromDir(jar, entry, prefix + entry.getName + "/") + dir.list foreach { entry => + if (entry.isFile) addFileToJar(entry.toFile) + else addFromDir(jar, entry.toDirectory, prefix + entry.name + "/") } } try { - val jar = new JarOutputStream(File(jarFile).outputStream()) + val jar = new JarOutputStream(jarFile.outputStream()) addFromDir(jar, sourcePath, "") jar.close } @@ -118,7 +118,7 @@ object ScriptRunner } /** Read the entire contents of a file as a String. */ - private def contentsOfFile(filename: String) = File(filename).toSource().mkString + private def contentsOfFile(filename: String) = File(filename).slurp() /** Find the length of the header in the specified file, if * there is one. The header part starts with "#!" or "::#!" @@ -249,16 +249,14 @@ object ScriptRunner scriptFile: String) (handler: String => Boolean): Boolean = { - import Interpreter.deleteRecursively - /** Compiles the script file, and returns the directory with the compiled * class files, if the compilation succeeded. */ - def compile: Option[JFile] = { - val compiledPath = File tempdir "scalascript" + def compile: Option[Directory] = { + val compiledPath = Directory makeTemp "scalascript" // delete the directory after the user code has finished - addShutdownHook(deleteRecursively(compiledPath.file)) + addShutdownHook(compiledPath.deleteRecursively()) settings.outdir.value = compiledPath.path @@ -269,37 +267,37 @@ object ScriptRunner val wrapped = wrappedScript(scriptMain(settings), scriptFile, compiler getSourceFile _) cr compileSources List(wrapped) - if (reporter.hasErrors) None else Some(compiledPath.file) + if (reporter.hasErrors) None else Some(compiledPath) } - else if (compileWithDaemon(settings, scriptFile)) Some(compiledPath.file) + else if (compileWithDaemon(settings, scriptFile)) Some(compiledPath) else None } if (settings.savecompiled.value) { - val jarFile = File(jarFileFor(scriptFile)) + val jarFile = jarFileFor(scriptFile) def jarOK = jarFile.canRead && (jarFile isFresher File(scriptFile)) def recompile() = { - jarFile.delete + jarFile.delete() compile match { case Some(compiledPath) => - tryMakeJar(jarFile.file, compiledPath) + tryMakeJar(jarFile, compiledPath) if (jarOK) { - deleteRecursively(compiledPath) - handler(jarFile.absolutePath) + compiledPath.deleteRecursively() + handler(jarFile.toAbsolute.path) } // jar failed; run directly from the class files - else handler(compiledPath.getPath) + else handler(compiledPath.path) case _ => false } } - if (jarOK) handler(jarFile.absolutePath) // pre-compiled jar is current - else recompile() // jar old - recompile the script. + if (jarOK) handler(jarFile.toAbsolute.path) // pre-compiled jar is current + else recompile() // jar old - recompile the script. } // don't use a cache jar at all--just use the class files - else compile map (cp => handler(cp.getPath)) getOrElse false + else compile map (cp => handler(cp.path)) getOrElse false } /** Run a script after it has been compiled @@ -368,7 +366,7 @@ object ScriptRunner command: String, scriptArgs: List[String]) : Boolean = { - val scriptFile = File.tempfile("scalacmd", ".scala") + val scriptFile = File.makeTemp("scalacmd", ".scala") // save the command to the file scriptFile writeAll List(command) -- cgit v1.2.3