aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/dotty/tools/io/Fileish.scala
blob: 0fcb133075b26cac701fb68ed058baca04e58aa1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* NSC -- new Scala compiler
 * Copyright 2005-2012 LAMP/EPFL
 * @author Paul Phillips
 */

package dotty.tools
package io

import java.io.{ InputStream }
import java.util.jar.JarEntry
import language.postfixOps

/** A common interface for File-based things and Stream-based things.
 *  (In particular, io.File and JarEntry.)
 */
class Fileish(val path: Path, val input: () => InputStream) extends Streamable.Chars {
  def inputStream() = input()

  def parent       = path.parent
  def name         = path.name
  def isSourceFile = path.hasExtension("java", "scala")

  private lazy val pkgLines = lines() collect { case x if x startsWith "package " => x stripPrefix "package" trim }
  lazy val pkgFromPath      = parent.path.replaceAll("""[/\\]""", ".")
  lazy val pkgFromSource    = pkgLines map (_ stripSuffix ";") mkString "."

  override def toString = path.path
}

object Fileish {
  def apply(f: File): Fileish = new Fileish(f, () => f.inputStream())
  def apply(f: JarEntry, in: () => InputStream): Fileish  = new Fileish(Path(f.getName), in)
  def apply(path: String, in: () => InputStream): Fileish = new Fileish(Path(path), in)
}