@@ -9,149 +9,19 @@ import Properties._
99
1010import scala .collection .JavaConverters ._
1111
12- object CompilerCommand {
13-
14- /** The name of the command */
15- def cmdName : String = " scalac"
16-
17- private def explainAdvanced = """
18- |-- Notes on option parsing --
19- |Boolean settings are always false unless set.
20- |Where multiple values are accepted, they should be comma-separated.
21- | example: -Xplugin:plugin1,plugin2
22- |<phases> means one or a comma-separated list of:
23- | - (partial) phase names with an optional "+" suffix to include the next phase
24- | - the string "all"
25- | example: -Xprint:all prints all phases.
26- | example: -Xprint:typer,mixin prints the typer and mixin phases.
27- | example: -Ylog:erasure+ logs the erasure phase and the phase after the erasure phase.
28- | This is useful because during the tree transform of phase X, we often
29- | already are in phase X + 1.
30- """
31-
32- def shortUsage : String = s " Usage: $cmdName <options> <source files> "
33-
34- def versionMsg : String = s " Scala compiler $versionString -- $copyrightString"
35-
36- def shouldStopWithInfo (using Context ): Boolean = {
37- val settings = ctx.settings
38- import settings ._
39- Set (help, Xhelp , Yhelp , showPlugins, XshowPhases ) exists (_.value)
40- }
41-
42- /** Distill arguments into summary detailing settings, errors and files to compiler */
43- def distill (args : Array [String ])(using Context ): ArgsSummary = {
44- /**
45- * Expands all arguments starting with @ to the contents of the
46- * file named like each argument.
47- */
48- def expandArg (arg : String ): List [String ] = {
49- def stripComment (s : String ) = s takeWhile (_ != '#' )
50- val path = Paths .get(arg stripPrefix " @" )
51- if (! Files .exists(path))
52- throw new java.io.FileNotFoundException (" argument file %s could not be found" format path.getFileName)
53-
54- val lines = Files .readAllLines(path) // default to UTF-8 encoding
55-
56- val params = lines.asScala map stripComment mkString " "
57- CommandLineParser .tokenize(params)
58- }
59-
60- // expand out @filename to the contents of that filename
61- def expandedArguments = args.toList flatMap {
62- case x if x startsWith " @" => expandArg(x)
63- case x => List (x)
64- }
65-
66- ctx.settings.processArguments(expandedArguments, processAll = true )
67- }
68-
69- /** Provide usage feedback on argument summary, assuming that all settings
70- * are already applied in context.
71- * @return The list of files to compile.
72- */
73- def checkUsage (summary : ArgsSummary , sourcesRequired : Boolean )(using Context ): List [String ] = {
74- val settings = ctx.settings
75-
76- /** Creates a help message for a subset of options based on cond */
77- def availableOptionsMsg (cond : Setting [? ] => Boolean ): String = {
78- val ss = (ctx.settings.allSettings filter cond).toList sortBy (_.name)
79- val width = (ss map (_.name.length)).max
80- def format (s : String ) = (" %-" + width + " s" ) format s
81- def helpStr (s : Setting [? ]) = {
82- def defaultValue = s.default match {
83- case _ : Int | _ : String => s.default.toString
84- case _ =>
85- // For now, skip the default values that do not make sense for the end user.
86- // For example 'false' for the version command.
87- " "
88- }
89- def formatSetting (name : String , value : String ) =
90- if (value.nonEmpty)
91- // the format here is helping to make empty padding and put the additional information exactly under the description.
92- s " \n ${format(" " )} $name: $value. "
93- else
94- " "
95- s " ${format(s.name)} ${s.description}${formatSetting(" Default" , defaultValue)}${formatSetting(" Choices" , s.legalChoices)}"
96- }
97- ss map helpStr mkString " \n "
98- }
99-
100- def createUsageMsg (label : String , shouldExplain : Boolean , cond : Setting [? ] => Boolean ): String = {
101- val prefix = List (
102- Some (shortUsage),
103- Some (explainAdvanced) filter (_ => shouldExplain),
104- Some (label + " options include:" )
105- ).flatten mkString " \n "
106-
107- prefix + " \n " + availableOptionsMsg(cond)
108- }
109-
110- def isStandard (s : Setting [? ]): Boolean = ! isAdvanced(s) && ! isPrivate(s)
111- def isAdvanced (s : Setting [? ]): Boolean = s.name.startsWith(" -X" ) && s.name != " -X"
112- def isPrivate (s : Setting [? ]) : Boolean = s.name.startsWith(" -Y" ) && s.name != " -Y"
113-
114- /** Messages explaining usage and options */
115- def usageMessage = createUsageMsg(" where possible standard" , shouldExplain = false , isStandard)
116- def xusageMessage = createUsageMsg(" Possible advanced" , shouldExplain = true , isAdvanced)
117- def yusageMessage = createUsageMsg(" Possible private" , shouldExplain = true , isPrivate)
118-
119- def phasesMessage : String = {
120- (new Compiler ()).phases.map {
121- case List (single) => single.phaseName
122- case more => more.map(_.phaseName).mkString(" {" , " , " , " }" )
123- }.mkString(" \n " )
124- }
125-
126- def infoMessage : String = {
127- import settings ._
128- if (help.value) usageMessage
129- else if (Xhelp .value) xusageMessage
130- else if (Yhelp .value) yusageMessage
131- else if (showPlugins.value) ctx.base.pluginDescriptions
132- else if (XshowPhases .value) phasesMessage
133- else " "
134- }
135-
136- // Print all warnings encountered during arguments parsing
137- summary.warnings.foreach(report.warning(_))
138-
139- if (summary.errors.nonEmpty) {
140- summary.errors foreach (report.error(_))
141- report.echo(" scalac -help gives more information" )
142- Nil
143- }
144- else if (settings.version.value) {
145- report.echo(versionMsg)
146- Nil
147- }
148- else if (shouldStopWithInfo) {
149- report.echo(infoMessage)
150- Nil
151- }
152- else {
153- if (sourcesRequired && summary.arguments.isEmpty) report.echo(usageMessage)
154- summary.arguments
155- }
156- }
157- }
12+ object CompilerCommand extends CliCommand :
13+ type ConcreteSettings = ScalaSettings
14+ override def cmdName : String = " scalac"
15+ override def versionMsg : String = s " Scala compiler $versionString -- $copyrightString"
16+ override def ifErrorsMsg : String = " scalac -help gives more information"
17+
18+ def infoMessage (using settings : ScalaSettings )(using SettingsState )(using Context ): String =
19+ if (settings.help.value) usageMessage
20+ else if (settings.Xhelp .value) xusageMessage
21+ else if (settings.Yhelp .value) yusageMessage
22+ else if (settings.showPlugins.value) ctx.base.pluginDescriptions
23+ else if (settings.XshowPhases .value) phasesMessage
24+ else " "
25+
26+ def shouldStopWithInfo (using settings : ScalaSettings )(using SettingsState ): Boolean =
27+ Set (settings.help, settings.Xhelp , settings.Yhelp , settings.showPlugins, settings.XshowPhases ) exists (_.value)
0 commit comments