Skip to content

Commit 6beb1b4

Browse files
committed
Refactor Driver
- Make parts more reusable - Introduce hook "sourcesRequired" that controls whether no sources on the command line give a help message.
1 parent de8368d commit 6beb1b4

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/dotty/tools/dotc/Driver.scala

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,40 @@ import scala.util.control.NonFatal
88

99
abstract class Driver extends DotClass {
1010

11-
val prompt = "\ndotc>"
11+
val prompt = "\ndotc> "
1212

1313
protected def newCompiler(): Compiler
1414

1515
protected def emptyReporter: Reporter = new StoreReporter
1616

17-
protected def doCompile(compiler: Compiler, fileNames: List[String], reporter: Option[Reporter] = None)
18-
(implicit ctx: Context): Reporter =
19-
if (fileNames.nonEmpty) {
20-
val run = compiler.newRun(ctx, reporter)
21-
run.compile(fileNames)
22-
run.printSummary()
23-
} else emptyReporter
17+
protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter =
18+
if (fileNames.nonEmpty)
19+
try {
20+
val run = compiler.newRun
21+
run.compile(fileNames)
22+
run.printSummary()
23+
}
24+
catch {
25+
case ex: FatalError =>
26+
ctx.error(ex.getMessage) // signals that we should fail compilation.
27+
ctx.typerState.reporter
28+
}
29+
else emptyReporter
2430

2531
protected def initCtx = (new ContextBase).initialCtx
2632

27-
def process(args: Array[String], rootCtx: Context, reporter: Option[Reporter] = None): Reporter = {
33+
protected def sourcesRequired = true
34+
35+
def setup(args: Array[String], rootCtx: Context): (List[String], Context) = {
2836
val summary = CompilerCommand.distill(args)(rootCtx)
2937
implicit val ctx: Context = initCtx.fresh.setSettings(summary.sstate)
30-
val fileNames = CompilerCommand.checkUsage(summary)
31-
try {
32-
doCompile(newCompiler(), fileNames, reporter)
33-
} catch {
34-
case ex: FatalError =>
35-
ctx.error(ex.getMessage) // signals that we should fail compilation.
36-
ctx.typerState.reporter
37-
}
38+
val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)
39+
(fileNames, ctx)
40+
}
41+
42+
def process(args: Array[String], rootCtx: Context): Reporter = {
43+
val (fileNames, ctx) = setup(args, rootCtx)
44+
doCompile(newCompiler(), fileNames)(ctx)
3845
}
3946

4047
def main(args: Array[String]): Unit =

src/dotty/tools/dotc/config/CompilerCommand.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object CompilerCommand extends DotClass {
6060
* are already applied in context.
6161
* @return The list of files to compile.
6262
*/
63-
def checkUsage(summary: ArgsSummary)(implicit ctx: Context): List[String] = {
63+
def checkUsage(summary: ArgsSummary, sourcesRequired: Boolean)(implicit ctx: Context): List[String] = {
6464
val settings = ctx.settings
6565

6666
/** Creates a help message for a subset of options based on cond */
@@ -121,8 +121,7 @@ object CompilerCommand extends DotClass {
121121
ctx.println(infoMessage)
122122
Nil
123123
} else {
124-
if (summary.arguments.isEmpty && !settings.resident.value)
125-
ctx.println(usageMessage)
124+
if (sourcesRequired && summary.arguments.isEmpty) ctx.println(usageMessage)
126125
summary.arguments
127126
}
128127
}

0 commit comments

Comments
 (0)