package scala.tools.nsc
import java.io.IOException
import scala.collection.mutable.ListBuffer
import io.File
class CompilerCommand(arguments: List[String], val settings: Settings) {
def this(arguments: List[String], error: String => Unit) = this(arguments, new Settings(error))
type Setting = Settings#Setting
lazy val fileEndings = Properties.fileEndings
private val processArgumentsResult =
if (shouldProcessArguments) processArguments
else (true, Nil)
def ok = processArgumentsResult._1
def files = processArgumentsResult._2
def cmdName = "scalac"
private def explainAdvanced = "\n" + """
|-- Notes on option parsing --
|Boolean settings are always false unless set.
|Where multiple values are accepted, they should be comma-separated.
| example: -Xplugin:plugin1,plugin2
|<phase> means one or a list of:
| (partial) phase names, phase ids, phase id ranges, or the string "all".
| example: -Xprint:all prints all phases.
| example: -Xprint:expl,24-26 prints phases explicitouter, closelim, dce, jvm.
| example: -Xprint:-4 prints only the phases up to typer.
|
""".stripMargin.trim + "\n"
def shortUsage = "Usage: %s <options> <source files>" format cmdName
def createUsagePreface(shouldExplain: Boolean) =
if (shouldExplain) shortUsage + "\n" + explainAdvanced else ""
def createUsageMsg(cond: Setting => Boolean): String = {
val baseList = (settings.visibleSettings filter cond).toList sortBy (_.name)
val width = baseList map (_.helpSyntax.length) max
def format(s: String) = ("%-" + width + "s") format s
def helpStr(s: Setting) = {
val str = format(s.helpSyntax) + " " + s.helpDescription
val suffix = s.deprecationMessage match {
case Some(msg) => "\n" + format("") + " deprecated: " + msg
case _ => ""
}
str + suffix
}
val debugs = baseList filter (_.isForDebug)
val deprecateds = baseList filter (_.isDeprecated)
val theRest = baseList filterNot (debugs.toSet ++ deprecateds)
def sstring(msg: String, xs: List[Setting]) =
if (xs.isEmpty) None else Some(msg :: xs.map(helpStr) mkString "\n ")
List(
sstring("", theRest),
sstring("\nAdditional debug settings:", debugs),
sstring("\nDeprecated settings:", deprecateds)
).flatten mkString "\n"
}
def createUsageMsg(label: String, shouldExplain: Boolean, cond: Setting => Boolean): String = {
val prefix = List(
Some(shortUsage),
Some(explainAdvanced) filter (_ => shouldExplain),
Some(label + " options include:")
).flatten mkString "\n"
prefix + createUsageMsg(cond)
}
def usageMsg = createUsageMsg("where possible standard", false, _.isStandard)
def xusageMsg = createUsageMsg("Possible advanced", true, _.isAdvanced)
def yusageMsg = createUsageMsg("Possible private", true, _.isPrivate)
def shouldStopWithInfo = {
import settings.{ Setting => _, _ }
Set[BooleanSetting](help, Xhelp, Yhelp, showPlugins, showPhases) exists (_.value)
}
def getInfoMessage(global: Global): String = {
import settings._
if (help.value) usageMsg + global.pluginOptionsHelp
else if (Xhelp.value) xusageMsg
else if (Yhelp.value) yusageMsg
else if (showPlugins.value) global.pluginDescriptions
else if (showPhases.value) global.phaseDescriptions
else ""
}
def expandArg(arg: String): List[String] = {
def stripComment(s: String) = s takeWhile (_ != '#')
val file = File(arg stripPrefix "@")
if (!file.exists)
throw new java.io.FileNotFoundException("argument file %s could not be found" format file.name)
settings splitParams (file.lines() map stripComment mkString " ")
}
def shouldProcessArguments: Boolean = true
def processArguments: (Boolean, List[String]) = {
val expandedArguments = arguments flatMap {
case x if x startsWith "@" => expandArg(x)
case x => List(x)
}
settings.processArguments(expandedArguments, true)
}
}