Skip to content

Commit 1947222

Browse files
authored
Merge pull request #5756 from sudohalt/patch-1
Slight refactoring of PathResolver
2 parents 0660c3e + 2ea2bec commit 1947222

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

compiler/src/dotty/tools/dotc/config/PathResolver.scala

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ package dotc
33
package config
44

55
import WrappedProperties.AccessControl
6-
import io.{ ClassPath, Directory, Path }
7-
import classpath.{AggregateClassPath, ClassPathFactory, JrtClassPath }
6+
import io.{ClassPath, Directory, Path}
7+
import classpath.{AggregateClassPath, ClassPathFactory, JrtClassPath}
88
import ClassPath.split
99
import PartialFunction.condOpt
1010
import scala.language.postfixOps
1111
import core.Contexts._
1212
import Settings._
1313
import dotty.tools.io.File
1414

15-
// Loosely based on the draft specification at:
16-
// https://wiki.scala-lang.org/display/SW/Classpath
17-
1815
object PathResolver {
1916

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

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

30-
/** pretty print class path */
27+
/** pretty print class path
28+
*/
3129
def ppcp(s: String): String = split(s) match {
3230
case Nil => ""
3331
case Seq(x) => x
@@ -53,7 +51,8 @@ object PathResolver {
5351
def scalaHome: String = propOrEmpty("scala.home")
5452
def scalaExtDirs: String = propOrEmpty("scala.ext.dirs")
5553

56-
/** The java classpath and whether to use it. */
54+
/** The java classpath and whether to use it.
55+
*/
5756
def javaUserClassPath: String = propOrElse("java.class.path", "")
5857
def useJavaClassPath: Boolean = propOrFalse("scala.usejavacp")
5958

@@ -77,14 +76,14 @@ object PathResolver {
7776
def javaExtDirs: String = Environment.javaExtDirs
7877
def useJavaClassPath: Boolean = Environment.useJavaClassPath
7978

80-
def scalaHome: String = Environment.scalaHome
81-
def scalaHomeDir: Directory = Directory(scalaHome)
82-
def scalaHomeExists: Boolean = scalaHomeDir.isDirectory
83-
def scalaLibDir: Directory = (scalaHomeDir / "lib").toDirectory
84-
def scalaClassesDir: Directory = (scalaHomeDir / "classes").toDirectory
79+
def scalaHome: String = Environment.scalaHome
80+
def scalaHomeDir: Directory = Directory(scalaHome)
81+
def scalaHomeExists: Boolean = scalaHomeDir.isDirectory
82+
def scalaLibDir: Directory = (scalaHomeDir / "lib").toDirectory
83+
def scalaClassesDir: Directory = (scalaHomeDir / "classes").toDirectory
8584

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

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

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

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

167167
private val classPathFactory = new ClassPathFactory
168168

169-
private def cmdLineOrElse(name: String, alt: String) = {
170-
(commandLineFor(name) match {
171-
case Some("") => None
172-
case x => x
173-
}) getOrElse alt
174-
}
169+
private def cmdLineOrElse(name: String, alt: String) =
170+
commandLineFor(name) match {
171+
case Some("") | None => alt
172+
case Some(x) => x
173+
}
175174

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

208-
/** Against my better judgment, giving in to martin here and allowing
209-
* CLASSPATH to be used automatically. So for the user-specified part
210-
* of the classpath:
211-
*
212-
* - If -classpath or -cp is given, it is that
213-
* - Otherwise, if CLASSPATH is set, it is that
214-
* - If neither of those, then "." is used.
215-
*/
216206
def userClassPath: String = {
217-
if (!settings.classpath.isDefault)
218-
settings.classpath.value
207+
if (!settings.classpath.isDefault) settings.classpath.value
219208
else sys.env.getOrElse("CLASSPATH", ".")
220209
}
221210

0 commit comments

Comments
 (0)