Skip to content

Instantly share code, notes, and snippets.

@mslinn
Last active December 26, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mslinn/7205854 to your computer and use it in GitHub Desktop.
Save mslinn/7205854 to your computer and use it in GitHub Desktop.
name := "demo"
version := "1.0-SNAPSHOT"
scalaVersion := "2.10.3"
version := "0.1.0"
play.Project.playScalaSettings
scalacOptions ++= Seq("-deprecation", "-encoding", "UTF-8", "-feature", "-target:jvm-1.6", "-unchecked",
"-Ywarn-adapted-args", "-Ywarn-value-discard", "-Xlint")
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
"org.webjars" %% "webjars-play" % "2.2.0",
"org.webjars" % "bootstrap" % "3.0.0",
"org.scala-lang" % "scala-compiler" % "2.10.3",
"org.scala-lang" % "scala-library" % "2.10.3",
"org.scala-lang" % "scala-reflect" % "2.10.3"
)
javaOptions in Test ++= Seq( "-Dconfig.file=conf/dev.conf" )
logBuffered in Test := false
Keys.fork in Test := false
parallelExecution in Test := false
// define the statements initially evaluated when entering 'console', 'console-quick', or 'console-project'
initialCommands := """ // make app resources accessible
|Thread.currentThread.setContextClassLoader(getClass.getClassLoader)
|new play.core.StaticApplication(new java.io.File("."))
|""".stripMargin
package views.helper
import java.io.{ByteArrayOutputStream, File, OutputStream}
import scala.tools.nsc.interpreter.IMain
import scala.tools.nsc.Settings
object eval {
val settings: Settings = {
lazy val urls = java.lang.Thread.currentThread.getContextClassLoader match {
case cl: java.net.URLClassLoader => cl.getURLs.toList
case _ => sys.error("classloader is not a URLClassLoader")
}
lazy val classpath = urls map {_.toString}
val tmp = new Settings()
tmp.bootclasspath.value = classpath.distinct mkString File.pathSeparator
tmp
}
val interpreter = new IMain(settings)
def apply(source: String): String =
stringFromStream { sout =>
Console.withOut(sout) {
interpreter.interpret(source)
}
}
// lets someone write to a stream and then we return the string contents of that stream
def stringFromStream(f: OutputStream => Any): String = {
val out = new ByteArrayOutputStream
f(out)
out.toString("UTF-8")
}
}
Failed to initialize compiler: object scala.runtime in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programatically, settings.usejavacp.value = true.
@()
@import views.helper.eval
@eval("List(1,2,3).map(_*2).mkString")
@aniketbhatnagar
Copy link

I have modified solution for specifying the classpath to make it work in a variety of environments. Here is what I did:

private def getClasspathUrls(classLoader: ClassLoader, acc: List[URL]): List[URL] = {
    classLoader match {
      case null                        => acc
      case cl: java.net.URLClassLoader => getClasspathUrls(classLoader.getParent, acc ++ cl.getURLs.toList)
      case c                           => LOGGER.error("classloader is not a URLClassLoader and will be skipped. ClassLoader type that was skipped is " + c.getClass)
                                          getClasspathUrls(classLoader.getParent, acc)
    }
}
val classpathUrls = getClasspathUrls(this.getClass.getClassLoader, List())
val classpathElements = classpathUrls map {url => url.toURI.getPath}
val classpath = classpathElements mkString java.io.File.pathSeparator
val settings = new Settings
settings.bootclasspath.value = classpath
val imain = new IMain(settings)
// use imain to interpret code. It should be able to access all your application classes as well as dependent libraries. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment