
scala.tools.nsc.GenericRunnerCommand.scala Maven / Gradle / Ivy
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.tools.nsc
import GenericRunnerCommand._
import scala.reflect.internal.util.ScalaClassLoader
/** A command for ScriptRunner */
class GenericRunnerCommand(
args: List[String],
override val settings: GenericRunnerSettings)
extends CompilerCommand(args, settings) {
def this(args: List[String], error: String => Unit) =
this(args, new GenericRunnerSettings(error))
def this(args: List[String]) =
this(args, str => Console.println("Error: " + str))
override def cmdName = "scala"
override def cmdDesc = "code runner"
def compCmdName = "scalac" // super.cmdName
// change CompilerCommand behavior
override def shouldProcessArguments: Boolean = false
private val (_ok, targetAndArguments) = settings.processArguments(args, processAll = false)
override def ok = _ok
private def guessHowToRun(target: String): GenericRunnerCommand.HowToRun = {
if (!ok) Error
else if (io.Jar.isJarOrZip(target)) AsJar
else if (ScalaClassLoader.classExists(settings.classpathURLs, target)) AsObject
else {
val f = io.File(target)
if (!f.hasExtension("class", "jar", "zip") && f.canRead) AsScript
else {
Console.err.println("No such file or class on classpath: " + target)
Error
}
}
}
/** String with either the jar file, class name, or script file name. */
def thingToRun = targetAndArguments.headOption getOrElse ""
/** Arguments to thingToRun. */
def arguments = targetAndArguments drop 1
val howToRun = waysToRun.find(_.name == settings.howtorun.value) match {
case Some(how) => how
case _ if settings.execute.isSetByUser => AsScript
case _ if targetAndArguments.isEmpty => AsRepl
case _ => guessHowToRun(thingToRun)
}
def shortUsageMsg =
s"""|Usage: $cmdName [