File tree 4 files changed +73
-3
lines changed
main/scala/org/scalaexercises/exercises/compiler
test/scala/org/scalaexercises/exercises/compiler
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 16
16
17
17
package org .scalaexercises .compiler
18
18
19
- import scala .annotation .tailrec
19
+ import java .io .File
20
+
21
+ import org .scalaexercises .exercises .compiler .CompilerSettings
20
22
23
+ import scala .annotation .tailrec
21
24
import scala .collection .compat ._
22
25
import scala .reflect .internal .util .BatchSourceFile
23
26
import scala .tools .nsc ._
24
27
import scala .tools .nsc .doc .{Settings => _ , _ }
25
-
26
28
import scala .tools .nsc .doc .base .comment .Comment
27
29
28
30
class SourceTextExtraction {
@@ -287,10 +289,15 @@ class DocExtractionGlobal(settings: Settings = DocExtractionGlobal.defaultSettin
287
289
}
288
290
289
291
object DocExtractionGlobal {
292
+
290
293
def defaultSettings =
291
294
new Settings {
292
295
embeddedDefaults[DocExtractionGlobal .type ]
293
296
// this flag is crucial for method body extraction
294
297
Yrangepos .value = true
298
+ usejavacp.value = true
299
+
300
+ bootclasspath.value = CompilerSettings .paths.mkString(File .pathSeparator)
301
+ classpath.value = CompilerSettings .paths.mkString(File .pathSeparator)
295
302
}
296
303
}
Original file line number Diff line number Diff line change 16
16
17
17
package org .scalaexercises .compiler
18
18
19
+ import java .io .File
20
+ import java .net .URLClassLoader
21
+
19
22
import org .scalaexercises .definitions .{BuildInfo , Library }
23
+ import org .scalaexercises .exercises .compiler .CompilerSettings
20
24
import org .scalatest .funspec .AnyFunSpec
21
25
import org .scalatest .matchers .should .Matchers
22
26
@@ -148,8 +152,28 @@ class CompilerSpec extends AnyFunSpec with Matchers {
148
152
}
149
153
150
154
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
151
172
val global = new Global (new Settings {
152
173
embeddedDefaults[CompilerSpec ]
174
+
175
+ bootclasspath.value = CompilerSettings .paths.mkString(File .pathSeparator)
176
+ classpath.value = (CompilerSettings .paths ::: currentClassPath).mkString(File .pathSeparator)
153
177
})
154
178
val outputTarget = new VirtualDirectory (" (memory)" , None )
155
179
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