Skip to content

Slight refactoring of PathResolver #5756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 28 additions & 39 deletions compiler/src/dotty/tools/dotc/config/PathResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package dotc
package config

import WrappedProperties.AccessControl
import io.{ ClassPath, Directory, Path }
import classpath.{AggregateClassPath, ClassPathFactory, JrtClassPath }
import io.{ClassPath, Directory, Path}
import classpath.{AggregateClassPath, ClassPathFactory, JrtClassPath}
import ClassPath.split
import PartialFunction.condOpt
import scala.language.postfixOps
import core.Contexts._
import Settings._
import dotty.tools.io.File

// Loosely based on the draft specification at:
// https://wiki.scala-lang.org/display/SW/Classpath

object PathResolver {

// Imports property/environment functions which suppress
Expand All @@ -24,10 +21,11 @@ object PathResolver {
def firstNonEmpty(xs: String*): String = xs find (_ != "") getOrElse ""

/** Map all classpath elements to absolute paths and reconstruct the classpath.
*/
*/
def makeAbsolute(cp: String): String = ClassPath.map(cp, x => Path(x).toAbsolute.path)

/** pretty print class path */
/** pretty print class path
*/
def ppcp(s: String): String = split(s) match {
case Nil => ""
case Seq(x) => x
Expand All @@ -53,7 +51,8 @@ object PathResolver {
def scalaHome: String = propOrEmpty("scala.home")
def scalaExtDirs: String = propOrEmpty("scala.ext.dirs")

/** The java classpath and whether to use it. */
/** The java classpath and whether to use it.
*/
def javaUserClassPath: String = propOrElse("java.class.path", "")
def useJavaClassPath: Boolean = propOrFalse("scala.usejavacp")

Expand All @@ -77,14 +76,14 @@ object PathResolver {
def javaExtDirs: String = Environment.javaExtDirs
def useJavaClassPath: Boolean = Environment.useJavaClassPath

def scalaHome: String = Environment.scalaHome
def scalaHomeDir: Directory = Directory(scalaHome)
def scalaHomeExists: Boolean = scalaHomeDir.isDirectory
def scalaLibDir: Directory = (scalaHomeDir / "lib").toDirectory
def scalaClassesDir: Directory = (scalaHomeDir / "classes").toDirectory
def scalaHome: String = Environment.scalaHome
def scalaHomeDir: Directory = Directory(scalaHome)
def scalaHomeExists: Boolean = scalaHomeDir.isDirectory
def scalaLibDir: Directory = (scalaHomeDir / "lib").toDirectory
def scalaClassesDir: Directory = (scalaHomeDir / "classes").toDirectory

def scalaLibAsJar: File = (scalaLibDir / "scala-library.jar").toFile
def scalaLibAsDir: Directory = (scalaClassesDir / "library").toDirectory
def scalaLibAsJar: File = (scalaLibDir / "scala-library.jar").toFile
def scalaLibAsDir: Directory = (scalaClassesDir / "library").toDirectory

def scalaLibDirFound: Option[Directory] =
if (scalaLibAsJar.isFile) Some(scalaLibDir)
Expand Down Expand Up @@ -134,9 +133,9 @@ object PathResolver {
new PathResolver()(ctx.fresh.setSettings(settings)).result
}

/** With no arguments, show the interesting values in Environment and Defaults.
* If there are arguments, show those in Calculated as if those options had been
* given to a scala runner.
/** Show values in Environment and Defaults when no argument is provided.
* Otherwise, show values in Calculated as if those options had been given
* to a scala runner.
*/
def main(args: Array[String]): Unit = {
if (args.isEmpty) {
Expand All @@ -159,19 +158,19 @@ object PathResolver {
}
}
}
import PathResolver.{ Defaults, ppcp }

import PathResolver.{Defaults, ppcp}

class PathResolver(implicit ctx: Context) {
import ctx.base.settings

private val classPathFactory = new ClassPathFactory

private def cmdLineOrElse(name: String, alt: String) = {
(commandLineFor(name) match {
case Some("") => None
case x => x
}) getOrElse alt
}
private def cmdLineOrElse(name: String, alt: String) =
commandLineFor(name) match {
case Some("") | None => alt
case Some(x) => x
}

private def commandLineFor(s: String): Option[String] = condOpt(s) {
case "javabootclasspath" => settings.javabootclasspath.value
Expand Down Expand Up @@ -199,23 +198,13 @@ class PathResolver(implicit ctx: Context) {
* [scaladoc] ../scala-trunk/src/reflect/scala/reflect/macros/Reifiers.scala:89: error: object api is not a member of package reflect
* [scaladoc] case class ReificationException(val pos: reflect.api.PositionApi, val msg: String) extends Throwable(msg)
* [scaladoc] ^
* because the bootstrapping will look at the sourcepath and create package "reflect" in "<root>"
* and then when typing relative names, instead of picking <root>.scala.relect, typedIdentifier will pick up the
* <root>.reflect package created by the bootstrapping. Thus, no bootstrapping for scaladoc!
* TODO: we should refactor this as a separate -bootstrap option to have a clean implementation, no? */
* Because bootstrapping looks at the sourcepath and creates the package "reflect" in "<root>" it will cause the
* typedIdentifier to pick <root>.reflect instead of the <root>.scala.reflect package. Thus, no bootstrapping for scaladoc!
*/
def sourcePath: String = cmdLineOrElse("sourcepath", Defaults.scalaSourcePath)

/** Against my better judgment, giving in to martin here and allowing
* CLASSPATH to be used automatically. So for the user-specified part
* of the classpath:
*
* - If -classpath or -cp is given, it is that
* - Otherwise, if CLASSPATH is set, it is that
* - If neither of those, then "." is used.
*/
def userClassPath: String = {
if (!settings.classpath.isDefault)
settings.classpath.value
if (!settings.classpath.isDefault) settings.classpath.value
else sys.env.getOrElse("CLASSPATH", ".")
}

Expand Down