sbt/compile/interface/ConsoleInterface.scala

65 lines
2.2 KiB
Scala
Raw Normal View History

2009-10-08 03:27:53 +02:00
/* sbt -- Simple Build Tool
* Copyright 2008, 2009 Mark Harrah
*/
package xsbt
import xsbti.Logger
import scala.tools.nsc.{GenericRunnerCommand, Interpreter, InterpreterLoop, ObjectRunner, Settings}
import scala.tools.nsc.interpreter.InteractiveReader
import scala.tools.nsc.reporters.Reporter
import scala.tools.nsc.util.ClassPath
2009-10-08 03:27:53 +02:00
class ConsoleInterface
{
def run(args: Array[String], bootClasspathString: String, classpathString: String, initialCommands: String, loader: ClassLoader, bindNames: Array[String], bindValues: Array[Any], log: Logger)
2009-10-08 03:27:53 +02:00
{
val options = args.toList
lazy val interpreterSettings = xsbt.MakeSettings(options, log)
val compilerSettings = xsbt.MakeSettings(options, log)
2010-03-23 01:42:59 +01:00
if(!bootClasspathString.isEmpty)
compilerSettings.bootclasspath.value = bootClasspathString
compilerSettings.classpath.value = classpathString
2009-10-08 03:27:53 +02:00
log.info(Message("Starting scala interpreter..."))
log.debug(Message(" Boot classpath: " + compilerSettings.bootclasspath.value))
log.debug(Message(" Classpath: " + compilerSettings.classpath.value))
2009-10-08 03:27:53 +02:00
log.info(Message(""))
val loop = new InterpreterLoop {
2009-10-08 03:27:53 +02:00
override def createInterpreter() = {
if(loader ne null)
{
in = InteractiveReader.createDefault()
interpreter = new Interpreter(settings)
{
override protected def parentClassLoader = if(loader eq null) super.parentClassLoader else loader
override protected def newCompiler(settings: Settings, reporter: Reporter) = super.newCompiler(compilerSettings, reporter)
}
interpreter.setContextClassLoader()
}
else
super.createInterpreter()
for( (id, value) <- bindNames zip bindValues)
interpreter.bind(id, value.asInstanceOf[AnyRef].getClass.getName, value)
if(!initialCommands.isEmpty)
interpreter.interpret(initialCommands)
2009-10-08 03:27:53 +02:00
}
}
loop.main(if(loader eq null) compilerSettings else interpreterSettings)
2009-10-08 03:27:53 +02:00
}
}
2010-03-26 21:15:52 +01:00
object MakeSettings
2009-10-08 03:27:53 +02:00
{
2010-04-25 19:18:36 +02:00
def apply(args: List[String], log: Logger) =
2009-10-08 03:27:53 +02:00
{
2010-04-25 19:18:36 +02:00
val command = new GenericRunnerCommand(args, message => log.error(Message(message)))
2009-10-08 03:27:53 +02:00
if(command.ok)
command.settings
else
throw new InterfaceCompileFailed(Array(), Array(), command.usageMsg)
2009-10-08 03:27:53 +02:00
}
}