File tree Expand file tree Collapse file tree 4 files changed +73
-3
lines changed
main/scala/org/scalaexercises/exercises/compiler
test/scala/org/scalaexercises/exercises/compiler Expand file tree Collapse file tree 4 files changed +73
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1616
1717package 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
2124import scala .collection .compat ._
2225import scala .reflect .internal .util .BatchSourceFile
2326import scala .tools .nsc ._
2427import scala .tools .nsc .doc .{Settings => _ , _ }
25-
2628import scala .tools .nsc .doc .base .comment .Comment
2729
2830class SourceTextExtraction {
@@ -287,10 +289,15 @@ class DocExtractionGlobal(settings: Settings = DocExtractionGlobal.defaultSettin
287289}
288290
289291object 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}
Original file line number Diff line number Diff line change 1616
1717package org .scalaexercises .compiler
1818
19+ import java .io .File
20+ import java .net .URLClassLoader
21+
1922import org .scalaexercises .definitions .{BuildInfo , Library }
23+ import org .scalaexercises .exercises .compiler .CompilerSettings
2024import org .scalatest .funspec .AnyFunSpec
2125import 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)
Original file line number Diff line number Diff line change 1- sbt.version =1.2.8
1+ sbt.version =1.3.13
You can’t perform that action at this time.
0 commit comments