Skip to content

Commit 66cc819

Browse files
Add a clear Main entrypoint for the JVM binary (#883)
As a newcomer to this project, I found it hard to find the entrypoint to the JVM binary. This PR adds a file `Main.scala` under `jvm/src/main/scala/effekt` which should be easy to find and understand. This could also be one of the first baby steps to disentangle some parts of this project from Kiama such as the LSP Server. The logic *should* reproduce the current behavior as far as I understand it.
1 parent 48c4435 commit 66cc819

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

build.sbt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ lazy val effekt: CrossProject = crossProject(JSPlatform, JVMPlatform).in(file("e
112112

113113
// Assembling one big jar-file and packaging it
114114
// --------------------------------------------
115-
assembly / mainClass := Some("effekt.Server"),
115+
assembly / mainClass := Some("effekt.Main"),
116+
117+
// Without this overwrite, sbt shows both Server and Main as main classes, but we do not use Server as an entrypoint.
118+
Compile / discoveredMainClasses := Seq("effekt.Main"),
116119

117120
assembly / assemblyJarName := "effekt.jar",
118121

@@ -131,7 +134,7 @@ lazy val effekt: CrossProject = crossProject(JSPlatform, JVMPlatform).in(file("e
131134
Compile / unmanagedResourceDirectories += (ThisBuild / baseDirectory).value / "licenses",
132135

133136
// cli flag so sbt doesn't crash when effekt does
134-
addCommandAlias("run", "runMain effekt.Server --no-exit-on-error"),
137+
addCommandAlias("run", "runMain effekt.Main --no-exit-on-error"),
135138

136139
assembleBinary := {
137140
val jarfile = assembly.value

effekt/jvm/src/main/scala/effekt/Driver.scala

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ trait Driver extends kiama.util.Compiler[EffektConfig, EffektError] { outer =>
2727
// We always only have one global instance of the compiler
2828
object context extends Context(positions) with IOModuleDB { val messaging = outer.messaging }
2929

30-
/**
31-
* If no file names are given, run the REPL
32-
*/
33-
override def run(config: EffektConfig): Unit =
34-
if (config.repl()) {
35-
new Repl(this).run(config)
36-
// This is overridden by kiama.Server to launch the LSP server.
37-
// TODO: remove dynamic dispatch here and consider replacing inheritance by composition.
38-
} else if (config.server()) {
39-
super.run(config)
40-
} else for (filename <- config.filenames()) {
41-
compileFile(filename, config)
42-
}
43-
4430
override def createConfig(args: Seq[String]) =
4531
new EffektConfig(args)
4632

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package effekt
2+
3+
import org.rogach.scallop.exceptions.ScallopException
4+
5+
object Main {
6+
/**
7+
* Main entry point for the Effekt compiler.
8+
*
9+
* Depending on the command line arguments, we run in one of the following modes:
10+
*
11+
* - Launch the Effekt language server (e.g. `effekt --server`)
12+
* - Launch the REPL (e.g. `effekt`)
13+
* - Build the provided files (e.g. `effekt --build hello.effekt`)
14+
*/
15+
def main(args: Array[String]): Unit = {
16+
val config = try {
17+
parseArgs(args)
18+
} catch {
19+
case e: ScallopException =>
20+
System.err.println(e.getMessage())
21+
return
22+
}
23+
24+
if (config.server()) {
25+
Server.launch(config)
26+
} else if (config.repl()) {
27+
new Repl(Server).run(config)
28+
} else {
29+
compileFiles(config)
30+
}
31+
}
32+
33+
/**
34+
* Parse command line arguments into an EffektConfig.
35+
*/
36+
private def parseArgs(args: Array[String]): EffektConfig = {
37+
val config = new EffektConfig(args.toIndexedSeq)
38+
config.verify()
39+
config
40+
}
41+
42+
/**
43+
* Compile files specified in the configuration.
44+
*/
45+
private def compileFiles(config: EffektConfig): Unit = {
46+
for (filename <- config.filenames()) {
47+
Server.compileFile(filename, config)
48+
}
49+
}
50+
}

effekt/jvm/src/main/scala/effekt/Server.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,6 @@ trait LSPServer extends kiama.util.Server[Tree, EffektConfig, EffektError] with
229229
}
230230

231231
/**
232-
* Main entry point for Effekt
232+
* Singleton for the language server
233233
*/
234234
object Server extends LSPServer

0 commit comments

Comments
 (0)