Skip to content

Commit 8694783

Browse files
Bumps sbt up (#17)
* Bumps sbt up * Adds classpath and bootclasspath to the compiler instance settings
1 parent ae27655 commit 8694783

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.scalaexercises.exercises.compiler
2+
3+
object CompilerSettings {
4+
5+
private def classPathOfClass(className: String): List[String] = {
6+
val resource = className.split('.').mkString("/", "/", ".class")
7+
val path = getClass.getResource(resource).getPath
8+
if (path.indexOf("file:") >= 0) {
9+
val indexOfFile = path.indexOf("file:") + 5
10+
val indexOfSeparator = path.lastIndexOf('!')
11+
List(path.substring(indexOfFile, indexOfSeparator))
12+
} else {
13+
require(path.endsWith(resource))
14+
List(path.substring(0, path.length - resource.length + 1))
15+
}
16+
}
17+
18+
private lazy val compilerPath =
19+
try classPathOfClass("scala.tools.nsc.Interpreter")
20+
catch {
21+
case e: Throwable =>
22+
throw new RuntimeException(
23+
"Unable to load Scala interpreter from classpath (scala-compiler jar is missing?)",
24+
e
25+
)
26+
}
27+
28+
private lazy val libPath =
29+
try classPathOfClass("scala.AnyVal")
30+
catch {
31+
case e: Throwable =>
32+
throw new RuntimeException(
33+
"Unable to load scala base object from classpath (scala-library jar is missing?)",
34+
e
35+
)
36+
}
37+
38+
lazy val paths: List[String] = compilerPath ::: libPath
39+
}

Diff for: compiler/src/main/scala/org/scalaexercises/exercises/compiler/SourceTextExtraction.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package org.scalaexercises.compiler
1818

19-
import scala.annotation.tailrec
19+
import java.io.File
20+
21+
import org.scalaexercises.exercises.compiler.CompilerSettings
2022

23+
import scala.annotation.tailrec
2124
import scala.collection.compat._
2225
import scala.reflect.internal.util.BatchSourceFile
2326
import scala.tools.nsc._
2427
import scala.tools.nsc.doc.{Settings => _, _}
25-
2628
import scala.tools.nsc.doc.base.comment.Comment
2729

2830
class SourceTextExtraction {
@@ -287,10 +289,15 @@ class DocExtractionGlobal(settings: Settings = DocExtractionGlobal.defaultSettin
287289
}
288290

289291
object DocExtractionGlobal {
292+
290293
def defaultSettings =
291294
new Settings {
292295
embeddedDefaults[DocExtractionGlobal.type]
293296
// this flag is crucial for method body extraction
294297
Yrangepos.value = true
298+
usejavacp.value = true
299+
300+
bootclasspath.value = CompilerSettings.paths.mkString(File.pathSeparator)
301+
classpath.value = CompilerSettings.paths.mkString(File.pathSeparator)
295302
}
296303
}

Diff for: compiler/src/test/scala/org/scalaexercises/exercises/compiler/CompilerSpec.scala

+24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package org.scalaexercises.compiler
1818

19+
import java.io.File
20+
import java.net.URLClassLoader
21+
1922
import org.scalaexercises.definitions.{BuildInfo, Library}
23+
import org.scalaexercises.exercises.compiler.CompilerSettings
2024
import org.scalatest.funspec.AnyFunSpec
2125
import org.scalatest.matchers.should.Matchers
2226

@@ -148,8 +152,28 @@ class CompilerSpec extends AnyFunSpec with Matchers {
148152
}
149153

150154
object globalUtil {
155+
156+
def getClassPath(cl: ClassLoader, acc: List[List[String]] = List.empty): List[List[String]] = {
157+
val cp = cl match {
158+
case urlClassLoader: URLClassLoader =>
159+
urlClassLoader.getURLs
160+
.filter(_.getProtocol == "file")
161+
.map(u => new File(u.toURI).getPath)
162+
.toList
163+
case _ => Nil
164+
}
165+
cl.getParent match {
166+
case null => (cp :: acc).reverse
167+
case parent => getClassPath(parent, cp :: acc)
168+
}
169+
}
170+
171+
val currentClassPath: List[String] = getClassPath(this.getClass.getClassLoader).head
151172
val global = new Global(new Settings {
152173
embeddedDefaults[CompilerSpec]
174+
175+
bootclasspath.value = CompilerSettings.paths.mkString(File.pathSeparator)
176+
classpath.value = (CompilerSettings.paths ::: currentClassPath).mkString(File.pathSeparator)
153177
})
154178
val outputTarget = new VirtualDirectory("(memory)", None)
155179
global.settings.outputDirs.setSingleOutput(outputTarget)

Diff for: project/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.2.8
1+
sbt.version=1.3.13

0 commit comments

Comments
 (0)