From 055190a38ba1496821b2b67c368c97e33573883a Mon Sep 17 00:00:00 2001 From: dcaoyuan Date: Thu, 19 Nov 2009 08:24:37 +0000 Subject: Path.parent now returns Path instead of Option[... Path.parent now returns Path instead of Option[Path], and it prefers relative path. --- src/compiler/scala/tools/nsc/Global.scala | 4 ++-- src/compiler/scala/tools/nsc/io/File.scala | 2 +- src/compiler/scala/tools/nsc/io/Path.scala | 30 ++++++++++++++++++------- src/compiler/scala/tools/nsc/io/PlainFile.scala | 2 +- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index ffe3b861da..09d900ce78 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -230,7 +230,7 @@ class Global(var settings: Settings, var reporter: Reporter) extends SymbolTable val depFilePath = Path(x) if (depFilePath.exists) { /** The directory where file lookup should start */ - val rootPath = depFilePath.normalize.parent.get.normalize + val rootPath = depFilePath.parent def toFile(path: String) = AbstractFile.getFile(rootPath resolve Path(path)) dependencyAnalysis.loadFrom(AbstractFile.getFile(depFilePath), toFile) } @@ -833,7 +833,7 @@ class Global(var settings: Settings, var reporter: Reporter) extends SymbolTable dependencyAnalysis.dependenciesFile = AbstractFile.getFile(depFilePath.createFile()) /** The directory where file lookup should start */ - val rootPath = depFilePath.normalize.parent.get.normalize + val rootPath = depFilePath.parent.normalize def fromFile(file: AbstractFile): String = rootPath.relativize(Path(file.file).normalize).path diff --git a/src/compiler/scala/tools/nsc/io/File.scala b/src/compiler/scala/tools/nsc/io/File.scala index 294139ba44..1fbe384bfa 100644 --- a/src/compiler/scala/tools/nsc/io/File.scala +++ b/src/compiler/scala/tools/nsc/io/File.scala @@ -90,7 +90,7 @@ with Streamable.Chars val dest = destPath.toFile if (!isValid) fail("Source %s is not a valid file." format name) if (this.normalize == dest.normalize) fail("Source and destination are the same.") - if (!dest.parent.map(_.exists).getOrElse(false)) fail("Destination cannot be created.") + if (!dest.parent.exists) fail("Destination cannot be created.") if (dest.exists && !dest.canWrite) fail("Destination exists but is not writable.") if (dest.isDirectory) fail("Destination exists but is a directory.") diff --git a/src/compiler/scala/tools/nsc/io/Path.scala b/src/compiler/scala/tools/nsc/io/Path.scala index df1736f688..64a313b00f 100644 --- a/src/compiler/scala/tools/nsc/io/Path.scala +++ b/src/compiler/scala/tools/nsc/io/Path.scala @@ -84,7 +84,7 @@ class Path private[io] (val jfile: JFile) /** Creates a new Path with the specified path appended. Assumes * the type of the new component implies the type of the result. */ - def /(child: Path): Path = new Path(new JFile(jfile, child.path)) + def /(child: Path): Path = if (isEmpty) child else new Path(new JFile(jfile, child.path)) def /(child: Directory): Directory = /(child: Path).toDirectory def /(child: File): File = /(child: Path).toFile @@ -110,10 +110,24 @@ class Path private[io] (val jfile: JFile) // derived from identity def root: Option[Path] = roots find (this startsWith _) def segments: List[String] = (path split separator).toList filterNot (_.length == 0) - def parent: Option[Path] = Option(jfile.getParent()) map Path.apply - def parents: List[Path] = parent match { - case None => Nil - case Some(p) => p :: p.parents + /** + * @return The path of the parent directory, or root if path is already root + */ + def parent: Path = { + val p = path match { + case "" | "." => ".." + case _ if path endsWith ".." => path + separator + ".." // the only solution + case _ => jfile.getParent match { + case null if isAbsolute => path // it should be a root. BTW, don't need to worry about relative pathed root + case null => "." // a file ot dir under pwd + case x => x + } + } + new Directory(new JFile(p)) + } + def parents: List[Path] = { + val p = parent + if (p isSame this) Nil else p :: p.parents } // if name ends with an extension (e.g. "foo.jpg") returns the extension ("jpg"), otherwise "" def extension: String = (name lastIndexOf '.') match { @@ -131,8 +145,8 @@ class Path private[io] (val jfile: JFile) def isDirectory = jfile.isDirectory() def isAbsolute = jfile.isAbsolute() def isHidden = jfile.isHidden() - def isSymlink = parent.isDefined && { - val x = parent.get / name + def isSymlink = { + val x = parent / name x.normalize != x.toAbsolute } def isEmpty = path.length == 0 @@ -145,7 +159,7 @@ class Path private[io] (val jfile: JFile) // Boolean path comparisons def endsWith(other: Path) = segments endsWith other.segments def startsWith(other: Path) = segments startsWith other.segments - def isSame(other: Path) = toAbsolute == other.toAbsolute + def isSame(other: Path) = normalize == other.normalize def isFresher(other: Path) = lastModified > other.lastModified // creations diff --git a/src/compiler/scala/tools/nsc/io/PlainFile.scala b/src/compiler/scala/tools/nsc/io/PlainFile.scala index 926f5ee042..ef10b6d6bd 100644 --- a/src/compiler/scala/tools/nsc/io/PlainFile.scala +++ b/src/compiler/scala/tools/nsc/io/PlainFile.scala @@ -38,7 +38,7 @@ class PlainFile(val givenPath: Path) extends AbstractFile { /** The absolute file. */ def absolute = new PlainFile(givenPath.normalize) - override def container: AbstractFile = new PlainFile(givenPath.parent.get) + override def container: AbstractFile = new PlainFile(givenPath.parent) override def input = givenPath.toFile.inputStream() override def output = givenPath.toFile.outputStream() override def sizeOption = Some(givenPath.length.toInt) -- cgit v1.2.3