Skip to content

Commit

Permalink
Bumps sbt up (#17)
Browse files Browse the repository at this point in the history
* Bumps sbt up

* Adds classpath and bootclasspath to the compiler instance settings
  • Loading branch information
juanpedromoreno authored Aug 10, 2020
1 parent ae27655 commit 8694783
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.scalaexercises.exercises.compiler

object CompilerSettings {

private def classPathOfClass(className: String): List[String] = {
val resource = className.split('.').mkString("/", "/", ".class")
val path = getClass.getResource(resource).getPath
if (path.indexOf("file:") >= 0) {
val indexOfFile = path.indexOf("file:") + 5
val indexOfSeparator = path.lastIndexOf('!')
List(path.substring(indexOfFile, indexOfSeparator))
} else {
require(path.endsWith(resource))
List(path.substring(0, path.length - resource.length + 1))
}
}

private lazy val compilerPath =
try classPathOfClass("scala.tools.nsc.Interpreter")
catch {
case e: Throwable =>
throw new RuntimeException(
"Unable to load Scala interpreter from classpath (scala-compiler jar is missing?)",
e
)
}

private lazy val libPath =
try classPathOfClass("scala.AnyVal")
catch {
case e: Throwable =>
throw new RuntimeException(
"Unable to load scala base object from classpath (scala-library jar is missing?)",
e
)
}

lazy val paths: List[String] = compilerPath ::: libPath
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package org.scalaexercises.compiler

import scala.annotation.tailrec
import java.io.File

import org.scalaexercises.exercises.compiler.CompilerSettings

import scala.annotation.tailrec
import scala.collection.compat._
import scala.reflect.internal.util.BatchSourceFile
import scala.tools.nsc._
import scala.tools.nsc.doc.{Settings => _, _}

import scala.tools.nsc.doc.base.comment.Comment

class SourceTextExtraction {
Expand Down Expand Up @@ -287,10 +289,15 @@ class DocExtractionGlobal(settings: Settings = DocExtractionGlobal.defaultSettin
}

object DocExtractionGlobal {

def defaultSettings =
new Settings {
embeddedDefaults[DocExtractionGlobal.type]
// this flag is crucial for method body extraction
Yrangepos.value = true
usejavacp.value = true

bootclasspath.value = CompilerSettings.paths.mkString(File.pathSeparator)
classpath.value = CompilerSettings.paths.mkString(File.pathSeparator)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package org.scalaexercises.compiler

import java.io.File
import java.net.URLClassLoader

import org.scalaexercises.definitions.{BuildInfo, Library}
import org.scalaexercises.exercises.compiler.CompilerSettings
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

Expand Down Expand Up @@ -148,8 +152,28 @@ class CompilerSpec extends AnyFunSpec with Matchers {
}

object globalUtil {

def getClassPath(cl: ClassLoader, acc: List[List[String]] = List.empty): List[List[String]] = {
val cp = cl match {
case urlClassLoader: URLClassLoader =>
urlClassLoader.getURLs
.filter(_.getProtocol == "file")
.map(u => new File(u.toURI).getPath)
.toList
case _ => Nil
}
cl.getParent match {
case null => (cp :: acc).reverse
case parent => getClassPath(parent, cp :: acc)
}
}

val currentClassPath: List[String] = getClassPath(this.getClass.getClassLoader).head
val global = new Global(new Settings {
embeddedDefaults[CompilerSpec]

bootclasspath.value = CompilerSettings.paths.mkString(File.pathSeparator)
classpath.value = (CompilerSettings.paths ::: currentClassPath).mkString(File.pathSeparator)
})
val outputTarget = new VirtualDirectory("(memory)", None)
global.settings.outputDirs.setSingleOutput(outputTarget)
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.8
sbt.version=1.3.13

0 comments on commit 8694783

Please sign in to comment.